function [ t, x, u, l, cost ] = opt11 ( x0, k, r, T, N )

%*****************************************************************************80
%
%% opt11() performs optimal control for case #11.
%
%  Discussion:
%
%    Timber Harvesting
%
%    maximize
%      integral ( 0 <= t <= T ) exp ( -r t ) x(t) ( 1 - u(t) ) dt
%    subject to
%      x'(t) = k x(t) u(t)
%      x(0) = x0
%    with Hamiltonian:
%      H = exp ( -r t ) x(t) ( 1 - u(t) ) + lambda(t) k x(t) u(t)
%    with adjoint equation:
%      lambda(t)' = - ( exp ( - r t ) ( 1 - u(t) ) + lambda(t) k u(t) )
%      lambda(T) = 0.0
%    and optimality condition:
%      x(t) ( k lambda(t) - exp ( - r t ) ) = 0
%    and
%      0 <= u(t) <= 1
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    09 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 x0: the initial value for the state variable.
%
%    real k: a coefficient in the state equation.
%
%    real r: the discount rate for future profits.
%
%    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 estimates.
%
%    real u(N+1): the optimal control estimate.
%
%    real l(N+1): 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,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, k );
%
%  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, k, r );
%
%  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.
%  The optimal control is bang-bang.
%
    test = x .* ( k * l - exp ( - r * t ) );
    i = find ( 0.0 < test )
    u = zeros ( N + 1, 1 );
    u(i) = 1.0;
    u = 0.5 * ( oldu + u );
%
%  Evaluate the cost.
%
    f = exp ( - r * t ) .* x .* ( 1.0 - u );
    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, 'opt11(): Convergence after %d iterations\n', it );
      break;
    end

  end

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

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

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

%*****************************************************************************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 = - exp ( - r * tval ) * ( 1.0 - uval ) - lval * k * uval;

  return
end

