function [ t, x, u, l, cost ] = opt06 ( A, k, m, x0, M, T, N )

%*****************************************************************************80
%
%% opt06() performs optimal control for case #06.
%
%  Discussion:
%
%    Fish Harvesting
%
%    maximize
%      integral ( 0 <= t <= T ) A k t / ( t + 1 ) x(t) u(t) - u(t)^2 dt
%    subject to
%      x'(t) = - ( m + u(t) ) x(t)
%      x(0) = x0
%    with Hamiltonian:
%      H = A k t / ( t + a ) x(t) u(t) - u(t)^2 - lambda(t) ( m + u(t) ) x(t)
%    with adjoint equation:
%      lambda(t)' = - A k t / ( t + 1 ) u(t) + lambda(t) ( m + u(t) )  
%      lambda(T) = 0.0
%    and optimality condition:
%      A k t / ( t + 1 ) x(t) - 2 u(t) - lambda(t) x(t) = 0
%    and
%      0 <= u(t) <= M
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  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 A: a weight parameter for the cost function.
%
%    real k: the maximum mass of this species.
%
%    real m: the natural death rate of the fish.
%
%    real x0: the initial condition for the state variable.
%
%    real M: an upper bound for the control.
%
%    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 state estimate.
%
%    real u(N+1): the optimal control estimate.
%
%    real l(N+1): the 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, 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, A, k, m );
%
%  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.
%
    u = - 0.5 * ( l .* x - A * k * t .* x ./ ( t + 1.0 ) );
    u = max ( u, 0.0 );
    u = min ( u, M );
    u = 0.5 * ( u + oldu );
%
%  Evaluate the cost.
%
    f = A * k * t ./ ( t + 1.0 ) .* x .* u - 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, 'opt06(): Convergence after %d iterations\n', it );
      break;
    end

  end

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

%*****************************************************************************80
%
%% g() evaluates the right hand side of the state equation.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    06 October 2022
%
%  Author:
%
%    John Burkardt
%
  uval = interp1 ( t, u, tval );
  gval = - ( m + uval ) * xval;

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

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

  return
end

