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

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_ode_ivp_rkts_onestep (d02pf)

## Purpose

nag_ode_ivp_rkts_onestep (d02pf) is a one-step function for solving an initial value problem for a first-order system of ordinary differential equations using Runge–Kutta methods.

## Syntax

[tnow, ynow, ypnow, user, iwsav, rwsav, ifail] = d02pf(f, n, iwsav, rwsav, 'user', user)
[tnow, ynow, ypnow, user, iwsav, rwsav, ifail] = nag_ode_ivp_rkts_onestep(f, n, iwsav, rwsav, 'user', user)

## Description

nag_ode_ivp_rkts_onestep (d02pf) and its associated functions (nag_ode_ivp_rkts_setup (d02pq), nag_ode_ivp_rkts_reset_tend (d02pr), nag_ode_ivp_rkts_interp (d02ps), nag_ode_ivp_rkts_diag (d02pt) and nag_ode_ivp_rkts_errass (d02pu)) solve an initial value problem for a first-order system of ordinary differential equations. The functions, based on Runge–Kutta methods and derived from RKSUITE (see Brankin et al. (1991)), integrate
 y′ = f(t,y)  given  y(t0) = y0 $y′=f(t,y) given y(t0)=y0$
where y$y$ is the vector of n$\mathit{n}$ solution components and t$t$ is the independent variable.
nag_ode_ivp_rkts_onestep (d02pf) is designed to be used in complicated tasks when solving systems of ordinary differential equations. You must first call nag_ode_ivp_rkts_setup (d02pq) to specify the problem and how it is to be solved. Thereafter you (repeatedly) call nag_ode_ivp_rkts_onestep (d02pf) to take one integration step at a time from tstart in the direction of tend (as specified in nag_ode_ivp_rkts_setup (d02pq)). In this manner nag_ode_ivp_rkts_onestep (d02pf) returns an approximation to the solution ynow and its derivative ypnow at successive points tnow. If nag_ode_ivp_rkts_onestep (d02pf) encounters some difficulty in taking a step, the integration is not advanced and the function returns with the same values of tnow, ynow and ypnow as returned on the previous successful step. nag_ode_ivp_rkts_onestep (d02pf) tries to advance the integration as far as possible subject to passing the test on the local error and not going past tend.
In the call to nag_ode_ivp_rkts_setup (d02pq) you can specify either the first step size for nag_ode_ivp_rkts_onestep (d02pf) to attempt or that it computes automatically an appropriate value. Thereafter nag_ode_ivp_rkts_onestep (d02pf) estimates an appropriate step size for its next step. This value and other details of the integration can be obtained after any call to nag_ode_ivp_rkts_onestep (d02pf) by a call to nag_ode_ivp_rkts_diag (d02pt). The local error is controlled at every step as specified in nag_ode_ivp_rkts_setup (d02pq). If you wish to assess the true error, you must set method to a positive value in the call to nag_ode_ivp_rkts_setup (d02pq). This assessment can be obtained after any call to nag_ode_ivp_rkts_onestep (d02pf) by a call to nag_ode_ivp_rkts_errass (d02pu).
If you want answers at specific points there are two ways to proceed:
 (i) The more efficient way is to step past the point where a solution is desired, and then call nag_ode_ivp_rkts_interp (d02ps) to get an answer there. Within the span of the current step, you can get all the answers you want at very little cost by repeated calls to nag_ode_ivp_rkts_interp (d02ps). This is very valuable when you want to find where something happens, e.g., where a particular solution component vanishes. You cannot proceed in this way with method = 3${\mathbf{method}}=3$ or -3$-3$. (ii) The other way to get an answer at a specific point is to set tend to this value and integrate to tend. nag_ode_ivp_rkts_onestep (d02pf) will not step past tend, so when a step would carry it past, it will reduce the step size so as to produce an answer at tend exactly. After getting an answer there (${\mathbf{tnow}}={\mathbf{tend}}$), you can reset tend to the next point where you want an answer, and repeat. tend could be reset by a call to nag_ode_ivp_rkts_setup (d02pq), but you should not do this. You should use nag_ode_ivp_rkts_reset_tend (d02pr) instead because it is both easier to use and much more efficient. This way of getting answers at specific points can be used with any of the available methods, but it is the only way with method = 3${\mathbf{method}}=3$ or -3$-3$. It can be inefficient. Should this be the case, the code will bring the matter to your attention.

## References

Brankin R W, Gladwell I and Shampine L F (1991) RKSUITE: A suite of Runge–Kutta codes for the initial value problems for ODEs SoftReport 91-S1 Southern Methodist University

## Parameters

### Compulsory Input Parameters

1:     f – function handle or string containing name of m-file
f must evaluate the functions fi${f}_{i}$ (that is the first derivatives yi${y}_{i}^{\prime }$) for given values of the arguments t$t$, yi${y}_{i}$.
[yp, user] = f(t, n, y, user)

Input Parameters

1:     t – double scalar
t$t$, the current value of the independent variable.
2:     n – int64int32nag_int scalar
n$\mathit{n}$, the number of ordinary differential equations in the system to be solved.
3:     y(n) – double array
The current values of the dependent variables, yi${y}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,\mathit{n}$.
4:     user – Any MATLAB object
f is called from nag_ode_ivp_rkts_onestep (d02pf) with the object supplied to nag_ode_ivp_rkts_onestep (d02pf).

Output Parameters

1:     yp(n) – double array
The values of fi${f}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,\mathit{n}$.
2:     user – Any MATLAB object
2:     n – int64int32nag_int scalar
n$n$, the number of ordinary differential equations in the system to be solved.
Constraint: n1${\mathbf{n}}\ge 1$.
3:     iwsav(130$130$) – int64int32nag_int array
4:     rwsav(32 × n + 350$32×{\mathbf{n}}+350$) – double array
These must be the same arrays supplied in a previous call to nag_ode_ivp_rkts_setup (d02pq). They must remain unchanged between calls.

### Optional Input Parameters

1:     user – Any MATLAB object
user is not used by nag_ode_ivp_rkts_onestep (d02pf), but is passed to f. Note that for large objects it may be more efficient to use a global variable which is accessible from the m-files than to use user.

iuser ruser

### Output Parameters

1:     tnow – double scalar
t$t$, the value of the independent variable at which a solution has been computed.
2:     ynow(n) – double array
An approximation to the solution at tnow. The local error of the step to tnow was no greater than permitted by the specified tolerances (see nag_ode_ivp_rkts_setup (d02pq)).
3:     ypnow(n) – double array
An approximation to the first derivative of the solution at tnow.
4:     user – Any MATLAB object
5:     iwsav(130$130$) – int64int32nag_int array
6:     rwsav(32 × n + 350$32×{\mathbf{n}}+350$) – double array
Information about the integration for use on subsequent calls to nag_ode_ivp_rkts_onestep (d02pf) or other associated functions.
7:     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:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

ifail = 1${\mathbf{ifail}}=1$
A call to this function cannot be made after it has returned an error.
The setup function must be called to start another problem.
On entry, n = _${\mathbf{n}}=_$, but the value passed to the setup function was .
On entry, the communication arrays have become corrupted, or a catastrophic error has already been detected elsewhere. You cannot continue integrating the problem.
tend, as specified in the setup function, has already been reached.
To start a new problem, you will need to call the setup function.
To continue integration beyond tend then nag_ode_ivp_rkts_reset_tend (d02pr) must first be called to reset tend to a new end value.
W ifail = 2${\mathbf{ifail}}=2$
More than 100$100$ output points have been obtained by integrating to tend (as specified in the setup function). They have been so clustered that it would probably be (much) more efficient to use the interpolation function (if |method| = 3$|{\mathbf{method}}|=3$, switch to |method| = 2$|{\mathbf{method}}|=2$ at setup).
However, you can continue integrating the problem.
W ifail = 3${\mathbf{ifail}}=3$
Approximately _$_$ function evaluations have been used to compute the solution since the integration started or since this message was last printed.
However, you can continue integrating the problem.
W ifail = 4${\mathbf{ifail}}=4$
Approximately _$_$ function evaluations have been used to compute the solution since the integration started or since this message was last printed. Your problem has been diagnosed as stiff. If the situation persists, it will cost roughly _$_$ times as much to reach tend (setup) as it has cost to reach the current time. You should probably call functions intended for stiff problems. However, you can continue integrating the problem.
W ifail = 5${\mathbf{ifail}}=5$
In order to satisfy your error requirements the solver has to use a step size of _$_$ at the current time, _$_$. This step size is too small for the machine precision, and is smaller than_$_$.
W ifail = 6${\mathbf{ifail}}=6$
The global error assessment algorithm failed at start of integration.
The integration is being terminated.
The global error assessment may not be reliable for times beyond _$_$.
The integration is being terminated.

## Accuracy

The accuracy of integration is determined by the parameters tol and thresh in a prior call to nag_ode_ivp_rkts_setup (d02pq) (see the function document for nag_ode_ivp_rkts_setup (d02pq) for further details and advice). Note that only the local error at each step is controlled by these parameters. The error estimates obtained are not strict bounds but are usually reliable over one step. Over a number of steps the overall error may accumulate in various ways, depending on the properties of the differential system.

If nag_ode_ivp_rkts_onestep (d02pf) returns with ${\mathbf{ifail}}={\mathbf{5}}$ and the accuracy specified by tol and thresh is really required then you should consider whether there is a more fundamental difficulty. For example, the solution may contain a singularity. In such a region the solution components will usually be large in magnitude. Successive output values of ynow should be monitored with the aim of trapping the solution before the singularity. In any case numerical integration cannot be continued through a singularity, and analytical treatment may be necessary.
Performance statistics are available after any return from nag_ode_ivp_rkts_onestep (d02pf) (except when ${\mathbf{ifail}}={\mathbf{1}}$) by a call to nag_ode_ivp_rkts_diag (d02pt). If method > 0${\mathbf{method}}>0$ in the call to nag_ode_ivp_rkts_setup (d02pq), global error assessment is available after any return from nag_ode_ivp_rkts_onestep (d02pf) (except when ${\mathbf{ifail}}={\mathbf{1}}$) by a call to nag_ode_ivp_rkts_errass (d02pu).
After a failure with ${\mathbf{ifail}}={\mathbf{5}}$ or 6${\mathbf{6}}$ each of the diagnostic functions nag_ode_ivp_rkts_diag (d02pt) and nag_ode_ivp_rkts_errass (d02pu) may be called only once.
If nag_ode_ivp_rkts_onestep (d02pf) returns with ${\mathbf{ifail}}={\mathbf{4}}$ then it is advisable to change to another code more suited to the solution of stiff problems. nag_ode_ivp_rkts_onestep (d02pf) will not return with ${\mathbf{ifail}}={\mathbf{4}}$ if the problem is actually stiff but it is estimated that integration can be completed using less function evaluations than already computed.

## Example

```function nag_ode_ivp_rkts_onestep_example
% Set initial conditions and input
method = int64(2);
tstart = 0;
tend = 2*pi;
yinit = [0;1];
hstart = 0;
thresh = [1e-08; 1e-08];
n = int64(2);
tol0 =  1.0E-4;
ynow = zeros(20, n);
tnow = zeros(20, 1);
err1 = zeros(20, 2);
err2 = zeros(20, 2);

% Set control for output
tol = 10.0*tol0;

% We run through the calculation twice with two tolerance values
for i = 1:2

tol = tol*0.1;

% Call setup function
[iwsav, rwsav, ifail] = ...
nag_ode_ivp_rkts_setup(tstart, tend, yinit, tol, thresh, method);

fprintf('\nCalculation with TOL = %8.1e\n\n', tol);
fprintf('    t         y1        y2       err1      err2\n');
fprintf(' %6.3f   %7.3f   %7.3f   %7.3f   %7.3f\n', tstart, yinit, 0, 0);

tnow(1) = tstart;
ynow(1,:) = yinit;
j=1;
while tnow(j) < tend
j=j+1;
[tnow(j), ynow(j, :), ypnow, user, iwsav, rwsav, ifail] = ...
nag_ode_ivp_rkts_onestep(@f, n, iwsav, rwsav);

err1(j, i) =  ynow(j, 1)-sin(tnow(j));
err2(j, i) =  ynow(j, 2)-cos(tnow(j));

fprintf(' %6.3f   %7.3f   %7.3f   %7.3f   %7.3f\n', tnow(j), ynow(j, :), err1(j, i), err2(j));
end

[fevals, stepcost, waste, stepsok, hnext, iwsav, ifail] = ...
nag_ode_ivp_rkts_diag(iwsav, rwsav);
fprintf('Cost of the integration in evaluations of f is %d\n', fevals);

% Store the t values for use in plotting errors
if i == 1
tnow1 = tnow;
end

% Store value of j
npts(i) = j;
end

% Plot results
fig = figure('Number', 'off');
title('First-order ODEs using Step-by-step Runge-Kutta Medium-order Method using Two Tolerances');
hold on;
axis([0 10 -1.2 1.2]);
xlabel('t');
ylabel('Solution (y, y'')');
plot(tnow(1:npts(2)), ynow(1:npts(2), 1), '-xr');
text(ceil(tnow(npts(2))), ynow(npts(2), 1), 'y', 'Color', 'r');
plot(tnow(1:npts(2)), ynow(1:npts(2), 2), '-xg');
text(ceil(tnow(npts(2))), ynow(npts(2), 2), 'y''', 'Color', 'g');
% Plot errors with a different (log) scale
ax1 = gca;
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','bottom',...
'YAxisLocation','right',...
'YScale', 'log', ...
'Color','none',...
'XColor','k','YColor','k');
hold on;
axis([0 10 1e-9 1e-4]);
ylabel('abs(Error)');
plot(ax2, tnow1(1:npts(1)), abs(err1(1:npts(1), 1)), '-*b');
text(ceil(tnow1(npts(1))), err1(npts(1), 1) - 1e-5, 'y-error (tol=0.001)', 'Color', 'b');
plot(ax2, tnow, abs(err1(:, 2)), '-sm');
text(ceil(tnow(npts(2))), err1(npts(2), 2), 'y-error (tol=0.0001)', 'Color', 'm');

function [yp, user] = f(t, n, y, user)
yp = [y(2); -y(1)];
```
```

Calculation with TOL =  1.0e-04

t         y1        y2       err1      err2
0.000     0.000     1.000     0.000     0.000
0.785     0.707     0.707     0.000    -0.000
1.519     0.999     0.051    -0.000    -0.000
2.282     0.757    -0.653    -0.000    -0.000
2.911     0.229    -0.974    -0.000     0.000
3.706    -0.535    -0.845    -0.000     0.000
4.364    -0.940    -0.341     0.000     0.000
5.320    -0.821     0.571     0.000     0.000
5.802    -0.463     0.886     0.000    -0.000
6.283     0.000     1.000     0.000    -0.000
Cost of the integration in evaluations of f is 204

Calculation with TOL =  1.0e-05

t         y1        y2       err1      err2
0.000     0.000     1.000     0.000     0.000
0.393     0.383     0.924     0.000    -0.000
0.785     0.707     0.707    -0.000    -0.000
1.416     0.988     0.154    -0.000    -0.000
1.870     0.956    -0.294    -0.000     0.000
2.204     0.806    -0.592    -0.000     0.000
2.761     0.371    -0.929    -0.000     0.000
3.230    -0.088    -0.996    -0.000     0.000
3.587    -0.430    -0.903    -0.000    -0.000
4.022    -0.771    -0.637     0.000    -0.000
4.641    -0.997    -0.072     0.000     0.000
5.152    -0.905     0.426     0.000     0.000
5.521    -0.690     0.724     0.000     0.000
5.902    -0.372     0.928     0.000     0.000
6.283     0.000     1.000     0.000     0.000
Cost of the integration in evaluations of f is 314

```
```function d02pf_example
% Set initial conditions and input
method = int64(2);
tstart = 0;
tend = 2*pi;
yinit = [0;1];
hstart = 0;
thresh = [1e-08; 1e-08];
n = int64(2);
tol0 =  1.0E-4;
ynow = zeros(20, n);
tnow = zeros(20, 1);
err1 = zeros(20, 2);
err2 = zeros(20, 2);

% Set control for output
tol = 10.0*tol0;

% We run through the calculation twice with two tolerance values
for i = 1:2

tol = tol*0.1;

% Call setup function
[iwsav, rwsav, ifail] = d02pq(tstart, tend, yinit, tol, thresh, method);

fprintf('\nCalculation with TOL = %8.1e\n\n', tol);
fprintf('    t         y1        y2       err1      err2\n');
fprintf(' %6.3f   %7.3f   %7.3f   %7.3f   %7.3f\n', tstart, yinit, 0, 0);

tnow(1) = tstart;
ynow(1,:) = yinit;
j=1;
while tnow(j) < tend
j=j+1;
[tnow(j), ynow(j, :), ypnow, user, iwsav, rwsav, ifail] = ...
d02pf(@f, n, iwsav, rwsav);

err1(j, i) =  ynow(j, 1)-sin(tnow(j));
err2(j, i) =  ynow(j, 2)-cos(tnow(j));

fprintf(' %6.3f   %7.3f   %7.3f   %7.3f   %7.3f\n', tnow(j), ynow(j, :), err1(j, i), err2(j));
end

[fevals, stepcost, waste, stepsok, hnext, iwsav, ifail] = d02pt(iwsav, rwsav);
fprintf('Cost of the integration in evaluations of f is %d\n', fevals);

% Store the t values for use in plotting errors
if i == 1
tnow1 = tnow;
end

% Store value of j
npts(i) = j;
end

% Plot results
fig = figure('Number', 'off');
title('First-order ODEs using Step-by-step Runge-Kutta Medium-order Method using Two Tolerances');
hold on;
axis([0 10 -1.2 1.2]);
xlabel('t');
ylabel('Solution (y, y'')');
plot(tnow(1:npts(2)), ynow(1:npts(2), 1), '-xr');
text(ceil(tnow(npts(2))), ynow(npts(2), 1), 'y', 'Color', 'r');
plot(tnow(1:npts(2)), ynow(1:npts(2), 2), '-xg');
text(ceil(tnow(npts(2))), ynow(npts(2), 2), 'y''', 'Color', 'g');
% Plot errors with a different (log) scale
ax1 = gca;
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','bottom',...
'YAxisLocation','right',...
'YScale', 'log', ...
'Color','none',...
'XColor','k','YColor','k');
hold on;
axis([0 10 1e-9 1e-4]);
ylabel('abs(Error)');
plot(ax2, tnow1(1:npts(1)), abs(err1(1:npts(1), 1)), '-*b');
text(ceil(tnow1(npts(1))), err1(npts(1), 1) - 1e-5, 'y-error (tol=0.001)', 'Color', 'b');
plot(ax2, tnow, abs(err1(:, 2)), '-sm');
text(ceil(tnow(npts(2))), err1(npts(2), 2), 'y-error (tol=0.0001)', 'Color', 'm');

function [yp, user] = f(t, n, y, user)
yp = [y(2); -y(1)];
```
```

Calculation with TOL =  1.0e-04

t         y1        y2       err1      err2
0.000     0.000     1.000     0.000     0.000
0.785     0.707     0.707     0.000    -0.000
1.519     0.999     0.051    -0.000    -0.000
2.282     0.757    -0.653    -0.000    -0.000
2.911     0.229    -0.974    -0.000     0.000
3.706    -0.535    -0.845    -0.000     0.000
4.364    -0.940    -0.341     0.000     0.000
5.320    -0.821     0.571     0.000     0.000
5.802    -0.463     0.886     0.000    -0.000
6.283     0.000     1.000     0.000    -0.000
Cost of the integration in evaluations of f is 204

Calculation with TOL =  1.0e-05

t         y1        y2       err1      err2
0.000     0.000     1.000     0.000     0.000
0.393     0.383     0.924     0.000    -0.000
0.785     0.707     0.707    -0.000    -0.000
1.416     0.988     0.154    -0.000    -0.000
1.870     0.956    -0.294    -0.000     0.000
2.204     0.806    -0.592    -0.000     0.000
2.761     0.371    -0.929    -0.000     0.000
3.230    -0.088    -0.996    -0.000     0.000
3.587    -0.430    -0.903    -0.000    -0.000
4.022    -0.771    -0.637     0.000    -0.000
4.641    -0.997    -0.072     0.000     0.000
5.152    -0.905     0.426     0.000     0.000
5.521    -0.690     0.724     0.000     0.000
5.902    -0.372     0.928     0.000     0.000
6.283     0.000     1.000     0.000     0.000
Cost of the integration in evaluations of f is 314

```

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

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