function test04 ( )

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

  A = 1.0;
  C = 4.0;
  x0 = 1.0;
  M1 = 0.0;
  M2 = 2.0;
  T = 1.0;
  N = 1000;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Parameters:\n' );
  fprintf ( 1, '  A  = %g\n', A );
  fprintf ( 1, '  C  = %g\n', C );
  fprintf ( 1, '  x0 = %g\n', x0 );
  fprintf ( 1, '  M1 = %g\n', M1 );
  fprintf ( 1, '  M2 = %g\n', M2 );
  fprintf ( 1, '  T  = %g\n', T );
  fprintf ( 1, '  N  = %g\n', N );

  [ t, x, u, lambda, cost ] = opt04 ( A, C, x0, M1, M2, T, N );

  clf ( );
  plot ( t, x, 'linewidth', 3 );
  grid ( 'on' );
  xlabel ( '<-- t -->' );
  ylabel ( '<-- X(t) -->' );
  title ( 'State variable X(t)' );
  filename = 'test04_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 = 'test04_u.png';
  print ( '-dpng', filename );
  fprintf ( 1, '    Graphics saved as "%s"\n', filename );

  clf ( );
  plot ( t, lambda, 'linewidth', 3 );
  grid ( 'on' );
  xlabel ( '<-- t -->' );
  ylabel ( '<-- \lambda(t) -->' );
  title ( 'Adjoint \lambda(t)' );
  filename = 'test04_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 = 'test04_cost.png';
  print ( '-dpng', filename );
  fprintf ( 1, '    Graphics saved as "%s"\n', filename );

  return
end

