function [ t, x, u, l, cost ] = opt12 ( K, G, D, x0, M, T, N )

%*****************************************************************************80
%
%% opt12() performs optimal control for case #12.
%
%  Discussion:
%
%    Bioreactor
%
%    maximize
%      integral ( 0 <= t <= 1 ) K x(t) - u(t) dt
%    subject to
%      x'(t) = G u(t) x(t) - D x(t)^2
%      x(0) = x0
%      z'(t) = - K z(t) x(t)
%      z(0) = z0
%    with Hamiltonian:
%      H = K x(t) - u(t) 
%        + lambda1(t) ( G u(t) x(t) - D x(t)^2 )
%        + lambda2(t) ( -K z(t) x(t) )
%    with adjoint equations:
%      lambda1(t)' = - K + lambda1(t) ( G u(t) - 2 D x(t) ) 
%                    + lambda2(t) K z(t)
%      lambda1(1) = 0.0
%      lambda2(t)' = lambda2(t) * K x(t)
%      lambda2(1) = 0.0
%    and optimality condition:
%      -1 + lambda1(t) G x(t)
%    and
%      0 <= u(t) <= M
%
%  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 K: the rate of degradation.
%
%    real G, D: the maximum growth rate, and the natural death rate.
%
%    real x0[2]: the initial values for the state variables.
%
%    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,2): the state estimates.
%
%    real u(N+1): the optimal control estimate.
%
%    real l(N+1,2): 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,2);
  x(1,1:2) = x0;
  u = zeros(N+1,1);
  l = zeros(N+1,2);
  l1 = [ 0.0, 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, D, G, K );
%
%  Solve state equation forward in time.
%
    [ t, x ] = ode45 ( dxdt, t, x0 );
%
%  Reverse time for adjoint equation.
%
    xb = x(end:-1:1,1:2);
    ub = u(end:-1:1);
    lb = l(end:-1:1,1:2);
%
%  Define the right hand side of the adjoint equation.
%
    dldt = @(tval,lval) h ( tval, lval, tb, xb, ub, lb, D, G, K );
%
%  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:2);
%
%  Update the control.
%  Use bang-bang control.
%
    test = G * x(:,1) .* l(:,1) - 1.0;
    i = find ( 0.0 <= test )
    u = zeros ( N + 1, 1 );
    u(i) = M;
    u = 0.5 * ( oldu + u );
%
%  Evaluate the cost.
%
    f = K * x(:,1) - u;
    fint = trapz ( t, f );
    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, l(:,1), oldl(:,1) );
    ratio = convergence ( ratio, l(:,2), oldl(:,2) );
    it = it + 1;
    if ( 0.0 <= ratio )
      fprintf ( 1, 'opt12(): Convergence after %d iterations\n', it );
      break;
    end

  end

  return
end
function gval = g ( tval, xval, t, x, u, D, G, K )

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

  gval(1) = G * uval * x1val - D * x1val^2;
  gval(2) = - K * x2val * x1val;

  return
end
function hval = h ( tval, lval, tb, xb, ub, lb, D, G, K )

%*****************************************************************************80
%
%% h() evaluates the right hand side of the adjoint equation.
%
%  Modified:
%
%    10 October 2022
%
%  Author:
%
%    John Burkardt
%
  x1val = interp1 ( tb, xb(:,1), tval );
  x2val = interp1 ( tb, xb(:,2), tval );
  uval = interp1 ( tb, ub, tval );
  l1val = lval(1);
  l2val = lval(2);
  hval = zeros ( 2, 1 );

  hval(1) = - K - l1val * ( G * uval - 2.0 * D * x1val ) ...
                + l2val * K * x2val;
  hval(2) = l2val * K * x1val;

  return
end

