% This program uses the method of adjoints to solve the example in
% section 7.7
%
% boundary conditions:
%
x1tf = 1;
x10 = 0;
% integrate the adjoint system m times, which is 1 in this case.
% notice that this is backward integration; the independent variable
% t = 1-tt; diff. eqs. described in 'model77a' are in terms of tt.
%
% also notice that the integral term in Eq.(7.7.28) is also calculated
% by solving the diff. eq. (7.7.29), so that p2 doesn't have to be saved
% for numerical integration or integrating with the state eqs.
%
% the initial conditions, which are actually final conditions of the
% adjoint variables, p, are not arbitrary. e.g., p0=[0 1 0]' will give
% wrong answer.
%
p0 = [1 0 0]';
[tt, p] = ode45n('model77a',[ 0 1.0], p0);
LL = length(tt);
p10 = p(LL, 1);
p20 = p(LL, 2);
integral = p(LL, 3);
tt = 1 - tt;
plot(tt, p(:, 1), '-', tt, p(:,2), '--');
title('Adjoint Variable Profiles');
xlabel('Time (t)');
ylabel('Adjoint Variables');
text(0.6, 1.2, 'p1');
text(0.6, 0.5, 'p2');
print fig713.eps;
pause;
x20 = 1/p20*(x1tf - p10*x10 - integral);
x0 = [x10 x20]';
[tx, x] = ode45n('model77',[ 0 1.0], x0);
plot(tx, x);
title('State Variable Profiles');
xlabel('Time (t)');
ylabel('State Variables');
text(0.6, 1.0, 'x1');
text(0.6, 0.4, 'x2');
print fig714.eps;