D02 Chapter Contents
D02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentD02HBF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

D02HBF solves a two-point boundary value problem for a system of ordinary differential equations, using initial value techniques and Newton iteration; it generalizes subroutine D02HAF to include the case where parameters other than boundary values are to be determined.

## 2  Specification

 SUBROUTINE D02HBF ( P, N1, PE, E, N, SOLN, M1, FCN, BC, RANGE, W, SDW, IFAIL)
 INTEGER N1, N, M1, SDW, IFAIL REAL (KIND=nag_wp) P(N1), PE(N1), E(N), SOLN(N,M1), W(N,SDW) EXTERNAL FCN, BC, RANGE

## 3  Description

D02HBF solves a two-point boundary value problem by determining the unknown parameters ${p}_{1},{p}_{2},\dots ,{p}_{{\mathit{n}}_{1}}$ of the problem. These parameters may be, but need not be, boundary values; they may include eigenvalue parameters in the coefficients of the differential equations, length of the range of integration, etc. The notation and methods used are similar to those of D02HAF and you are advised to study this first. (The parameters ${p}_{1},{p}_{2},\dots ,{p}_{{\mathit{n}}_{1}}$ correspond precisely to the unknown boundary conditions in D02HAF.) It is assumed that we have a system of $\mathit{n}$ first-order ordinary differential equations of the form:
 $dyi dx =fix,y1,y2,…,yn, i=1,2,…,n,$
and that the derivatives ${f}_{i}$ are evaluated by FCN. The system, including the boundary conditions given by BC and the range of integration given by RANGE, involves the ${\mathit{n}}_{1}$ unknown parameters ${p}_{1},{p}_{2},\dots ,{p}_{{\mathit{n}}_{1}}$ which are to be determined, and for which initial estimates must be supplied. The number of unknown parameters ${\mathit{n}}_{1}$ must not exceed the number of equations $\mathit{n}$. If ${\mathit{n}}_{1}<\mathit{n}$, we assume that $\left(\mathit{n}-{\mathit{n}}_{1}\right)$ equations of the system are not involved in the matching process. These are usually referred to as ‘driving equations’; they are independent of the parameters and of the solutions of the other ${\mathit{n}}_{1}$ equations. In numbering the equations for FCN, the driving equations must be put first.
The estimated values of the parameters are corrected by a form of Newton iteration. The Newton correction on each iteration is calculated using a Jacobian matrix whose $\left(i,j\right)$th element depends on the derivative of the $i$th component of the solution, ${y}_{i}$, with respect to the $j$th parameter, ${p}_{j}$. This matrix is calculated by a simple numerical differentiation technique which requires ${\mathit{n}}_{1}$ evaluations of the differential system.
If the parameter IFAIL is set appropriately, the routine automatically prints messages to inform you of the flow of the calculation. These messages are discussed in detail in Section 8.
D02HBF is a simplified version of D02SAF which is described in detail in Gladwell (1979).

## 4  References

Gladwell I (1979) The development of the boundary value codes in the ordinary differential equations chapter of the NAG Library Codes for Boundary Value Problems in Ordinary Differential Equations. Lecture Notes in Computer Science (eds B Childs, M Scott, J W Daniel, E Denman and P Nelson) 76 Springer–Verlag

## 5  Parameters

You are strongly recommended to read Sections 3 and 8 in conjunction with this section.
1:     P(N1) – REAL (KIND=nag_wp) arrayInput/Output
On entry: an estimate for the $\mathit{i}$th parameter, ${p}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,{\mathit{n}}_{1}$.
On exit: the corrected value for the $i$th parameter, unless an error has occurred, when it contains the last calculated value of the parameter.
2:     N1 – INTEGERInput
On entry: ${\mathit{n}}_{1}$, the number of parameters.
Constraint: $1\le {\mathbf{N1}}\le {\mathbf{N}}$.
3:     PE(N1) – REAL (KIND=nag_wp) arrayInput
On entry: the elements of PE must be given small positive values. The element ${\mathbf{PE}}\left(i\right)$ is used
 (i) in the convergence test on the $i$th parameter in the Newton iteration, and (ii) in perturbing the $i$th parameter when approximating the derivatives of the components of the solution with respect to this parameter for use in the Newton iteration.
The elements ${\mathbf{PE}}\left(i\right)$ should not be chosen too small. They should usually be several orders of magnitude larger than machine precision.
Constraint: ${\mathbf{PE}}\left(\mathit{i}\right)>0.0$, for $\mathit{i}=1,2,\dots ,{\mathbf{N1}}$.
4:     E(N) – REAL (KIND=nag_wp) arrayInput
On entry: the elements of E must be given positive values. The element ${\mathbf{E}}\left(i\right)$ is used in the bound on the local error in the $i$th component of the solution ${y}_{i}$ during integration.
The elements ${\mathbf{E}}\left(i\right)$ should not be chosen too small. They should usually be several orders of magnitude larger than machine precision.
Constraint: ${\mathbf{E}}\left(\mathit{i}\right)>0.0$, for $\mathit{i}=1,2,\dots ,{\mathbf{N}}$.
5:     N – INTEGERInput
On entry: $\mathit{n}$, the total number of differential equations.
Constraint: ${\mathbf{N}}\ge 2$.
6:     SOLN(N,M1) – REAL (KIND=nag_wp) arrayOutput
On exit: the solution when ${\mathbf{M1}}>1$.
7:     M1 – INTEGERInput
On entry: a value which controls exit values.
${\mathbf{M1}}=1$
The final solution is not calculated.
${\mathbf{M1}}>1$
The final values of the solution at interval (length of range)/$\left({\mathbf{M1}}-1\right)$ are calculated and stored sequentially in the array SOLN starting with the values of the solutions evaluated at the first end point (see RANGE) stored in the first column of SOLN.
Constraint: ${\mathbf{M1}}\ge 1$.
8:     FCN – SUBROUTINE, supplied by the user.External Procedure
FCN must evaluate the functions ${f}_{\mathit{i}}$ (i.e., the derivatives ${y}_{\mathit{i}}^{\prime }$), for $\mathit{i}=1,2,\dots ,\mathit{n}$, at a general point $x$.
The specification of FCN is:
 SUBROUTINE FCN ( X, Y, F, P)
 REAL (KIND=nag_wp) X, Y(*), F(*), P(*)
In the description of the parameters of D02HBF below, $\mathit{n}$ and $\mathit{n1}$ denote the numerical values of N and N1 in the call of D02HBF.
1:     X – REAL (KIND=nag_wp)Input
On entry: $x$, the value of the argument.
2:     Y($*$) – REAL (KIND=nag_wp) arrayInput
On entry: ${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$, the value of the argument.
3:     F($*$) – REAL (KIND=nag_wp) arrayOutput
On exit: the value of ${f}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$. The ${f}_{i}$ may depend upon the parameters ${p}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,{\mathit{n}}_{1}$. If there are any driving equations (see Section 3) then these must be numbered first in the ordering of the components of F in FCN.
4:     P($*$) – REAL (KIND=nag_wp) arrayInput
On entry: the current estimate of the parameter ${p}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,{\mathit{n}}_{1}$.
FCN must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02HBF is called. Parameters denoted as Input must not be changed by this procedure.
9:     BC – SUBROUTINE, supplied by the user.External Procedure
BC must place in G1 and G2 the boundary conditions at $a$ and $b$ respectively (see RANGE).
The specification of BC is:
 SUBROUTINE BC ( G1, G2, P)
 REAL (KIND=nag_wp) G1(*), G2(*), P(*)
In the description of the parameters of D02HBF below, $\mathit{n}$ and $\mathit{n1}$ denote the numerical values of N and N1 in the call of D02HBF.
1:     G1($*$) – REAL (KIND=nag_wp) arrayOutput
On exit: the value of ${y}_{\mathit{i}}\left(a\right)$, (where this may be a known value or a function of the parameters ${p}_{\mathit{j}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$ and $\mathit{j}=1,2,\dots ,{\mathit{n}}_{1}$).
2:     G2($*$) – REAL (KIND=nag_wp) arrayOutput
On exit: the value of ${y}_{\mathit{i}}\left(b\right)$, for $\mathit{i}=1,2,\dots ,\mathit{n}$, (where these may be known values or functions of the parameters ${p}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,{\mathit{n}}_{1}$). If $\mathit{n}>{\mathit{n}}_{1}$, so that there are some driving equations, then the first $\mathit{n}-{\mathit{n}}_{1}$ values of G2 need not be set since they are never used.
3:     P($*$) – REAL (KIND=nag_wp) arrayInput
On entry: an estimate of the parameter ${p}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,{\mathit{n}}_{1}$.
BC must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02HBF is called. Parameters denoted as Input must not be changed by this procedure.
10:   RANGE – SUBROUTINE, supplied by the user.External Procedure
RANGE must evaluate the boundary points $a$ and $b$, each of which may depend on the parameters ${p}_{1},{p}_{2},\dots ,{p}_{{\mathit{n}}_{1}}$. The integrations in the shooting method are always from $a$ to $b$.
The specification of RANGE is:
 SUBROUTINE RANGE ( A, B, P)
 REAL (KIND=nag_wp) A, B, P(*)
In the description of the parameters of D02HBF below, $\mathit{n1}$ denotes the actual value of N1 in the call of D02HBF.
1:     A – REAL (KIND=nag_wp)Output
On exit: $a$, one of the boundary points.
2:     B – REAL (KIND=nag_wp)Output
On exit: the second boundary point, $b$. Note that ${\mathbf{B}}>{\mathbf{A}}$ forces the direction of integration to be that of increasing $x$. If A and B are interchanged the direction of integration is reversed.
3:     P($*$) – REAL (KIND=nag_wp) arrayInput
On entry: the current estimate of the $\mathit{i}$th parameter, ${p}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,{\mathit{n}}_{1}$.
RANGE must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02HBF is called. Parameters denoted as Input must not be changed by this procedure.
11:   W(N,SDW) – REAL (KIND=nag_wp) arrayOutput
Used mainly for workspace.
On exit: with ${\mathbf{IFAIL}}={\mathbf{2}}$, ${\mathbf{3}}$, ${\mathbf{4}}$ or ${\mathbf{5}}$ (see Section 6), ${\mathbf{W}}\left(\mathit{i},1\right)$, for $\mathit{i}=1,2,\dots ,\mathit{n}$, contains the solution at the point $x$ when the error occurred. ${\mathbf{W}}\left(1,2\right)$ contains $x$.
12:   SDW – INTEGERInput
On entry: the second dimension of the array W as declared in the (sub)program from which D02HBF is called.
Constraint: ${\mathbf{SDW}}\ge 3{\mathbf{N}}+14+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(11,{\mathbf{N}}\right)$.
13:   IFAIL – INTEGERInput/Output
For this routine, the normal use of IFAIL is extended to control the printing of error and warning messages as well as specifying hard or soft failure (see Section 3.3 in the Essential Introduction).
On entry: IFAIL must be set to a value with the decimal expansion $\mathit{cba}$, where each of the decimal digits $c$, $b$ and $a$ must have a value of $0$ or $1$.
 $a=0$ specifies hard failure, otherwise soft failure; $b=0$ suppresses error messages, otherwise error messages will be printed (see Section 6); $c=0$ suppresses warning messages, otherwise warning messages will be printed (see Section 6).
The recommended value for inexperienced users is $110$ (i.e., hard failure with all messages printed).
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{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$
One or more of the parameters N, N1, M1, SDW, E or PE is incorrectly set.
${\mathbf{IFAIL}}=2$
The step length for the integration became too short whilst calculating the residual (see Section 8).
${\mathbf{IFAIL}}=3$
No initial step length could be chosen for the integration whilst calculating the residual.
Note: ${\mathbf{IFAIL}}={\mathbf{2}}$ or ${\mathbf{3}}$ can occur due to choosing too small a value for E or due to choosing the wrong direction of integration. Try varying E and interchanging $a$ and $b$. These error exits can also occur for very poor initial choices of the parameters in the array P and, in extreme cases, because D02HBF cannot be used to solve the problem posed.
${\mathbf{IFAIL}}=4$
As for ${\mathbf{IFAIL}}={\mathbf{2}}$ but the error occurred when calculating the Jacobian.
${\mathbf{IFAIL}}=5$
As for ${\mathbf{IFAIL}}={\mathbf{3}}$ but the error occurred when calculating the Jacobian.
${\mathbf{IFAIL}}=6$
The calculated Jacobian has an insignificant column. This can occur because a parameter ${p}_{i}$ is incorrectly entered when posing the problem.
Note: ${\mathbf{IFAIL}}={\mathbf{4}}$, ${\mathbf{5}}$ or ${\mathbf{6}}$ usually indicate a badly scaled problem. You may vary the size of PE. Otherwise the use of the more general D02SAF which affords more control over the calculations is advised.
${\mathbf{IFAIL}}=7$
The linear algebra routine used (F08KBF (DGESVD)) has failed. This error exit should not occur and can be avoided by changing the initial estimates ${p}_{i}$.
${\mathbf{IFAIL}}=8$
The Newton iteration has failed to converge. This can indicate a poor initial choice of parameters ${p}_{i}$ or a very difficult problem. Consider varying the elements ${\mathbf{PE}}\left(i\right)$ if the residuals are small in the monitoring output. If the residuals are large, try varying the initial parameters ${p}_{i}$.
${\mathbf{IFAIL}}=9$
${\mathbf{IFAIL}}=10$
${\mathbf{IFAIL}}=11$
${\mathbf{IFAIL}}=12$
${\mathbf{IFAIL}}=13$
Indicates that a serious error has occurred in an internal call. Check all array subscripts and subroutine parameter lists in the call to D02HBF. Seek expert help.

## 7  Accuracy

If the process converges, the accuracy to which the unknown parameters are determined is usually close to that specified by you; the solution, if requested, may be determined to a required accuracy by varying E.

## 8  Further Comments

The time taken by D02HBF depends on the complexity of the system, and on the number of iterations required. In practice, integration of the differential equations is by far the most costly process involved.
Wherever they occur in the routine, the error parameters contained in the arrays E and PE are used in ‘mixed’ form; that is ${\mathbf{E}}\left(i\right)$ always occurs in expressions of the form
 $Ei×1+yi$
and ${\mathbf{PE}}\left(i\right)$ always occurs in expressions of the form
 $PEi×1+pi.$
Though not ideal for every application, it is expected that this mixture of absolute and relative error testing will be adequate for most purposes.
You may determine a suitable direction of integration $a$ to $b$ and suitable values for ${\mathbf{E}}\left(i\right)$ by integrations with D02PEF. The best direction of integration is usually the direction of decreasing solutions. You are strongly recommended to set IFAIL to obtain self-explanatory error messages, and also monitoring information about the course of the computation. You may select the channel numbers on which this output is to appear by calls of X04AAF (for error messages) or X04ABF (for monitoring information) – see Section 9 for an example. Otherwise the default channel numbers will be used, as specified in the Users' Note. The monitoring information produced at each iteration includes the current parameter values, the residuals and $2$-norms: a basic norm and a current norm. At each iteration the aim is to find parameter values which make the current norm less than the basic norm. Both these norms should tend to zero as should the residuals. (They would all be zero if the exact parameters were used as input.) For more details, in particular about the other monitoring information printed, you are advised to consult the specification of D02SAF, and especially the description of the parameter MONIT there.
The computing time for integrating the differential equations can sometimes depend critically on the quality of the initial estimates for the parameters ${p}_{i}$. If it seems that too much computing time is required and, in particular, if the values of the residuals printed by the monitoring routine are much larger than the expected values of the solution at $b$, then the coding of FCN, BC and RANGE should be checked for errors. If no errors can be found, an independent attempt should be made to improve the initial estimates for ${p}_{i}$.
The subroutine can be used to solve a very wide range of problems, for example:
 (a) eigenvalue problems, including problems where the eigenvalue occurs in the boundary conditions; (b) problems where the differential equations depend on some parameters which are to be determined so as to satisfy certain boundary conditions (see Example 2 in Section 9); (c) problems where one of the end points of the range of integration is to be determined as the point where a variable ${y}_{i}$ takes a particular value (see Example 2 in Section 9); (d) singular problems and problems on infinite ranges of integration where the values of the solution at $a$ or $b$ or both are determined by a power series or an asymptotic expansion (or a more complicated expression) and where some of the coefficients in the expression are to be determined (see Example 1 in Section 9); and (e) differential equations with certain terms defined by other independent (driving) differential equations.

## 9  Example

For this routine two examples are presented. There is a single example program for D02HBF, with a main program and the code to solve the two example problems given in Example 1 (EX1) and Example 2 (EX2).
Example 1 (EX1)
This example finds the solution of the differential equation
 $y′′=y3-y′/2x$
on the range $0\le x\le 16$, with boundary conditions $y\left(0\right)=0.1$ and $y\left(16\right)=1/6$. We cannot use the differential equation at $x=0$ because it is singular, so we take a truncated power series expansion
 $yx=1/10+p1×x/10+x/100$
near the origin where ${p}_{1}$ is one of the parameters to be determined. We choose the interval as $\left[0.1,16\right]$ and setting ${p}_{2}={y}^{\prime }\left(16\right)$, we can determine all the boundary conditions. We take $\mathrm{X1}=16$. We write $y={\mathbf{Y}}\left(1\right)$, ${y}^{\prime }={\mathbf{Y}}\left(2\right)$, and estimate $\mathrm{PARAM}\left(1\right)=0.2$, $\mathrm{PARAM}\left(2\right)=0.0$. Note the call to X04ABF before the call to D02HBF.
Example 2 (EX2)
This example finds the gravitational constant ${p}_{1}$ and the range ${p}_{2}$ over which a projectile must be fired to hit the target with a given velocity.
The differential equations are
 $y′ = tan⁡ϕ v′ = -p1sin⁡ϕ+0.00002⁢v2 vcos⁡ϕ ϕ ′ = -p1v2$
on the range $0, with boundary conditions
We write $y={\mathbf{Y}}\left(1\right)$, $v={\mathbf{Y}}\left(2\right)$, $\varphi ={\mathbf{Y}}\left(3\right)$. We estimate ${p}_{1}=\mathrm{PARAM}\left(1\right)=32$, ${p}_{2}=\mathrm{PARAM}\left(2\right)=6000$ and ${p}_{3}=\mathrm{PARAM}\left(3\right)=0.54$ (though this last estimate is not important).

### 9.1  Program Text

Program Text (d02hbfe.f90)

### 9.2  Program Data

Program Data (d02hbfe.d)

### 9.3  Program Results

Program Results (d02hbfe.r)