function [ t, x, u, l, cost ] = opt13 ( theta, A, d1, d2, M, x0, T, N )

%*****************************************************************************80
%
%% opt13() performs optimal control for case #13.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    10 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 theta: ?.
%
%    real A: a weight coefficient for the cost function.
%
%    real d1, d2: death rate coefficients.
%
%    real M: the pesticide application level.
%
%    real x0(3): the initial values for the state variables.
%
%    real T: the final time.
%
%    integer N: the number of time steps.
%
%  Output:
%
%    real t(N+1): the time values.
%
%    real x(N+1,3), the state estimates.
%
%    real u(N+1): the optimal control estimate.
%
%    real l(N+1,3): the adjoint estimates.
%
%    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,3);
  x(1,1:3) = x0(1:3);
  u = zeros(N+1,1);
  l = zeros(N+1,3);
  l(:,3) = theta;
  l1 = [ 1.0, 0.0, theta ];
  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, d1, d2 );
%
%  Solve state equation forward in time.
%
    [ t, x ] = ode45 ( dxdt, t, x0 );
%
%  Reverse time for adjoint equation.
%
    xb = x(end:-1:1,1:3);
    ub = u(end:-1:1);
    lb = l(end:-1:1,1:3);
%
%  Define the right hand side of the adjoint equation.
%
    dldt = @(tval,lval) h ( tval, lval, tb, xb, ub, lb, d1, d2 );
%
%  Solve the adjoint equation "in the wrong time direction".
%
    [ tb, lb ] = ode45 ( dldt, tb, l1 );
%
%  Re-reverse adjoint variable.
%
    l = lb(end:-1:1,1:3);
%
%  Update the control.
%  
    u = ( d1 * x(:,1) .* l(:,1) + d2 * x(:,2) .* l(:,2) - l(:,3) ) / A;
    u = max ( u, 0.0 );
    u = min ( u, M );
    u = 0.5 * ( u + oldu );
%
%  Evaluate the cost.
%
    f = 0.5 * A * u.^2;
    fint = trapz ( t, f ) + x(N+1,1);
    cost = [ cost, fint ];
%
%  Convergence test.
%
    ratio = Inf;
    ratio = convergence ( ratio, u, oldu );
    ratio = convergence ( ratio, x(:,1), oldx(:,1) );
    ratio = convergence ( ratio, x(:,2), oldx(:,2) );
    ratio = convergence ( ratio, x(:,3), oldx(:,3) );
    ratio = convergence ( ratio, l(:,1), oldl(:,1) );
    ratio = convergence ( ratio, l(:,2), oldl(:,2) );
    ratio = convergence ( ratio, l(:,3), oldl(:,3) );
    it = it + 1;
    if ( 0.0 <= ratio )
      fprintf ( 1, 'opt13(): Convergence after %d iterations\n', it );
      break;
    end

  end

  return
end
function gval = g ( tval, xval, t, x, u, d1, d2 )

%*****************************************************************************80
%
%% g() evaluates the right hand side of the state equation.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    10 October 2022
%
%  Author:
%
%    John Burkardt
%
  x1val = xval(1);
  x2val = xval(2);
  x3val = xval(3);
  uval = interp1 ( t, u, tval );
  gval = zeros ( 3, 1 );

  gval(1) = ( 1.0 - x2val - d1 * uval ) * x1val;
  gval(2) = ( x1val - 1.0 - d2 * uval ) * x2val;
  gval(3) = uval;

  return
end
function hval = h ( tval, lval, tb, xb, ub, lb, d1, d2 )

%*****************************************************************************80
%
%% h() evaluates the right hand side of the adjoint equation.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    10 October 2022
%
%  Author:
%
%    John Burkardt
%
  x1val = interp1 ( tb, xb(:,1), tval );
  x2val = interp1 ( tb, xb(:,2), tval );
  x3val = interp1 ( tb, xb(:,3), tval );
  uval = interp1 ( tb, ub, tval );
  l1val = lval(1);
  l2val = lval(2);
  l3val = lval(3);
  hval = zeros ( 3, 1 );

  hval(1) = - l1val * ( 1.0 - x2val - d1 * uval ) - l2val * x2val;
  hval(2) = + l1val * x1val - l2val * ( x1val - 1.0 - d2 * uval );
  hval(3) = 0.0;

  return
end
