# NAG FL Interfaced02bgf (ivp_​rkm_​val_​simple)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

d02bgf integrates a system of first-order ordinary differential equations over an interval with suitable initial conditions, using a Runge–Kutta–Merson method, until a specified component attains a given value.

## 2Specification

Fortran Interface
 Subroutine d02bgf ( x, xend, n, y, tol, hmax, m, val, fcn, w,
 Integer, Intent (In) :: n, m Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: xend, hmax, val Real (Kind=nag_wp), Intent (Inout) :: x, y(n), tol Real (Kind=nag_wp), Intent (Out) :: w(n,10) External :: fcn
#include <nag.h>
 void d02bgf_ (double *x, const double *xend, const Integer *n, double y[], double *tol, const double *hmax, const Integer *m, const double *val, void (NAG_CALL *fcn)(const double *x, const double y[], double f[]),double w[], Integer *ifail)
The routine may be called by the names d02bgf or nagf_ode_ivp_rkm_val_simple.

## 3Description

d02bgf advances the solution of a system of ordinary differential equations
 $yi′=fi(x,y1,y2,…,yn), i=1,2,…,n,$
from $x={\mathbf{x}}$ towards $x={\mathbf{xend}}$ using a Merson form of the Runge–Kutta method. The system is defined by fcn, which evaluates ${f}_{i}$ in terms of $x$ and ${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$ (see Section 5), and the values of ${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$ must be given at $x={\mathbf{x}}$.
As the integration proceeds, a check is made on the specified component ${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}=\alpha$ can be determined by searching for a change in sign in the function ${y}_{m}-\alpha$.
The accuracy of the integration and, indirectly, of the determination of the position where ${y}_{m}=\alpha$ is controlled by the argument tol.
For a description of Runge–Kutta methods and their practical implementation see Hall and Watt (1976).
Hall G and Watt J M (ed.) (1976) Modern Numerical Methods for Ordinary Differential Equations Clarendon Press, Oxford

## 5Arguments

1: $\mathbf{x}$Real (Kind=nag_wp) Input/Output
On entry: must be set to the initial value of the independent variable $x$.
On exit: the point where the component ${y}_{m}$ attains the value $\alpha$ unless an error has occurred, when it contains the value of $x$ at the error. In particular, if ${y}_{m}\ne \alpha$ anywhere on the range $x={\mathbf{x}}$ to $x={\mathbf{xend}}$, it will contain xend on exit.
2: $\mathbf{xend}$Real (Kind=nag_wp) Input
On entry: the final value of the independent variable $x$.
If ${\mathbf{xend}}<{\mathbf{x}}$ on entry integration will proceed in the negative direction.
3: $\mathbf{n}$Integer Input
On entry: $\mathit{n}$, the number of differential equations.
Constraint: ${\mathbf{n}}>0$.
4: $\mathbf{y}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input/Output
On entry: the initial values of the solution ${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$.
On exit: 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.
5: $\mathbf{tol}$Real (Kind=nag_wp) Input/Output
On entry: must be set to a positive tolerance for controlling the error in the integration and in the determination of the position where ${y}_{m}=\alpha$.
d02bgf 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}=\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 d02bgf 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 d02bgf with ${\mathbf{tol}}={10.0}^{-p}$ and ${\mathbf{tol}}={10.0}^{-p-1}$ if $p$ correct decimal digits in the solution are required.
Constraint: ${\mathbf{tol}}>0.0$.
On exit: normally unchanged. However if the range from x to the position where ${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 ${\mathbf{tol}}<0.0$, d02bgf should be called again with a positive value of tol whose magnitude is considerably smaller than that of the previous call.
6: $\mathbf{hmax}$Real (Kind=nag_wp) Input
On entry: controls how the sign of ${y}_{m}-\alpha$ is checked.
${\mathbf{hmax}}=0.0$
${y}_{m}-\alpha$ is checked at every internal integration step.
${\mathbf{hmax}}\ne 0.0$
The computed solution is checked for a change in sign of ${y}_{m}-\alpha$ at steps of not greater than $|{\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 ${y}_{m}-\alpha$ are expected within a distance $h$, say, of each other then a suitable value for hmax might be ${\mathbf{hmax}}=h/2$. If only one change of sign in ${y}_{m}-\alpha$ is expected on the range x to xend then ${\mathbf{hmax}}=0.0$ is most appropriate.
7: $\mathbf{m}$Integer Input
On entry: the index $m$ of the component of the solution whose value is to be checked.
Constraint: $1\le {\mathbf{m}}\le {\mathbf{n}}$.
8: $\mathbf{val}$Real (Kind=nag_wp) Input
On entry: the value of $\alpha$ in the equation ${y}_{m}=\alpha$ to be solved for x.
9: $\mathbf{fcn}$Subroutine, supplied by the user. External Procedure
fcn must evaluate the functions ${f}_{i}$ (i.e., the derivatives ${y}_{i}^{\prime }$) for given values of its arguments $x,{y}_{1},\dots ,{y}_{\mathit{n}}$.
The specification of fcn is:
Fortran Interface
 Subroutine fcn ( x, y, f)
 Real (Kind=nag_wp), Intent (In) :: x, y(*) Real (Kind=nag_wp), Intent (Out) :: f(*)
 void fcn (const double *x, const double y[], double f[])
In the description of the arguments of d02bgf below, $\mathit{n}$ denotes the actual value of n in the call of d02bgf.
1: $\mathbf{x}$Real (Kind=nag_wp) Input
On entry: $x$, the value of the argument.
2: $\mathbf{y}\left(*\right)$Real (Kind=nag_wp) array Input
On entry: ${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$, the value of the argument.
3: $\mathbf{f}\left(*\right)$Real (Kind=nag_wp) array Output
On exit: the value of ${f}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$.
fcn must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which d02bgf is called. Arguments denoted as Input must not be changed by this procedure.
Note: fcn should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by d02bgf. If your code inadvertently does return any NaNs or infinities, d02bgf is likely to produce unexpected results.
10: $\mathbf{w}\left({\mathbf{n}},10\right)$Real (Kind=nag_wp) array Workspace
11: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m}}>0$.
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m}}\le {\mathbf{n}}$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}>0$.
On entry, ${\mathbf{tol}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{tol}}>0.0$.
${\mathbf{ifail}}=2$
The value of tol, $⟨\mathit{\text{value}}⟩$, is too small for the function to make any further progress across the integration range. Current value of ${\mathbf{x}}=⟨\mathit{\text{value}}⟩$.
With the given value of tol, no further progress can be made across the integration range from the current point $x={\mathbf{x}}$, or dependence of the error on tol would be lost if further progress across the integration range were attempted (see Section 9 for a discussion of this error exit). The components ${\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={\mathbf{x}}$. No point at which ${y}_{m}-\alpha$ changes sign has been located up to the point $x={\mathbf{x}}$.
${\mathbf{ifail}}=3$
The value of tol, $⟨\mathit{\text{value}}⟩$, is too small for the function to take an initial step.
${\mathbf{ifail}}=4$
No change in sign of the function ${y}_{m}-\alpha$ was detected in the integration range.
${\mathbf{ifail}}=5$
An internal error has occurred in this routine. Check the routine call and any array sizes. If the call is correct then please contact NAG for assistance.
${\mathbf{ifail}}=6$
A serious error occurred in a call to the internal integrator.
The error code internally was $⟨\mathit{\text{value}}⟩$. Please contact NAG.
${\mathbf{ifail}}=7$
Unexpected internal error in call to interpolation routine.
The interpolation routine returned error flag $⟨\mathit{\text{value}}⟩$.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

The accuracy depends on tol, on the mathematical properties of the differential system, on the position where ${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 ${\mathbf{ifail}}={\mathbf{2}}$ or ${\mathbf{3}}$ is possible.

## 8Parallelism and Performance

d02bgf is not threaded in any implementation.

The time taken by d02bgf 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×\mathit{n}$ where $a$ and $b$ are machine-dependent computing times.
For some problems it is possible that d02bgf will exit with ${\mathbf{ifail}}={\mathbf{4}}$ due to inaccuracy of the computed value ${y}_{m}$. For example, consider a case where the component ${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}$ and the (two) positions where ${y}_{m}=\alpha$ are all in the same step and so the position where ${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}$ is expected to equal $\alpha$, for example $|{\mathbf{xend}}-{\mathbf{x}}|$ should be made about $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}$ at xend, then inaccuracy is not a likely source of error.
If d02bgf 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 routine, the system may be very stiff (see below) or so badly scaled that it cannot be solved to the required accuracy.
If d02bgf 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:
1. (a)in the region of a singularity (infinite value) of the solution, the routine will usually stop with ${\mathbf{ifail}}={\mathbf{2}}$, unless overflow occurs first. If overflow occurs using d02bgf, routine d02pff 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;
2. (b)for ‘stiff’ equations, where the solution contains rapidly decaying components the routine will use very small steps in $x$ (internally to d02bgf) 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 the method d02ejf which uses a Backward Differentiation Formula. To determine whether a problem is stiff, d02pef 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 d02cjf which uses an Adams' method.
For problems for which d02bgf is not sufficiently general, you should consider the routines d02bhf and d02pff. Routine d02bhf can be used to solve an equation involving the components ${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 d02bgf if the component whose value is to be determined is very small in modulus on the integration range. d02bhf can always be used in place of d02bgf, but will usually be computationally more expensive for solving the same problem. d02pff is a more general routine with many facilities including a more general error control criterion. d02pff can be combined with the root-finder c05azf and the interpolation routine d02psf to solve equations involving ${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$ and their derivatives.
This routine is only intended to be used to locate the first zero of the function ${y}_{m}-\alpha$. If later zeros are required you are strongly advised to construct your own more general root-finding routines as discussed above.

## 10Example

This example finds the value ${\mathbf{x}}>0.0$ where $y=0.0$, where $y$, $v$, $\varphi$, are defined by
 $y′=tan⁡ϕ v′= -0.032tan⁡ϕv- 0.02v cos⁡ϕ ϕ′= -0.032v2$
and where at ${\mathbf{x}}=0.0$ we are given $y=0.5$, $v=0.5$ and $\varphi =\pi /5$. We write $y={\mathbf{y}}\left(1\right)$, $v={\mathbf{y}}\left(2\right)$ and $\varphi ={\mathbf{y}}\left(3\right)$ and we set ${\mathbf{tol}}=\text{1.0E−4}$ and ${\mathbf{tol}}=\text{1.0E−5}$ in turn so that we can compare the solutions obtained. We expect the solution ${\mathbf{x}}\simeq 7.3$ and we set ${\mathbf{xend}}=10.0$ so that the point where $y=0.0$ is not too near the end of the range of integration. The initial values and range are read from a data file.

### 10.1Program Text

Program Text (d02bgfe.f90)

### 10.2Program Data

Program Data (d02bgfe.d)

### 10.3Program Results

Program Results (d02bgfe.r)