function test12 ( )

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

  K = 2.1;
  G = 1.2;
  D = 1.0;
  x0 = [ 0.5, 0.1 ];
  M = 1;
  T = 2.0;
  N = 1000;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Parameters:\n' );
  fprintf ( 1, '  K  = %g\n', K );
  fprintf ( 1, '  G  = %g\n', G );
  fprintf ( 1, '  D  = %g\n', D );
  fprintf ( 1, '  x0 = %g, %g\n', x0 );
  fprintf ( 1, '  M  = %g\n', M );
  fprintf ( 1, '  T  = %g\n', T );
  fprintf ( 1, '  N  = %d\n', N );

  [ t, x, u, l, cost ] = opt12 ( K, G, D, x0, M, T, N );

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

  clf ( );
  plot ( t, u, 'linewidth', 3 );
  grid ( 'on' );
  xlabel ( '<-- t -->' );
  ylabel ( '<-- U(t) -->' );
  title ( 'Control U(t)' );
  filename = 'test12_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 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<-- t -->' );
  ylabel ( '<-- \lambda(t) -->' );
  title ( 'Adjoint \lambda(t)' );
  legend ( '\lambda_1(t)', '\lambda_2(t)' );
  filename = 'test12_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 = 'test12_cost.png';
  print ( '-dpng', filename );
  fprintf ( 1, '    Graphics saved as "%s"\n', filename );

  return
end

