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_rkm_zero_simple (d02bh)

## Purpose

nag_ode_ivp_rkm_zero_simple (d02bh) integrates a system of first-order ordinary differential equations over an interval with suitable initial conditions, using a Runge–Kutta–Merson method, until a user-specified function of the solution is zero.

## Syntax

[x, y, tol, ifail] = d02bh(x, xend, y, tol, irelab, hmax, fcn, g, 'n', n)
[x, y, tol, ifail] = nag_ode_ivp_rkm_zero_simple(x, xend, y, tol, irelab, hmax, fcn, g, 'n', n)

## Description

nag_ode_ivp_rkm_zero_simple (d02bh) 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}}$ towards x = xend$x={\mathbf{xend}}$ using a Merson form of the Runge–Kutta 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}}$ (see Section [Parameters]), and the values of y1,y2,,yn${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$ must be given at x = x$x={\mathbf{x}}$.
As the integration proceeds, a check is made on the function g(x,y)$g\left(x,y\right)$ specified by you, to determine an interval where it changes sign. The position of this sign change is then determined accurately by interpolating for the solution and its derivative. 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$g\left(x,y\right)=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 and, indirectly, of the determination of the position where g(x,y) = 0$g\left(x,y\right)=0$, is controlled by tol.
For a description of Runge–Kutta 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
Must be set to the initial value of the independent variable x$x$.
2:     xend – double scalar
The final value of the independent variable x$x$.
If ${\mathbf{xend}}<{\mathbf{x}}$ on entry, integration proceeds in a negative direction.
3:     y(n) – double array
n, the dimension of the array, must satisfy the constraint n > 0${\mathbf{n}}>0$.
The initial values of the solution y1,y2,,yn${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$.
4:     tol – double scalar
Must be set to a positive tolerance for controlling the error in the integration and in the determination of the position where g(x,y) = 0.0$g\left(x,y\right)=0.0$.
nag_ode_ivp_rkm_zero_simple (d02bh) has been designed so that, for most problems, a reduction in tol leads to an approximately proportional reduction in the error in the solution obtained in the integration. The relation between changes in tol and the error in the determination of the position where g(x,y) = 0.0$g\left(x,y\right)=0.0$ is less clear, but for tol small enough the error should be approximately proportional to tol. However, the actual relation between tol and the accuracy cannot be guaranteed. You are strongly recommended to call nag_ode_ivp_rkm_zero_simple (d02bh) 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 results obtained by calling nag_ode_ivp_rkm_zero_simple (d02bh) with tol = 10.0p${\mathbf{tol}}={10.0}^{-p}$ and tol = 10.0p1${\mathbf{tol}}={10.0}^{-p-1}$ if p$p$ correct decimal digits in the solution are required.
Constraint: tol > 0.0${\mathbf{tol}}>0.0$.
5:     irelab – int64int32nag_int scalar
Determines 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:
irelab = 0${\mathbf{irelab}}=0$
esttol × max {1.0,|y1|,|y2|,,|yn|}$\mathit{est}\le {\mathbf{tol}}×\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left\{1.0,|{y}_{1}|,|{y}_{2}|,\dots ,|{y}_{\mathit{n}}|\right\}$;
irelab = 1${\mathbf{irelab}}=1$
esttol$\mathit{est}\le {\mathbf{tol}}$;
irelab = 2${\mathbf{irelab}}=2$
esttol × max {ε,|y1|,|y2|,,|yn|}$\mathit{est}\le {\mathbf{tol}}×\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left\{\epsilon ,|{y}_{1}|,|{y}_{2}|,\dots ,|{y}_{\mathit{n}}|\right\}$, where ε$\epsilon$ is machine precision.
If the appropriate condition is not satisfied, the step size is reduced and the solution 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 set irelab = 1${\mathbf{irelab}}=1$ on entry, whereas if the error requirement is in terms of the number of correct significant digits, then set irelab = 2${\mathbf{irelab}}=2$. Where there is no preference in the choice of error test, irelab = 0${\mathbf{irelab}}=0$ will result in a mixed error test. It should be borne in mind that the computed solution will be used in evaluating g(x,y)$g\left(x,y\right)$.
Constraint: irelab = 0${\mathbf{irelab}}=0$, 1$1$ or 2$2$.
6:     hmax – double scalar
If hmax = 0.0${\mathbf{hmax}}=0.0$, no special action is taken.
If hmax0.0${\mathbf{hmax}}\ne 0.0$, a check is made for a change in sign of g(x,y)$g\left(x,y\right)$ at steps not greater than |hmax|$|{\mathbf{hmax}}|$. This facility should be used if there is any chance of ‘missing’ the change in sign by checking too infrequently. For example, if two changes of sign of g(x,y)$g\left(x,y\right)$ are expected within a distance h$h$, say, of each other, then a suitable value for hmax might be hmax = h / 2${\mathbf{hmax}}=h/2$. If only one change of sign in g(x,y)$g\left(x,y\right)$ is expected on the range x to xend, then the choice hmax = 0.0${\mathbf{hmax}}=0.0$ is most appropriate.
7:     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 argument.
2:     y( : $:$) – double array
yi${y}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,\mathit{n}$, the value of the argument.

Output Parameters

1:     f( : $:$) – double array
The value of fi${f}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,\mathit{n}$.
8:     g – function handle or string containing name of m-file
g must evaluate the function g(x,y)$g\left(x,y\right)$ at a specified point.
[result] = g(x, y)

Input Parameters

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

Output Parameters

1:     result – double scalar
The result of the function.

### Optional Input Parameters

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

w

### Output Parameters

1:     x – double scalar
The point where g(x,y) = 0.0$g\left(x,y\right)=0.0$ unless an error has occurred, when it contains the value of x$x$ at the error. In particular, if g(x,y)0.0$g\left(x,y\right)\ne 0.0$ anywhere on the range x to xend, it will contain xend on exit.
2:     y(n) – double array
The computed values of the solution at the final point x = x$x={\mathbf{x}}$.
3:     tol – double scalar
Normally unchanged. However if the range from x = x$x={\mathbf{x}}$ to the position where g(x,y) = 0.0$g\left(x,y\right)=0.0$ (or to the final value of x$x$ if an error occurs) is so short that a small change in tol is unlikely to make any change in the computed solution, then tol is returned with its sign changed. To check results returned with tol < 0.0${\mathbf{tol}}<0.0$, nag_ode_ivp_rkm_zero_simple (d02bh) should be called again with a positive value of tol whose magnitude is considerably smaller than that of the previous call.
4:     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 irelab ≠ 0${\mathbf{irelab}}\ne 0$, 1$1$ or 2$2$.
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}}$, or dependence of the error on tol would be lost if further progress across the integration range were attempted (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(\mathit{n}\right)$ contain the computed values of the solution at the current point x = x$x={\mathbf{x}}$. 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_rkm_zero_simple (d02bh) to take an initial step (see Section [Further Comments]). x and y(1),y(2),,y(n)${\mathbf{y}}\left(1\right),{\mathbf{y}}\left(2\right),\dots ,{\mathbf{y}}\left(\mathit{n}\right)$ retain their initial values.
ifail = 4${\mathbf{ifail}}=4$
At no point in the range x to xend did the function g(x,y)$g\left(x,y\right)$ change sign. It is assumed that g(x,y) = 0.0$g\left(x,y\right)=0.0$ has no solution.
ifail = 5${\mathbf{ifail}}=5$ (nag_roots_contfn_brent_rcomm (c05az))
A serious error has occurred in an internal call to the specified function. Check all function calls and array dimensions. Seek expert help.
ifail = 6${\mathbf{ifail}}=6$
A serious error has occurred in an internal call to an integration function. Check all function calls and array dimensions. Seek expert help.
ifail = 7${\mathbf{ifail}}=7$
A serious error has occurred in an internal call to an interpolation function. Check all (sub)program calls and array dimensions. Seek expert help.

## Accuracy

The accuracy depends on tol, on the mathematical properties of the differential system, on the position where g(x,y) = 0.0$g\left(x,y\right)=0.0$ and on the method. It can be controlled by varying tol but the approximate proportionality of the error to tol holds only for a restricted range of values of tol. For tol too large, the underlying theory may break down and the result of varying tol may be unpredictable. For tol too small, rounding error may affect the solution significantly and an error exit with ${\mathbf{ifail}}={\mathbf{2}}$ or 3${\mathbf{3}}$ is possible.
The accuracy may also be restricted by the properties of g(x,y)$g\left(x,y\right)$. You should try to code g without introducing any unnecessary cancellation errors.

The time taken by nag_ode_ivp_rkm_zero_simple (d02bh) depends on the complexity and mathematical properties of the system of differential equations defined by fcn, the complexity of g, on the range, the position of the solution and the tolerance. There is also an overhead of the form a + b × n$a+b×\mathit{n}$ where a$a$ and b$b$ are machine-dependent computing times.
For some problems it is possible that nag_ode_ivp_rkm_zero_simple (d02bh) will return ${\mathbf{ifail}}={\mathbf{4}}$ because of inaccuracy of the computed values y, leading to inaccuracy in the computed values of g(x,y)$g\left(x,y\right)$ used in the search for the solution of g(x,y) = 0.0$g\left(x,y\right)=0.0$. This difficulty can be overcome by reducing tol sufficiently, and if necessary, by choosing hmax sufficiently small. If possible, you should choose xend well beyond the expected point where g(x,y) = 0.0$g\left(x,y\right)=0.0$; for example make |xendx|$|{\mathbf{xend}}-{\mathbf{x}}|$ about 50%$50%$ larger than the expected range. As a simple check, if, with xend fixed, a change in tol does not lead to a significant change in y at xend, then inaccuracy is not a likely source of error.
If nag_ode_ivp_rkm_zero_simple (d02bh) fails with ${\mathbf{ifail}}={\mathbf{3}}$, then it could 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_rkm_zero_simple (d02bh) fails with ${\mathbf{ifail}}={\mathbf{2}}$, it is likely 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. If overflow occurs using nag_ode_ivp_rkm_zero_simple (d02bh), nag_ode_ivp_rkts_onestep (d02pf) can be used instead to detect the increasing solution, before overflow occurs. In any case, numerical integration cannot be continued through a singularity, and analytical treatment should be considered; (b) for ‘stiff’ equations, where the solution contains rapidly decaying components, the function will compute in very small steps in x$x$ (internally to nag_ode_ivp_rkm_zero_simple (d02bh)) to preserve stability. This will usually exhibit itself by making the computing time excessively long, or occasionally by an exit with ${\mathbf{ifail}}={\mathbf{2}}$. Merson's method is not efficient in such cases, and you should try nag_ode_ivp_bdf_zero_simple (d02ej) which uses a Backward Differentiation Formula method. To determine whether a problem is stiff, nag_ode_ivp_rkts_range (d02pe) may be used.
For well-behaved systems with no difficulties such as stiffness or singularities, the Merson method should work well for low accuracy calculations (three or four figures). For high accuracy calculations or where fcn is costly to evaluate, Merson's method may not be appropriate and a computationally less expensive method may be nag_ode_ivp_adams_zero_simple (d02cj) which uses an Adams method.
For problems for which nag_ode_ivp_rkm_zero_simple (d02bh) is not sufficiently general, you should consider nag_ode_ivp_rkts_onestep (d02pf). nag_ode_ivp_rkts_onestep (d02pf) is a more general function with many facilities including a more general error control criterion. nag_ode_ivp_rkts_onestep (d02pf) can be combined with the rootfinder nag_roots_contfn_brent_rcomm (c05az) and the interpolation function nag_ode_ivp_rkts_interp (d02ps) to solve equations involving y1,y2,,yn${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$ and their derivatives.
nag_ode_ivp_rkm_zero_simple (d02bh) can also be used to solve an equation involving x$x$, y1,y2,,yn${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$ and the derivatives of y1,y2,,yn${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$. For example in Section [Example], nag_ode_ivp_rkm_zero_simple (d02bh) is used to find a value of x > 0.0${\mathbf{x}}>0.0$ where y(1) = 0.0${\mathbf{y}}\left(1\right)=0.0$. It could instead be used to find a turning-point of y1${y}_{1}$ by replacing the function g(x,y)$g\left(x,y\right)$ in the program by:
```function result = g(x,y)
f = d02bh_f(x,y);
result = f(1);
```
This function is only intended to locate the first zero of g(x,y)$g\left(x,y\right)$. If later zeros are required, you are strongly advised to construct your own more general root-finding functions as discussed above.

## Example

```function nag_ode_ivp_rkm_zero_simple_example
x = 0;
xend = 10;
y = [0.5;
0.5;
0.6283185307179586];
tol = 0.0001;
irelab = int64(0);
hmax = 0;
[xOut, yOut, tolOut, ifail] = ...
nag_ode_ivp_rkm_zero_simple(x, xend, y, tol, irelab, hmax, @fcn, @g)

function f = fcn(x,y)
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)
result = y(1);
```
```

xOut =

7.2884

yOut =

-0.0000
0.4749
-0.7601

tolOut =

1.0000e-04

ifail =

0

```
```function d02bh_example
x = 0;
xend = 10;
y = [0.5;
0.5;
0.6283185307179586];
tol = 0.0001;
irelab = int64(0);
hmax = 0;
[xOut, yOut, tolOut, ifail] = ...
d02bh(x, xend, y, tol, irelab, hmax, @fcn, @g)

function f = fcn(x,y)
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)
result = y(1);
```
```

xOut =

7.2884

yOut =

-0.0000
0.4749
-0.7601

tolOut =

1.0000e-04

ifail =

0

```