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

# NAG Toolbox: nag_ode_ivp_2nd_rkn_interp (d02lz)

## Purpose

nag_ode_ivp_2nd_rkn_interp (d02lz) interpolates components of the solution of a non-stiff system of second-order differential equations from information provided by the integrator nag_ode_ivp_2nd_rkn (d02la), when the low-order method has been used.

## Syntax

[ywant, ypwant, ifail] = d02lz(t, y, yp, nwant, twant, rwork, 'neq', neq)
[ywant, ypwant, ifail] = nag_ode_ivp_2nd_rkn_interp(t, y, yp, nwant, twant, rwork, 'neq', neq)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 22: lrwork has been removed from the interface
.

## Description

nag_ode_ivp_2nd_rkn_interp (d02lz) evaluates the first nwant${\mathbf{nwant}}$ (neq$\text{}\le {\mathbf{neq}}$) components of the solution of a non-stiff system of second-order ordinary differential equations at any point using a special Runge–Kutta–Nystrom formula (see Dormand and Prince (1986)) and information generated by nag_ode_ivp_2nd_rkn (d02la) when the low-order method has been used. This information must be presented unchanged to nag_ode_ivp_2nd_rkn_interp (d02lz). nag_ode_ivp_2nd_rkn_interp (d02lz) should not normally be used to extrapolate outside the range of the values from nag_ode_ivp_2nd_rkn (d02la).

## References

Dormand J R and Prince P J (1986) Runge–Kutta–Nystrom triples Mathematical Report TP-CS-86-05 Teesside Polytechnic

## Parameters

### Compulsory Input Parameters

1:     t – double scalar
t$t$, the current value at which the solution and its derivative have been computed (as returned in parameter t on output from nag_ode_ivp_2nd_rkn (d02la)).
2:     y(neq) – double array
The i$\mathit{i}$th component of the solution at t$t$, for i = 1,2,,neq$\mathit{i}=1,2,\dots ,{\mathbf{neq}}$, as returned from nag_ode_ivp_2nd_rkn (d02la).
3:     yp(neq) – double array
The i$\mathit{i}$th component of the derivative at t$t$, for i = 1,2,,neq$\mathit{i}=1,2,\dots ,{\mathbf{neq}}$, as returned from nag_ode_ivp_2nd_rkn (d02la).
4:     nwant – int64int32nag_int scalar
The number of components of the solution and derivative whose values at twant are required. The first nwant components are evaluated.
Constraint: 1nwantneq$1\le {\mathbf{nwant}}\le {\mathbf{neq}}$.
5:     twant – double scalar
The point at which components of the solution and derivative are to be evaluated. twant should not normally be an extrapolation point, that is twant should satisfy
 told ≤ twant ≤ t, $told≤twant≤t,$
or if integration is proceeding in the negative direction
 told ≥ twant ≥ t, $told≥twant≥t,$
where told is the previous integration point which is held in an element of the array rwork and is, to within rounding, ${\mathbf{t}}-{\mathbf{hused}}$. (hused is given by nag_ode_ivp_2nd_rkn_diag (d02ly).) Extrapolation is permitted but not recommended, and ${\mathbf{ifail}}={\mathbf{2}}$ is returned whenever extrapolation is attempted.
6:     rwork(lrwork) – double array
This must be the same parameter rwork as supplied to nag_ode_ivp_2nd_rkn (d02la). It is used to pass information from nag_ode_ivp_2nd_rkn (d02la) to nag_ode_ivp_2nd_rkn_interp (d02lz) and therefore the contents of this array must not be changed before calling nag_ode_ivp_2nd_rkn_interp (d02lz).

### Optional Input Parameters

1:     neq – int64int32nag_int scalar
Default: The dimension of the arrays y, yp. (An error is raised if these dimensions are not equal.)
The number of second-order ordinary differential equations being solved by nag_ode_ivp_2nd_rkn (d02la). It must contain the same value as the parameter neq in a prior call to nag_ode_ivp_2nd_rkn (d02la).

lrwork

### Output Parameters

1:     ywant(nwant) – double array
The calculated value of the i$\mathit{i}$th component of the solution at t = twant$t={\mathbf{twant}}$, for i = 1,2,,nwant$\mathit{i}=1,2,\dots ,{\mathbf{nwant}}$.
2:     ypwant(nwant) – double array
The calculated value of the i$\mathit{i}$th component of the derivative at t = twant$t={\mathbf{twant}}$, for i = 1,2,,nwant$\mathit{i}=1,2,\dots ,{\mathbf{nwant}}$.
3:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).
If nag_ode_ivp_2nd_rkn_interp (d02lz) is to be used for extrapolation at twant, ifail should be set to 1$1$ before entry. It is then essential to test the value of ifail on exit.

## 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$
Illegal input detected, i.e., one of the following conditions:
 – nag_ode_ivp_2nd_rkn (d02la) has not been called; – one or both of the parameters neq and lrwork does not match the corresponding parameter supplied to the setup function nag_ode_ivp_2nd_rkn_setup (d02lx); – no integration steps have been taken since the last call to nag_ode_ivp_2nd_rkn_setup (d02lx) with start = true${\mathbf{start}}=\mathbf{true}$; – nwant < 1${\mathbf{nwant}}<1$ or ${\mathbf{nwant}}>{\mathbf{neq}}$.
This error exit can be caused if elements of rwork have been overwritten.
W ifail = 2${\mathbf{ifail}}=2$
nag_ode_ivp_2nd_rkn_interp (d02lz) has been called for extrapolation. The values of the solution and its derivative at twant have been calculated and placed in ywant and ypwant before returning with this error number (see Section [Accuracy]).
ifail = 3${\mathbf{ifail}}=3$
nag_ode_ivp_2nd_rkn (d02la) last used the high order method to integrate the system of differential equations. Interpolation is not permitted with this method.

## Accuracy

The error in interpolation is of a similar order to the error arising from the integration using nag_ode_ivp_2nd_rkn (d02la) with the lower order method.
The same order of accuracy can be expected when extrapolating using nag_ode_ivp_2nd_rkn_interp (d02lz). However, the actual error in extrapolation will, in general, be much larger than for interpolation.

When interpolation for only a few components is required then it is more efficient to order the components of interest so that they are numbered first.

## Example

```function nag_ode_ivp_2nd_rkn_interp_example
t = 0;
tend = 20;
y = [0.5;
0];
yp = [0;
1.732050807568877];
ydp = zeros(2, 1);
rwork = zeros(56,1);
twant = 2;
tnext = 2;
nwant = int64(2);
[startOut, rwork, ifail] = ...
nag_ode_ivp_2nd_rkn_setup(0, 1e-4, zeros(2,1), zeros(2,1), int64(0), true, true, false, rwork);
fprintf('\n  T       Y(1)       Y(2)\n');
fprintf('%4.1f %10.5f %10.5f\n', t, y(1), y(2));
while (t < tend && ifail == 0)
[t, y, yp, ydp, rwork, ifail] = nag_ode_ivp_2nd_rkn(@fcn, t, tend, y, yp, ydp, rwork);
while (tnext <= t && ifail == 0)
[ywant, ypwant, ifail] = nag_ode_ivp_2nd_rkn_interp(t, y, yp, nwant, tnext, rwork);
fprintf('%4.1f %10.5f %10.5f\n', tnext, ywant(1), ywant(2));
tnext = tnext + 2;
end
end
if (ifail == 0)
[hnext, hused, hstart, nsucc, nfail, natt, thres, thresp, ifail] =  ...
nag_ode_ivp_2nd_rkn_diag(nwant, rwork);

fprintf('\n Number of successful steps = %d\n', nsucc);
fprintf(' Number of failed steps     = %d\n', nfail);
end

function ydp = fcn(neq, t, y)
% Evaluate second derivatives.
r = sqrt(y(1)^2+y(2)^2)^3;
f = zeros(2,1);
ydp(1) = -y(1)/r;
ydp(2) = -y(2)/r;
```
```

T       Y(1)       Y(2)
0.0    0.50000    0.00000
2.0   -1.20573    0.61357
4.0   -1.33476   -0.47685
6.0    0.35748   -0.44558
8.0   -1.03762    0.73022
10.0   -1.42617   -0.32658
12.0    0.05515   -0.72032
14.0   -0.82880    0.81788
16.0   -1.48103   -0.16788
18.0   -0.26719   -0.84223
20.0   -0.57803    0.86339

Number of successful steps = 108
Number of failed steps     = 16

```
```function d02lz_example
t = 0;
tend = 20;
y = [0.5;
0];
yp = [0;
1.732050807568877];
ydp = zeros(2, 1);
rwork = zeros(56,1);
twant = 2;
tnext = 2;
nwant = int64(2);
[startOut, rwork, ifail] = ...
d02lx(0, 1e-4, zeros(2,1), zeros(2,1), int64(0), true, true, false, rwork);
fprintf('\n  T       Y(1)       Y(2)\n');
fprintf('%4.1f %10.5f %10.5f\n', t, y(1), y(2));
while (t < tend && ifail == 0)
[t, y, yp, ydp, rwork, ifail] = d02la(@fcn, t, tend, y, yp, ydp, rwork);
while (tnext <= t && ifail == 0)
[ywant, ypwant, ifail] = d02lz(t, y, yp, nwant, tnext, rwork);
fprintf('%4.1f %10.5f %10.5f\n', tnext, ywant(1), ywant(2));
tnext = tnext + 2;
end
end
if (ifail == 0)
[hnext, hused, hstart, nsucc, nfail, natt, thres, thresp, ifail] =  ...
d02ly(nwant, rwork);

fprintf('\n Number of successful steps = %d\n', nsucc);
fprintf(' Number of failed steps     = %d\n', nfail);
end

function ydp = fcn(neq, t, y)
% Evaluate second derivatives.
r = sqrt(y(1)^2+y(2)^2)^3;
f = zeros(2,1);
ydp(1) = -y(1)/r;
ydp(2) = -y(2)/r;
```
```

T       Y(1)       Y(2)
0.0    0.50000    0.00000
2.0   -1.20573    0.61357
4.0   -1.33476   -0.47685
6.0    0.35748   -0.44558
8.0   -1.03762    0.73022
10.0   -1.42617   -0.32658
12.0    0.05515   -0.72032
14.0   -0.82880    0.81788
16.0   -1.48103   -0.16788
18.0   -0.26719   -0.84223
20.0   -0.57803    0.86339

Number of successful steps = 108
Number of failed steps     = 16

```