PDF version (NAG web site
, 64bit version, 64bit version)
NAG Toolbox: nag_ode_ivp_rkm_val_simple (d02bg)
Purpose
nag_ode_ivp_rkm_val_simple (d02bg) integrates a system of firstorder ordinary differential equations over an interval with suitable initial conditions, using a Runge–Kutta–Merson method, until a specified component attains a given value.
Syntax
[
x,
y,
tol,
ifail] = d02bg(
x,
xend,
y,
tol,
hmax,
m,
val,
fcn, 'n',
n)
[
x,
y,
tol,
ifail] = nag_ode_ivp_rkm_val_simple(
x,
xend,
y,
tol,
hmax,
m,
val,
fcn, 'n',
n)
Description
nag_ode_ivp_rkm_val_simple (d02bg) advances the solution of a system of ordinary differential equations
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
f_{i}${f}_{i}$ in terms of
x$x$ and
y_{1},y_{2}, … ,y_{n}${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$ (see
Section [Parameters]), and the values of
y_{1},y_{2}, … ,y_{n}${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 specified component y_{m}${y}_{m}$ of the solution to determine an interval where it attains a given value α$\alpha $. The position where this value is attained is then determined accurately by interpolation on the solution and its derivative. It is assumed that the solution of y_{m} = α${y}_{m}=\alpha $ can be determined by searching for a change in sign in the function y_{m} − α${y}_{m}\alpha $.
The accuracy of the integration and, indirectly, of the determination of the position where
y_{m} = α${y}_{m}=\alpha $ is controlled by the parameter
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
xend < x${\mathbf{xend}}<{\mathbf{x}}$ on entry integration will proceed in the 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 y_{1},y_{2}, … ,y_{n}${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
y_{m} = α${y}_{m}=\alpha $.
nag_ode_ivp_rkm_val_simple (d02bg) 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
y_{m} = α${y}_{m}=\alpha $ 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_val_simple (d02bg) 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_val_simple (d02bg) with
tol = 10.0^{ − p}${\mathbf{tol}}={10.0}^{p}$ and
tol = 10.0^{ − p − 1}${\mathbf{tol}}={10.0}^{p1}$ if
p$p$ correct decimal digits in the solution are required.
Constraint:
tol > 0.0${\mathbf{tol}}>0.0$.
 5:
hmax – double scalar
Controls how the sign of
y_{m} − α${y}_{m}\alpha $ is checked.
 hmax = 0.0${\mathbf{hmax}}=0.0$
 y_{m} − α${y}_{m}\alpha $ is checked at every internal integration step.
 hmax ≠ 0.0${\mathbf{hmax}}\ne 0.0$
 The computed solution is checked for a change in sign of y_{m} − α${y}_{m}\alpha $ at steps of not greater than hmax$\left{\mathbf{hmax}}\right$. 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 y_{m} − α${y}_{m}\alpha $ 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 y_{m} − α${y}_{m}\alpha $ is expected on the range x to xend then hmax = 0.0${\mathbf{hmax}}=0.0$ is most appropriate.
 6:
m – int64int32nag_int scalar
The index m$m$ of the component of the solution whose value is to be checked.
Constraint:
1 ≤ m ≤ n$1\le {\mathbf{m}}\le {\mathbf{n}}$.
 7:
val – double scalar
The value of
α$\alpha $ in the equation
y_{m} = α${y}_{m}=\alpha $ to be solved for
x.
 8:
fcn – function handle or string containing name of mfile
fcn must evaluate the functions
f_{i}${f}_{i}$ (i.e., the derivatives
y_{i}^{ ′ }${y}_{i}^{\prime}$) for given values of its arguments
x,y_{1}, … ,y_{n}$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
y_{i}${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
f_{i}${f}_{\mathit{i}}$, for i = 1,2, … ,n$\mathit{i}=1,2,\dots ,\mathit{n}$.
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$.
Input Parameters Omitted from the MATLAB Interface
 w
Output Parameters
 1:
x – double scalar
The point where the component
y_{m}${y}_{m}$ attains the value
α$\alpha $ unless an error has occurred, when it contains the value of
x$x$ at the error. In particular, if
y_{m} ≠ α${y}_{m}\ne \alpha $ anywhere on the range
x = x$x={\mathbf{x}}$ to
x = xend$x={\mathbf{xend}}$, it will contain
xend on exit.
 2:
y(n) – double array
The computed values of the solution at a point near the solution
x, unless an error has occurred when they contain the computed values at the final value of
x.
 3:
tol – double scalar
Normally unchanged. However if the range from
x to the position where
y_{m} = α${y}_{m}=\alpha $ (or to the final value of
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, on return,
tol has its sign changed. To check results returned with
tol < 0.0${\mathbf{tol}}<0.0$,
nag_ode_ivp_rkm_val_simple (d02bg) 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
ifail = 0${\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$
On entry,  tol ≤ 0.0${\mathbf{tol}}\le 0.0$, 
or  n ≤ 0${\mathbf{n}}\le 0$, 
or  m ≤ 0${\mathbf{m}}\le 0$, 
or  m > n${\mathbf{m}}>{\mathbf{n}}$. 
 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
y_{m} − α${y}_{m}\alpha $ changes sign has been located up to the point
x = x$x={\mathbf{x}}$.
 ifail = 3${\mathbf{ifail}}=3$
tol is too small for the function 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.
 W ifail = 4${\mathbf{ifail}}=4$

At no point in the range
x to
xend did the function
y_{m} − α${y}_{m}\alpha $ change sign. It is assumed that
y_{m} − α${y}_{m}\alpha $ 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
y_{m} = α${y}_{m}=\alpha $ 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
ifail = 2${\mathbf{ifail}}={\mathbf{2}}$ or
3${\mathbf{3}}$ is possible.
Further Comments
The time taken by
nag_ode_ivp_rkm_val_simple (d02bg) depends on the complexity and mathematical properties of the system of differential equations defined by
fcn, on the range, the position of solution and the tolerance. There is also an overhead of the form
a + b × n$a+b\times \mathit{n}$ where
a$a$ and
b$b$ are machinedependent computing times.
For some problems it is possible that
nag_ode_ivp_rkm_val_simple (d02bg) will exit with
ifail = 4${\mathbf{ifail}}={\mathbf{4}}$ due to inaccuracy of the computed value
y_{m}${y}_{m}$. For example, consider a case where the component
y_{m}${y}_{m}$ has a maximum in the integration range and
α$\alpha $ is close to the maximum value. If
tol is too large, it is possible that the maximum might be estimated as less than
α$\alpha $, or even that the integration step length chosen might be so long that the maximum of
y_{m}${y}_{m}$ and the (two) positions where
y_{m} = α${y}_{m}=\alpha $ are all in the same step and so the position where
y_{m} = α${y}_{m}=\alpha $ remains undetected. Both these difficulties can be overcome by reducing
tol sufficiently and, if necessary, by choosing
hmax sufficiently small. For similar reasons, care should be taken when choosing
xend. If possible, you should choose
xend well beyond the point where
y_{m}${y}_{m}$ is expected to equal
α$\alpha $, for example
xend − x${\mathbf{xend}}{\mathbf{x}}$ should be made about
50%$50\%$ longer 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_{m}${y}_{m}$ at
xend, then inaccuracy is not a likely source of error.
If
nag_ode_ivp_rkm_val_simple (d02bg) fails with
ifail = 3${\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_val_simple (d02bg) fails with
ifail = 2${\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 wellbehaved 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 ifail = 2${\mathbf{ifail}}={\mathbf{2}}$, unless overflow occurs first. If overflow occurs using nag_ode_ivp_rkm_val_simple (d02bg), function 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 use very small steps in x$x$ (internally to nag_ode_ivp_rkm_val_simple (d02bg)) to preserve stability. This will usually exhibit itself by making the computing time excessively long, or occasionally by an exit with ifail = 2${\mathbf{ifail}}={\mathbf{2}}$. Merson's method is not efficient in such cases, and you should try the method nag_ode_ivp_bdf_zero_simple (d02ej) which uses a Backward Differentiation Formula. To determine whether a problem is stiff, nag_ode_ivp_rkts_range (d02pe) may be used. 
For wellbehaved 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_val_simple (d02bg) is not sufficiently general, you should consider the functions
nag_ode_ivp_rkm_zero_simple (d02bh) and
nag_ode_ivp_rkts_onestep (d02pf). Routine
nag_ode_ivp_rkm_zero_simple (d02bh) can be used to solve an equation involving the components
y_{1},y_{2}, … ,y_{n}${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$ and their derivatives (for example, to find where a component passes through zero or to find the maximum value of a component). It also permits a more general form of error control and may be preferred to
nag_ode_ivp_rkm_val_simple (d02bg) if the component whose value is to be determined is very small in modulus on the integration range.
nag_ode_ivp_rkm_zero_simple (d02bh) can always be used in place of
nag_ode_ivp_rkm_val_simple (d02bg), but will usually be computationally more expensive for solving the same problem.
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
y_{1},y_{2}, … ,y_{n}${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$ and their derivatives.
This function is only intended to be used to locate the first zero of the function y_{m} − α${y}_{m}\alpha $. If later zeros are required you are strongly advised to construct your own more general rootfinding functions as discussed above.
Example
Open in the MATLAB editor:
nag_ode_ivp_rkm_val_simple_example
function nag_ode_ivp_rkm_val_simple_example
x = 0;
xend = 10;
y = [0.5;
0.5;
0.6283185307179586];
tol = 0.0001;
hmax = 0;
m = int64(1);
val = 0;
[xOut, yOut, tolOut, ifail] = nag_ode_ivp_rkm_val_simple(x, xend, y, tol, hmax, m, val, @fcn)
function [f] = fcn(x,y)
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;
xOut =
7.2884
yOut =
0.6115
0.5064
0.8391
tolOut =
1.0000e04
ifail =
0
Open in the MATLAB editor:
d02bg_example
function d02bg_example
x = 0;
xend = 10;
y = [0.5;
0.5;
0.6283185307179586];
tol = 0.0001;
hmax = 0;
m = int64(1);
val = 0;
[xOut, yOut, tolOut, ifail] = d02bg(x, xend, y, tol, hmax, m, val, @fcn)
function [f] = fcn(x,y)
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;
xOut =
7.2884
yOut =
0.6115
0.5064
0.8391
tolOut =
1.0000e04
ifail =
0
PDF version (NAG web site
, 64bit version, 64bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013