function test09 ( )

%*****************************************************************************80
%
%% test09() computes optimal control for case #9.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    07 October 2022
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'test09():\n' );
  fprintf ( 1, '  Optimal control for case #9.\n' );
  fprintf ( 1, '  Bear Populations:\n' );

  r = 0.1;
  K = 0.75;
  mp = 0.5;
  mf = 0.5;
  P0 = 0.4;
  F0 = 0.2;
  O0 = 0.0;
  cp = 10000;
  cf = 10;
  T = 25;
  N = 1000;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Parameters:\n' );
  fprintf ( 1, '  r  = %g\n', r );
  fprintf ( 1, '  K  = %g\n', K );
  fprintf ( 1, '  mp = %g\n', mp );
  fprintf ( 1, '  mf = %g\n', mf );
  fprintf ( 1, '  P0 = %g\n', P0 );
  fprintf ( 1, '  F0 = %g\n', F0 );
  fprintf ( 1, '  O0 = %g\n', O0 );
  fprintf ( 1, '  cp = %g\n', cp );
  fprintf ( 1, '  cf = %g\n', cf );
  fprintf ( 1, '  T  = %g\n', T );
  fprintf ( 1, '  N  = %d\n', N );

  [ t, x, u, l, cost ] = opt09 ( r, K, mp, mf, P0, F0, O0, cp, cf, T, N );

  clf ( );
  hold ( 'on' );
    plot ( t, x(:,1), 'r-', 'linewidth', 3 );
    plot ( t, x(:,2), 'b--', 'linewidth', 3 );
    plot ( t, x(:,3), 'g:', 'linewidth', 3 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<-- t -->' );
  ylabel ( '<-- P/F/O -->' );
  title ( 'State variables' );
  legend ( 'P', 'F', 'O' );
  filename = 'test09_x.png';
  print ( '-dpng', filename );
  fprintf ( 1, '    Graphics saved as "%s"\n', filename );

  clf ( );
  hold ( 'on' );
    plot ( t, u(:,1), 'linewidth', 3 );
    plot ( t, u(:,2), 'linewidth', 3 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<-- t -->' );
  ylabel ( '<-- U(t) -->' );
  title ( 'Control U(t)' );
  legend ( 'up', 'uf' );
  filename = 'test09_u.png';
  print ( '-dpng', filename );
  fprintf ( 1, '    Graphics saved as "%s"\n', filename );

  clf ( );
  hold ( 'on' );
    plot ( t, l(:,1), 'linewidth', 3 );
    plot ( t, l(:,2), 'linewidth', 3 );
    plot ( t, l(:,3), 'linewidth', 3 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<-- t -->' );
  ylabel ( '<-- \lambda(t) -->' );
  title ( 'Adjoints \lambda(t)' );
  legend ( '\lambda_1', '\lambda_2', '\lambda_3' );
  filename = 'test09_lambda.png';
  print ( '-dpng', filename );
  fprintf ( 1, '    Graphics saved as "%s"\n', filename );

  clf ( );
  plot ( cost, 'linewidth', 3 );
  grid ( 'on' );
  xlabel ( '<-- iteration -->' );
  ylabel ( '<-- cost -->' );
  title ( 'Cost function' );
  filename = 'test09_cost.png';
  print ( '-dpng', filename );
  fprintf ( 1, '    Graphics saved as "%s"\n', filename );

  return
end

