function [ t, x, u, l, cost ] = opt01 ( A, B, C, x0, T, N )

%*****************************************************************************80
%
%% opt01() performs optimal control for case #01.
%
%  Discussion:
%
%    Introductory Example
%
%    maximize:
%      integral ( 0 <= t <= 1 ) A x(t) - B u(t)^2 dt
%    subject to:
%      x'(t) = -0.5 x(t)^2 + C u(t)
%      x(0) = x0
%    with Hamiltonian:
%      H = A x(t) - B u(t)^2 + lambda(t) * ( -0.5 x(t)^2 + C u(t) )
%    with adjoint equation:
%      lambda'(t) = - A + lambda x(t)
%      lambda(1) = 0
%    and optimality condition:
%      - 2 B u* + C lambda* = 0
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    04 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, B: coefficients in the cost function.
%    A and B must be positive.
%
%    real C: a coefficient in the state equation.
%
%    real x0: the initial value for the state.
%    The value of x0 must be greater than -2.
%
%    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 );
  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.
%
    oldx = x;
    oldu = u;
    oldl = l;
%
%  Define the right hand side of the state equation.
%
    dxdt = @(tval,xval) g ( tval, xval, t, x, u, C );
%
%  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 );
%
%  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 = C * l / ( 2.0 * B );
    u = 0.5 * ( u1 + oldu );
%
%  Compute the current cost.
%
    f = A * x - B * 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, 'opt01(): Convergence after %d iterations\n', it );
      break;
    end

  end

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

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

  return
end
function hval = h ( tval, lval, tb, xb, ub, lb, 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 = - A + lval .* xval;

  return
end

