Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

## Purpose

nag_ode_ivp_adams_zero_simple (d02cj) integrates a system of first-order ordinary differential equations over a range with suitable initial conditions, using a variable-order, variable-step Adams method until a user-specified function, if supplied, of the solution is zero, and returns the solution at points specified by you, if desired.

## Syntax

[x, y, ifail] = d02cj(x, xend, y, fcn, tol, relabs, output, g, 'n', n)
[x, y, ifail] = nag_ode_ivp_adams_zero_simple(x, xend, y, fcn, tol, relabs, output, g, 'n', n)

## Description

nag_ode_ivp_adams_zero_simple (d02cj) advances the solution of a system of ordinary differential equations
 yi ′ = fi(x,y1,y2, … ,yn),  i = 1,2, … ,n, $yi′=fi(x,y1,y2,…,yn), i=1,2,…,n,$
from x = x$x={\mathbf{x}}$ to x = xend$x={\mathbf{xend}}$ using a variable-order, variable-step Adams method. The system is defined by fcn, which evaluates fi${f}_{i}$ in terms of x$x$ and y1,y2,,yn${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$. The initial values of y1,y2,,yn${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$ must be given at x = x$x={\mathbf{x}}$.
The solution is returned via output at points specified by you, if desired: this solution is obtained by C1${C}^{1}$ interpolation on solution values produced by the method. As the integration proceeds a check can be made on the user-specified function g(x,y)$g\left(x,y\right)$ to determine an interval where it changes sign. The position of this sign change is then determined accurately by C1${C}^{1}$ interpolation to the solution. It is assumed that g(x,y)$g\left(x,y\right)$ is a continuous function of the variables, so that a solution of g(x,y) = 0.0$g\left(x,y\right)=0.0$ can be determined by searching for a change in sign in g(x,y)$g\left(x,y\right)$. The accuracy of the integration, the interpolation and, indirectly, of the determination of the position where g(x,y) = 0.0$g\left(x,y\right)=0.0$, is controlled by the parameters tol and relabs.
For a description of Adams methods and their practical implementation see Hall and Watt (1976).

## References

Hall G and Watt J M (ed.) (1976) Modern Numerical Methods for Ordinary Differential Equations Clarendon Press, Oxford

## Parameters

### Compulsory Input Parameters

1:     x – double scalar
The initial value of the independent variable x$x$.
Constraint: ${\mathbf{x}}\ne {\mathbf{xend}}$.
2:     xend – double scalar
The final value of the independent variable. If ${\mathbf{xend}}<{\mathbf{x}}$, integration will proceed in the negative direction.
Constraint: ${\mathbf{xend}}\ne {\mathbf{x}}$.
3:     y(n) – double array
n, the dimension of the array, must satisfy the constraint n1${\mathbf{n}}\ge 1$.
The initial values of the solution y1,y2,,yn${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$ at x = x$x={\mathbf{x}}$.
4:     fcn – function handle or string containing name of m-file
fcn must evaluate the functions fi${f}_{i}$ (i.e., the derivatives yi${y}_{i}^{\prime }$) for given values of its arguments x,y1,,yn$x,{y}_{1},\dots ,{y}_{\mathit{n}}$.
[f] = fcn(x, y)

Input Parameters

1:     x – double scalar
x$x$, the value of the independent variable.
2:     y(n$\mathit{n}$) – double array
yi${y}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,\mathit{n}$, the value of the variable.

Output Parameters

1:     f(n$\mathit{n}$) – double array
The value of fi${f}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,\mathit{n}$.
5:     tol – double scalar
A positive tolerance for controlling the error in the integration. Hence tol affects the determination of the position where g(x,y) = 0.0$g\left(x,y\right)=0.0$, if g$g$ is supplied.
nag_ode_ivp_adams_zero_simple (d02cj) has been designed so that, for most problems, a reduction in tol leads to an approximately proportional reduction in the error in the solution. However, the actual relation between tol and the accuracy achieved cannot be guaranteed. You are strongly recommended to call nag_ode_ivp_adams_zero_simple (d02cj) with more than one value for tol and to compare the results obtained to estimate their accuracy. In the absence of any prior knowledge, you might compare the results obtained by calling nag_ode_ivp_adams_zero_simple (d02cj) with tol = 10.0p${\mathbf{tol}}={10.0}^{-p}$ and tol = 10.0p1${\mathbf{tol}}={10.0}^{-p-1}$ where p$p$ correct decimal digits are required in the solution.
Constraint: tol > 0.0${\mathbf{tol}}>0.0$.
6:     relabs – string (length ≥ 1)
The type of error control. At each step in the numerical solution an estimate of the local error, est$\mathit{est}$, is made. For the current step to be accepted the following condition must be satisfied:
 est = sqrt( ∑ i = 1n(ei / (τr × |yi| + τa))2) ≤ 1.0 $est=∑i=1n(ei/(τr×|yi|+τa))2≤1.0$
where τr${\tau }_{r}$ and τa${\tau }_{a}$ are defined by
 relabs τr${\tau }_{r}$ τa${\tau }_{a}$ 'M' tol tol 'A' 0.0$0.0$ tol 'R' tol ε$\epsilon$ 'D' tol tol
where ε$\epsilon$ is a small machine-dependent number and ei${e}_{i}$ is an estimate of the local error at yi${y}_{i}$, computed internally. If the appropriate condition is not satisfied, the step size is reduced and the solution is recomputed on the current step. If you wish to measure the error in the computed solution in terms of the number of correct decimal places, then relabs should be set to 'A' on entry, whereas if the error requirement is in terms of the number of correct significant digits, then relabs should be set to 'R'. If you prefer a mixed error test, then relabs should be set to 'M', otherwise if you have no preference, relabs should be set to the default 'D'. Note that in this case 'D' is taken to be 'M'.
Constraint: relabs = 'M'${\mathbf{relabs}}=\text{'M'}$, 'A'$\text{'A'}$, 'R'$\text{'R'}$ or 'D'$\text{'D'}$.
7:     output – function handle or string containing name of m-file
output permits access to intermediate values of the computed solution (for example to print or plot them), at successive user-specified points. It is initially called by nag_ode_ivp_adams_zero_simple (d02cj) with ${\mathbf{xsol}}={\mathbf{x}}$ (the initial value of x$x$). You must reset xsol to the next point (between the current xsol and xend) where output is to be called, and so on at each call to output. If, after a call to output, the reset point xsol is beyond xend, nag_ode_ivp_adams_zero_simple (d02cj) will integrate to xend with no further calls to output; if a call to output is required at the point ${\mathbf{xsol}}={\mathbf{xend}}$, then xsol must be given precisely the value xend.
[xsol] = output(xsol, y)

Input Parameters

1:     xsol – double scalar
The output value of the independent variable x$x$.
2:     y(n$\mathit{n}$) – double array
The computed solution at the point xsol.

Output Parameters

1:     xsol – double scalar
You must set xsol to the next value of x$x$ at which output is to be called.
If you do not wish to access intermediate output, the actual parameter output must be the string 'd02cjx'. (nag_ode_ivp_adams_zero_simple_dummy_output (d02cjx) is included in the NAG Toolbox.)
8:     g – function handle or string containing name of m-file
g must evaluate the function g(x,y)$g\left(x,y\right)$ for specified values x,y$x,y$. It specifies the function g$g$ for which the first position x$x$ where g(x,y) = 0$g\left(x,y\right)=0$ is to be found.
[result] = g(x, y)

Input Parameters

1:     x – double scalar
x$x$, the value of the independent variable.
2:     y(n$\mathit{n}$) – double array
yi${y}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,\mathit{n}$, the value of the variable.

Output Parameters

1:     result – double scalar
The result of the function.
If you do not require the root-finding option, the actual parameter g must be the string 'd02cjw'. (nag_ode_ivp_adams_zero_simple_dummy_g (d02cjw) is included in the NAG Toolbox.)

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the array y.
n$\mathit{n}$, the number of differential equations.
Constraint: n1${\mathbf{n}}\ge 1$.

w

### Output Parameters

1:     x – double scalar
If g$g$ is supplied by you, it contains the point where g(x,y) = 0.0$g\left(x,y\right)=0.0$, unless g(x,y)0.0$g\left(x,y\right)\ne 0.0$ anywhere on the range x to xend, in which case, x will contain xend. If g$g$ is not supplied by you it contains xend, unless an error has occurred, when it contains the value of x$x$ at the error.
2:     y(n) – double array
The computed values of the solution at the final point x = x$x={\mathbf{x}}$.
3:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
 On entry, tol ≤ 0.0${\mathbf{tol}}\le 0.0$, or n ≤ 0${\mathbf{n}}\le 0$, or relabs ≠ 'M'${\mathbf{relabs}}\ne \text{'M'}$, 'A'$\text{'A'}$, 'R'$\text{'R'}$ or 'D'$\text{'D'}$, or ${\mathbf{x}}={\mathbf{xend}}$.
ifail = 2${\mathbf{ifail}}=2$
With the given value of tol, no further progress can be made across the integration range from the current point x = x$x={\mathbf{x}}$. (See Section [Further Comments] for a discussion of this error exit.) The components y(1),y(2),,y(n)${\mathbf{y}}\left(1\right),{\mathbf{y}}\left(2\right),\dots ,{\mathbf{y}}\left({\mathbf{n}}\right)$ contain the computed values of the solution at the current point x = x$x={\mathbf{x}}$. If you have supplied g$g$, then no point at which g(x,y)$g\left(x,y\right)$ changes sign has been located up to the point x = x$x={\mathbf{x}}$.
ifail = 3${\mathbf{ifail}}=3$
tol is too small for nag_ode_ivp_adams_zero_simple (d02cj) to take an initial step. x and y(1),y(2),,y(n)${\mathbf{y}}\left(1\right),{\mathbf{y}}\left(2\right),\dots ,{\mathbf{y}}\left({\mathbf{n}}\right)$ retain their initial values.
ifail = 4${\mathbf{ifail}}=4$
xsol has not been reset or xsol lies behind x in the direction of integration, after the initial call to output, if the output option was selected.
ifail = 5${\mathbf{ifail}}=5$
A value of xsol returned by the output has not been reset or lies behind the last value of xsol in the direction of integration, if the output option was selected.
ifail = 6${\mathbf{ifail}}=6$
At no point in the range x to xend did the function g(x,y)$g\left(x,y\right)$ change sign, if g$g$ was supplied. It is assumed that g(x,y) = 0$g\left(x,y\right)=0$ has no solution.
ifail = 7${\mathbf{ifail}}=7$
A serious error has occurred in an internal call. Check all function calls and array sizes. Seek expert help.

## Accuracy

The accuracy of the computation of the solution vector y may be controlled by varying the local error tolerance tol. In general, a decrease in local error tolerance should lead to an increase in accuracy. You are advised to choose relabs = 'M'${\mathbf{relabs}}=\text{'M'}$ unless you have a good reason for a different choice.
If the problem is a root-finding one, then the accuracy of the root determined will depend on the properties of g(x,y)$g\left(x,y\right)$. You should try to code g without introducing any unnecessary cancellation errors.

If more than one root is required then nag_ode_ivp_adams_roots (d02qf) should be used.
If nag_ode_ivp_adams_zero_simple (d02cj) fails with ${\mathbf{ifail}}={\mathbf{3}}$, then it can be called again with a larger value of tol if this has not already been tried. If the accuracy requested is really needed and cannot be obtained with this function, the system may be very stiff (see below) or so badly scaled that it cannot be solved to the required accuracy.
If nag_ode_ivp_adams_zero_simple (d02cj) fails with ${\mathbf{ifail}}={\mathbf{2}}$, it is probable that it has been called with a value of tol which is so small that a solution cannot be obtained on the range x to xend. This can happen for well-behaved systems and very small values of tol. You should, however, consider whether there is a more fundamental difficulty. For example:
 (a) in the region of a singularity (infinite value) of the solution, the function will usually stop with ${\mathbf{ifail}}={\mathbf{2}}$, unless overflow occurs first. Numerical integration cannot be continued through a singularity, and analytic treatment should be considered; (b) for ‘stiff’ equations where the solution contains rapidly decaying components, the function will use very small steps in x$x$ (internally to nag_ode_ivp_adams_zero_simple (d02cj)) to preserve stability. This will exhibit itself by making the computing time excessively long, or occasionally by an exit with ${\mathbf{ifail}}={\mathbf{2}}$. Adams methods are not efficient in such cases, and you should try nag_ode_ivp_bdf_zero_simple (d02ej).

## Example

This example illustrates the solution of four different problems. In each case the differential system (for a projectile) is
 y′ = tanφ v′ = ( − 0.032tanφ)/v − (0.02v)/(cosφ) φ′ = ( − 0.032)/(v2)
$y′ = tan⁡ϕ v′ = -0.032tan⁡ϕv-0.02v cos⁡ϕ ϕ′ = -0.032v2$
over an interval x = 0.0${\mathbf{x}}=0.0$ to xend = 10.0${\mathbf{xend}}=10.0$ starting with values y = 0.5$y=0.5$, v = 0.5$v=0.5$ and φ = π / 5$\varphi =\pi /5$. We solve each of the following problems with local error tolerances 1.0e−4$\text{1.0e−4}$ and 1.0e−5$\text{1.0e−5}$.
 (i) To integrate to x = 10.0$x=10.0$ producing output at intervals of 2.0$2.0$ until a point is encountered where y = 0.0$y=0.0$. (ii) As (i) but with no intermediate output. (iii) As (i) but with no termination on a root-finding condition. (iv) As (i) but with no intermediate output and no root-finding termination condition.
```function nag_ode_ivp_adams_zero_simple_example
% For communication with save.
global ykeep ncall xkeep;

% Initialize variables and arrays.
x = 0;
xend = 10;
y = [0.5; 0.5; 0.6283185307179586];
tol = 0.0001;
relabs = 'Default';

ncall = 0;
ykeep = zeros(1,length(y));
xkeep = zeros(1,1);

fprintf('Case 1: intermediate output, root-finding\n\n');
for j = 4:5
tol = double(10)^(-j);
disp(['Calculation with tol = ',num2str(tol)]);
disp(' X     Y(1)       Y(2)       Y(3)');

% output outputs intermediate results.
[xOut, yOut, ifail] = nag_ode_ivp_adams_zero_simple(x, xend, y, @fcn, tol, ...
relabs, @output, @g);
if ifail ~= 0
% Problems in integration.  Print message and exit.
error('Warning: nag_ode_ivp_adams_zero_simple returned with ifail = %1d ',ifail);
end

disp(' ');
disp(['Root of Y(1) = 0.0 at ',num2str(xOut)]);
disp('Solution is' );
fprintf('  %8.4f   %8.4f   %8.4f\n\n', yOut(1), yOut(2), yOut(3));
end

fprintf('Case 2: no intermediate output, root-finding\n\n');
for j = 4:5
tol = double(10)^(-j);
disp(['Calculation with tol = ',num2str(tol)]);

[xOut, yOut, ifail] = nag_ode_ivp_adams_zero_simple(x, xend, y, @fcn, tol, ...
if ifail ~= 0
% Problems in integration.  Print message and exit.
error('Warning: nag_ode_ivp_adams_zero_simple returned with ifail = %1d ',ifail);
end

disp(['Root of Y(1) = 0.0 at ',num2str(xOut)]);
disp('Solution is' );
fprintf('  %8.4f   %8.4f   %8.4f\n\n', yOut(1), yOut(2), yOut(3));

% Store the x value for plotting.
xOut1 = xOut;
end

fprintf('Case 3: intermediate output, no root-finding\n\n');
for j = 4:5
tol = double(10)^(-j);
disp(['Calculation with tol = ',num2str(tol)]);
disp(' X     Y(1)       Y(2)       Y(3)');

% save stores intermediate values in xkeep, ykeep, which are
% plotted later (it also outputs them).
[xOut, yOut, ifail] = nag_ode_ivp_adams_zero_simple(x, xend, y, @fcn, tol, ...
if ifail ~= 0
% Problems in integration.  Print message and exit.
error('Warning: nag_ode_ivp_adams_zero_simple returned with ifail = %1d ',ifail);
end
fprintf('\n');
end

fprintf(['Case 4: no intermediate output, no root-finding ', ...
'(integrate to xend)\n\n']);
for j = 4:5
tol = double(10)^(-j);
disp(['Calculation with tol = ',num2str(tol)]);
disp(' X     Y(1)       Y(2)       Y(3)');
fprintf('%2d   %8.4f   %8.4f   %8.4f\n', x, y(1), y(2), y(3));

[xOut, yOut, ifail] = nag_ode_ivp_adams_zero_simple(x, xend, y, @fcn, tol, ...
if ifail ~= 0
% Problems in integration.  Print message and exit.
error('Warning: nag_ode_ivp_adams_zero_simple returned with ifail = %1d ',ifail);
end
fprintf('\n');
end
% Plot results.
nres = 0.5*length(xkeep);
xplot = xkeep(nres+1:2*nres);
yplot = ykeep(nres+1:2*nres, :);
fig = figure('Number', 'off');
display_plot(xplot, yplot, xOut1)

function xsolOut = save(xsol, y)
% For communication with main routine.
global ykeep ncall xkeep;

% This version of the intermediate output routine stores the values
% (so they can be plotted in the main routine).
ncall = ncall+1;
ykeep(ncall,:) = y;
xkeep(ncall,:) = xsol;
fprintf('%2d   %8.4f   %8.4f   %8.4f\n', xsol, y(1), y(2), y(3));
xsolOut = xsol + 2;
function xsolOut = output(xsol, y)
% Output intermediate values of solution.
fprintf('%2d   %8.4f   %8.4f   %8.4f\n', xsol, y(1), y(2), y(3));
xsolOut = xsol + 2;
function f = fcn(x,y)
% Evaluate the derivatives.
f = zeros(3,1);
f(1) = tan(y(3));
f(2) = -0.032*tan(y(3))/y(2) - 0.02*y(2)/cos(y(3));
f(3) = -0.032/y(2)^2;
function result = g(x,y)
% Evaluate g(x,y) when root-finding option is selected.
result = y(1);
function display_plot(xplot, yplot, xOut1)
% Formatting for title and axis labels.
titleFmt = {'FontName', 'Helvetica', 'FontWeight', 'Bold', 'FontSize', 14};
labFmt = {'FontName', 'Helvetica', 'FontWeight', 'Bold', 'FontSize', 13};
set(gca, 'FontSize', 13); % for legend, axis tick labels, etc.
% Plot the three curves.
plot(xplot, yplot(:,1), '-+', ...
xplot, yplot(:,2), '--x', ...
xplot, yplot(:,3), ':*');
% Mark the height=0 point.
do_stem(xplot, yplot, xOut1);
title('ODE Solution using Adams Method with Root-finding', titleFmt{:});
% Label the axes.
xlabel('x', labFmt{:});
ylabel('Solution', labFmt{:});
legend('height','velocity','angle','height = 0','Location','Best');
function do_stem(xplot, yplot, xOut1)
% Find the x bin that xOut1 lies in.
for i = 1:length(xplot)
if xplot(i) > xOut1
break
end
end
% Use linear interpolation to find the corresponding y values on the
% two curves.
a1 = xplot(i)-xplot(i-1);
b1 = yplot(i,2)-yplot(i-1,2);
c1 = xOut1-xplot(i-1);
d1 = c1/a1;
e1 = d1*b1;
f1 = e1+yplot(i-1,2);
a2 = xplot(i)-xplot(i-1);
b2 = yplot(i,3)-yplot(i-1,3);
c2 = xOut1-xplot(i-1);
d2 = c2/a2;
e2 = d2*b2;
f2 = e2+yplot(i-1,3);
% Plot the line from the x axis to the two y values.
hold on
stem([xOut1,xOut1],[f1,0],'k:s');
hold on
stem([xOut1,xOut1],[f2,0],'k:s');
```
```

Case 1: intermediate output, root-finding

Calculation with tol = 0.0001
X     Y(1)       Y(2)       Y(3)
0     0.5000     0.5000     0.6283
2     1.5493     0.4055     0.3066
4     1.7423     0.3743    -0.1289
6     1.0055     0.4173    -0.5507

Root of Y(1) = 0.0 at 7.2883
Solution is
-0.0000     0.4749    -0.7601

Calculation with tol = 1e-05
X     Y(1)       Y(2)       Y(3)
0     0.5000     0.5000     0.6283
2     1.5493     0.4055     0.3066
4     1.7423     0.3743    -0.1289
6     1.0055     0.4173    -0.5507

Root of Y(1) = 0.0 at 7.2882
Solution is
-0.0000     0.4749    -0.7601

Case 2: no intermediate output, root-finding

Calculation with tol = 0.0001
Root of Y(1) = 0.0 at 7.2883
Solution is
-0.0000     0.4749    -0.7601

Calculation with tol = 1e-05
Root of Y(1) = 0.0 at 7.2882
Solution is
-0.0000     0.4749    -0.7601

Case 3: intermediate output, no root-finding

Calculation with tol = 0.0001
X     Y(1)       Y(2)       Y(3)
0     0.5000     0.5000     0.6283
2     1.5493     0.4055     0.3066
4     1.7423     0.3743    -0.1289
6     1.0055     0.4173    -0.5507
8    -0.7459     0.5130    -0.8537
10    -3.6281     0.6333    -1.0515

Calculation with tol = 1e-05
X     Y(1)       Y(2)       Y(3)
0     0.5000     0.5000     0.6283
2     1.5493     0.4055     0.3066
4     1.7423     0.3743    -0.1289
6     1.0055     0.4173    -0.5507
8    -0.7460     0.5130    -0.8537
10    -3.6283     0.6333    -1.0515

Case 4: no intermediate output, no root-finding (integrate to xend)

Calculation with tol = 0.0001
X     Y(1)       Y(2)       Y(3)
0     0.5000     0.5000     0.6283

Calculation with tol = 1e-05
X     Y(1)       Y(2)       Y(3)
0     0.5000     0.5000     0.6283

```
```function d02cj_example
% For communication with save.
global ykeep ncall xkeep;

% Initialize variables and arrays.
x = 0;
xend = 10;
y = [0.5; 0.5; 0.6283185307179586];
tol = 0.0001;
relabs = 'Default';

ncall = 0;
ykeep = zeros(1,length(y));
xkeep = zeros(1,1);

fprintf('d02cj example program results\n\n');

fprintf('Case 1: intermediate output, root-finding\n\n');
for j = 4:5
tol = double(10)^(-j);
disp(['Calculation with tol = ',num2str(tol)]);
disp(' X     Y(1)       Y(2)       Y(3)');

% output outputs intermediate results.
[xOut, yOut, ifail] = d02cj(x, xend, y, @fcn, tol, ...
relabs, @output, @g);
if ifail ~= 0
% Problems in integration.  Print message and exit.
error('Warning: d02cj returned with ifail = %1d',ifail);
end

disp(' ');
disp(['Root of Y(1) = 0.0 at ',num2str(xOut)]);
disp('Solution is' );
fprintf('  %8.4f   %8.4f   %8.4f\n\n', yOut(1), yOut(2), yOut(3));
end

fprintf('Case 2: no intermediate output, root-finding\n\n');
for j = 4:5
tol = double(10)^(-j);
disp(['Calculation with tol = ',num2str(tol)]);

[xOut, yOut, ifail] = d02cj(x, xend, y, @fcn, tol, ...
relabs, 'd02cjx', @g);
if ifail ~= 0
% Problems in integration.  Print message and exit.
error('Warning: d02cj returned with ifail = %1d',ifail);
end

disp(['Root of Y(1) = 0.0 at ',num2str(xOut)]);
disp('Solution is' );
fprintf('  %8.4f   %8.4f   %8.4f\n\n', yOut(1), yOut(2), yOut(3));

% Store the x value for plotting.
xOut1 = xOut;
end

fprintf('Case 3: intermediate output, no root-finding\n\n');
for j = 4:5
tol = double(10)^(-j);
disp(['Calculation with tol = ',num2str(tol)]);
disp(' X     Y(1)       Y(2)       Y(3)');

% save stores intermediate values in xkeep, ykeep, which are
% plotted later (it also outputs them).
[xOut, yOut, ifail] = d02cj(x, xend, y, @fcn, tol, ...
relabs, @save, 'd02cjw');
if ifail ~= 0
% Problems in integration.  Print message and exit.
error('Warning: d02cj returned with ifail = %1d',ifail);
end
fprintf('\n');
end

fprintf(['Case 4: no intermediate output, no root-finding ', ...
'(integrate to xend)\n\n']);
for j = 4:5
tol = double(10)^(-j);
disp(['Calculation with tol = ',num2str(tol)]);
disp(' X     Y(1)       Y(2)       Y(3)');
fprintf('%2d   %8.4f   %8.4f   %8.4f\n', x, y(1), y(2), y(3));

[xOut, yOut, ifail] = d02cj(x, xend, y, @fcn, tol, ...
relabs, 'd02cjx','d02cjw');
if ifail ~= 0
% Problems in integration.  Print message and exit.
error('Warning: d02cj returned with ifail = %1d',ifail);
end
fprintf('\n');
end
% Plot results.
nres = 0.5*length(xkeep);
xplot = xkeep(nres+1:2*nres);
yplot = ykeep(nres+1:2*nres, :);
fig = figure('Number', 'off');
display_plot(xplot, yplot, xOut1)

function xsolOut = save(xsol, y)
% For communication with main routine.
global ykeep ncall xkeep;

% This version of the intermediate output routine stores the values
% (so they can be plotted in the main routine).
ncall = ncall+1;
ykeep(ncall,:) = y;
xkeep(ncall,:) = xsol;
fprintf('%2d   %8.4f   %8.4f   %8.4f\n', xsol, y(1), y(2), y(3));
xsolOut = xsol + 2;
function xsolOut = output(xsol, y)
% Output intermediate values of solution.
fprintf('%2d   %8.4f   %8.4f   %8.4f\n', xsol, y(1), y(2), y(3));
xsolOut = xsol + 2;
function f = fcn(x,y)
% Evaluate the derivatives.
f = zeros(3,1);
f(1) = tan(y(3));
f(2) = -0.032*tan(y(3))/y(2) - 0.02*y(2)/cos(y(3));
f(3) = -0.032/y(2)^2;
function result = g(x,y)
% Evaluate g(x,y) when root-finding option is selected.
result = y(1);
function display_plot(xplot, yplot, xOut1)
% Formatting for title and axis labels.
titleFmt = {'FontName', 'Helvetica', 'FontWeight', 'Bold', 'FontSize', 14};
labFmt = {'FontName', 'Helvetica', 'FontWeight', 'Bold', 'FontSize', 13};
set(gca, 'FontSize', 13); % for legend, axis tick labels, etc.
% Plot the three curves.
plot(xplot, yplot(:,1), '-+', ...
xplot, yplot(:,2), '--x', ...
xplot, yplot(:,3), ':*');
% Mark the height=0 point.
do_stem(xplot, yplot, xOut1);
title('ODE Solution using Adams Method with Root-finding', titleFmt{:});
% Label the axes.
xlabel('x', labFmt{:});
ylabel('Solution', labFmt{:});
legend('height','velocity','angle','height = 0','Location','Best');
function do_stem(xplot, yplot, xOut1)
% Find the x bin that xOut1 lies in.
for i = 1:length(xplot)
if xplot(i) > xOut1
break
end
end
% Use linear interpolation to find the corresponding y values on the
% two curves.
a1 = xplot(i)-xplot(i-1);
b1 = yplot(i,2)-yplot(i-1,2);
c1 = xOut1-xplot(i-1);
d1 = c1/a1;
e1 = d1*b1;
f1 = e1+yplot(i-1,2);
a2 = xplot(i)-xplot(i-1);
b2 = yplot(i,3)-yplot(i-1,3);
c2 = xOut1-xplot(i-1);
d2 = c2/a2;
e2 = d2*b2;
f2 = e2+yplot(i-1,3);
% Plot the line from the x axis to the two y values.
hold on
stem([xOut1,xOut1],[f1,0],'k:s');
hold on
stem([xOut1,xOut1],[f2,0],'k:s');
```
```
d02cj example program results

Case 1: intermediate output, root-finding

Calculation with tol = 0.0001
X     Y(1)       Y(2)       Y(3)
0     0.5000     0.5000     0.6283
2     1.5493     0.4055     0.3066
4     1.7423     0.3743    -0.1289
6     1.0055     0.4173    -0.5507

Root of Y(1) = 0.0 at 7.2883
Solution is
-0.0000     0.4749    -0.7601

Calculation with tol = 1e-05
X     Y(1)       Y(2)       Y(3)
0     0.5000     0.5000     0.6283
2     1.5493     0.4055     0.3066
4     1.7423     0.3743    -0.1289
6     1.0055     0.4173    -0.5507

Root of Y(1) = 0.0 at 7.2882
Solution is
-0.0000     0.4749    -0.7601

Case 2: no intermediate output, root-finding

Calculation with tol = 0.0001
Root of Y(1) = 0.0 at 7.2883
Solution is
-0.0000     0.4749    -0.7601

Calculation with tol = 1e-05
Root of Y(1) = 0.0 at 7.2882
Solution is
-0.0000     0.4749    -0.7601

Case 3: intermediate output, no root-finding

Calculation with tol = 0.0001
X     Y(1)       Y(2)       Y(3)
0     0.5000     0.5000     0.6283
2     1.5493     0.4055     0.3066
4     1.7423     0.3743    -0.1289
6     1.0055     0.4173    -0.5507
8    -0.7459     0.5130    -0.8537
10    -3.6281     0.6333    -1.0515

Calculation with tol = 1e-05
X     Y(1)       Y(2)       Y(3)
0     0.5000     0.5000     0.6283
2     1.5493     0.4055     0.3066
4     1.7423     0.3743    -0.1289
6     1.0055     0.4173    -0.5507
8    -0.7460     0.5130    -0.8537
10    -3.6283     0.6333    -1.0515

Case 4: no intermediate output, no root-finding (integrate to xend)

Calculation with tol = 0.0001
X     Y(1)       Y(2)       Y(3)
0     0.5000     0.5000     0.6283

Calculation with tol = 1e-05
X     Y(1)       Y(2)       Y(3)
0     0.5000     0.5000     0.6283

```

Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013