function [ t, x, u, l, cost ] = opt02 ( r, M, A, x0, T, N )

%*****************************************************************************80
%
%% opt02() performs optimal control for case #02.
%
%  Discussion:
%
%    Mold and Fungicide
%
%    minimize
%      integral ( 0 <= t <= 1 ) A x(t)^2 + u(t)^2 dt
%    subject to
%      x'(t) = r ( M - x(t) ) - u(t) x(t)
%      x(0) = x0
%    with Hamiltonian:
%      H = A x(t)^2 + u(t)^2 + lambda(t) ( r ( M - x(t) ) - u(t) x(t) )
%    with adjoint equation:
%      lambda'(t) = - 2 A x(t) + r lambda(t) + u(t) lambda(t)
%      lambda(1) = 0
%    and optimality condition:
%      2 u* - lambda* x* = 0
%
%  Modified:
%
%    06 October 2022
%
%  Author:
%
%    Suzanne Lenhart, John Workman
%    Modifications by John Burkardt
%
%  Reference:
%
%    Suzanne Lenhart, John Workman,
%    Optimal Control Applied to Biological Models,
%    Chapman and Hall, 2007
%    LC: QH324.8.L46,
%    ISBN: 978-1-58488-640-2
%
%  Input:
%
%    real r: the growth rate of the mold.
%
%    real M: the carrying capacity.
%
%    real A: the coefficient of x in the cost function.
%
%    real x0: the initial value for the state.
%
%    real T: the final time.
%
%    integer N: the number of time steps.
%
%  Output:
%
%    real t(N+1): the time values.
%
%    real x(N+1): the final state estimate.
%
%    real u(N+1): the final optimal control estimate.
%
%    real l(N+1): the final adjoint estimate.
%
%    real cost(*): the cost history.
%
  t0 = 0.0;
  t1 = T;
%
%  Initialize the problem variables.
%
  t = linspace ( t0, t1, N+1 );
  t = t';
  tb = t(end:-1:1);
  x = zeros(N+1,1);
  x(1) = x0;
  u = zeros(N+1,1);
  l = zeros(N+1,1);
  l1 = 0.0;
  cost = [];
%
%  Forward/backward sweep.
%
  it = 0;

  while ( true )
%
%  Save previous variables for convergence test.
%
    oldu = u;
    oldx = x;
    oldl = l;
%
%  Define the right hand side of the state equation.
%
    dxdt = @(tval,xval) g ( tval, xval, t, x, u, r, M );
%
%  Solve state equation forward in time.
%
    [ t, x ] = ode45 ( dxdt, t, x0 );
%
%  Reverse time for adjoint equation.
%
    xb = x(end:-1:1);
    ub = u(end:-1:1);
    lb = l(end:-1:1);
%
%  Define the right hand side of the adjoint equation.
%
    dldt = @(tval,lval) h ( tval, lval, tb, xb, ub, lb, r, A );
%
%  Solve the adjoint equation "in the wrong time direction".
%
    [ tb, lb ] = ode45 ( dldt, tb, l1 );
%
%  Re-reverse adjoint variable.
%
    l = lb(end:-1:1);
%
%  Update the control.
%
    u1 = 0.5 * l .* x;
    u = 0.5 * ( oldu + u1 );
%
%  Update the cost.
%
    f = A * x.^2 + u.^2;
    fint = trapz ( t, f );
    cost = [ cost, fint ];
%
%  Convergence test.
%   
    ratio = Inf;
    ratio = convergence ( ratio, u, oldu );
    ratio = convergence ( ratio, x, oldx );
    ratio = convergence ( ratio, l, oldl );
    it = it + 1;
    if ( 0.0 <= ratio )
      fprintf ( 1, 'opt02(): Convergence after %d iterations\n', it );
      break;
    end

  end

  return
end
function gval = g ( tval, xval, t, x, u, r, M )

%*****************************************************************************80
%
%% g() evaluates the right hand side of the state equation.
%
%  Modified:
%
%    05 October 2022
%
%  Author:
%
%    John Burkardt
%
  uval = interp1 ( t, u, tval );
  gval = r * ( M - xval ) - uval .* xval;

  return
end
function hval = h ( tval, lval, tb, xb, ub, lb, r, A )

%*****************************************************************************80
%
%% h() evaluates the right hand side of the adjoint equation.
%
%  Modified:
%
%    05 October 2022
%
%  Author:
%
%    John Burkardt
%
  xval = interp1 ( tb, xb, tval );
  uval = interp1 ( tb, ub, tval );
  hval = - 2.0 * A * xval + r * lval + uval * lval;

  return
end

