# NAG FL Interfaced02haf (bvp_​shoot_​bval)

## 1Purpose

d02haf solves a two-point boundary value problem for a system of ordinary differential equations, using a Runge–Kutta–Merson method and a Newton iteration in a shooting and matching technique.

## 2Specification

Fortran Interface
 Subroutine d02haf ( u, v, n, a, b, tol, fcn, soln, m1, w, sdw,
 Integer, Intent (In) :: n, m1, sdw Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: v(n,2), a, b, tol Real (Kind=nag_wp), Intent (Inout) :: u(n,2) Real (Kind=nag_wp), Intent (Out) :: soln(n,m1), w(n,sdw) External :: fcn
#include <nag.h>
 void d02haf_ (double u[], const double v[], const Integer *n, const double *a, const double *b, const double *tol, void (NAG_CALL *fcn)(const double *x, const double y[], double f[]),double soln[], const Integer *m1, double w[], const Integer *sdw, Integer *ifail)
The routine may be called by the names d02haf or nagf_ode_bvp_shoot_bval.

## 3Description

d02haf solves a two-point boundary value problem for a system of $\mathit{n}$ ordinary differential equations in the range $a,b$. The system is written in the form:
 $yi′=fix,y1,y2,…,yn, i=1,2,…,n$ (1)
and the derivatives ${f}_{i}$ are evaluated by fcn. Initially, $\mathit{n}$ boundary values of the variables ${y}_{i}$ must be specified, some at $a$ and some at $b$. You must supply estimates of the remaining $\mathit{n}$ boundary values (called parameters below); the subroutine corrects these by a form of Newton iteration. It also calculates the complete solution on an equispaced mesh if required.
Starting from the known and estimated values of ${y}_{i}$ at $a$, the subroutine integrates the equations from $a$ to $b$ (using a Runge–Kutta–Merson method). The differences between the values of ${y}_{i}$ at $b$ from integration and those specified initially should be zero for the true solution. (These differences are called residuals below.) The subroutine uses a generalized Newton method to reduce the residuals to zero, by calculating corrections to the estimated boundary values. This process is repeated iteratively until convergence is obtained, or until the routine can no longer reduce the residuals. See Hall and Watt (1976) for a simple discussion of shooting and matching techniques.

## 4References

Hall G and Watt J M (ed.) (1976) Modern Numerical Methods for Ordinary Differential Equations Clarendon Press, Oxford

## 5Arguments

1: $\mathbf{u}\left({\mathbf{n}},2\right)$Real (Kind=nag_wp) array Input/Output
On entry: ${\mathbf{u}}\left(\mathit{i},1\right)$ must be set to the known or estimated value of ${y}_{\mathit{i}}$ at $a$ and ${\mathbf{u}}\left(\mathit{i},2\right)$ must be set to the known or estimated value of ${y}_{\mathit{i}}$ at $b$, for $\mathit{i}=1,2,\dots ,\mathit{n}$.
On exit: the known values unaltered, and corrected values of the estimates, unless an error has occurred. If an error has occurred, u contains the known values and the latest values of the estimates.
2: $\mathbf{v}\left({\mathbf{n}},2\right)$Real (Kind=nag_wp) array Input
On entry: ${\mathbf{v}}\left(\mathit{i},\mathit{j}\right)$ must be set to $0.0$ if ${\mathbf{u}}\left(\mathit{i},\mathit{j}\right)$ is a known value and to $1.0$ if ${\mathbf{u}}\left(\mathit{i},\mathit{j}\right)$ is an estimated value, for $\mathit{i}=1,2,\dots ,\mathit{n}$ and $\mathit{j}=1,2$.
Constraint: precisely $\mathit{n}$ of the ${\mathbf{v}}\left(i,j\right)$ must be set to $0.0$, i.e., precisely $\mathit{n}$ of the ${\mathbf{u}}\left(i,j\right)$ must be known values, and these must not be all at $a$ or all at $b$.
3: $\mathbf{n}$Integer Input
On entry: $\mathit{n}$, the number of equations.
Constraint: ${\mathbf{n}}\ge 1$.
4: $\mathbf{a}$Real (Kind=nag_wp) Input
On entry: $a$, the initial point of the interval of integration.
5: $\mathbf{b}$Real (Kind=nag_wp) Input
On entry: $b$, the final point of the interval of integration.
6: $\mathbf{tol}$Real (Kind=nag_wp) Input
On entry: must be set to a small quantity suitable for:
1. (a)testing the local error in ${y}_{i}$ during integration,
2. (b)testing for the convergence of ${y}_{i}$ at $b$,
3. (c)calculating the perturbation in estimated boundary values for ${y}_{i}$, which are used to obtain the approximate derivatives of the residuals for use in the Newton iteration.
Constraint: ${\mathbf{tol}}>0.0$.
7: $\mathbf{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:
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 d02haf below, $\mathit{n}$ denotes the actual value of n in the call of d02haf.
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 values of ${f}_{\mathit{i}}\left(x\right)$, 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 d02haf 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 d02haf. If your code inadvertently does return any NaNs or infinities, d02haf is likely to produce unexpected results.
8: $\mathbf{soln}\left({\mathbf{n}},{\mathbf{m1}}\right)$Real (Kind=nag_wp) array Output
On exit: the solution when ${\mathbf{m1}}>1$.
9: $\mathbf{m1}$Integer Input
On entry: a value which controls output.
${\mathbf{m1}}=1$
The final solution is not evaluated.
${\mathbf{m1}}>1$
The final values of ${y}_{\mathit{i}}$ at interval $\left(b-a\right)/\left({\mathbf{m1}}-1\right)$ are calculated and stored in the array soln by columns, starting with values ${y}_{\mathit{i}}$ at $a$ stored in ${\mathbf{soln}}\left(\mathit{i},1\right)$, for $\mathit{i}=1,2,\dots ,\mathit{n}$.
Constraint: ${\mathbf{m1}}\ge 1$.
10: $\mathbf{w}\left({\mathbf{n}},{\mathbf{sdw}}\right)$Real (Kind=nag_wp) array Output
On exit: if ${\mathbf{ifail}}={\mathbf{2}}$, ${\mathbf{3}}$, ${\mathbf{4}}$ or ${\mathbf{5}}$, ${\mathbf{w}}\left(\mathit{i},1\right)$, for $\mathit{i}=1,2,\dots ,\mathit{n}$, contains the solution at the point where the integration fails and the point of failure is returned in ${\mathbf{w}}\left(1,2\right)$.
11: $\mathbf{sdw}$Integer Input
On entry: the second dimension of the array w as declared in the (sub)program from which d02haf is called.
Constraint: ${\mathbf{sdw}}\ge 3{\mathbf{n}}+17+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(11,{\mathbf{n}}\right)$.
12: $\mathbf{ifail}$Integer Input/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 4 in the Introduction to the NAG Library FL Interface).
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).

## 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, incorrect number of boundary values were flagged as known.
Number flagged as known: $〈\mathit{\text{value}}〉$, but number should be $〈\mathit{\text{value}}〉$.
On entry, ${\mathbf{m1}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m1}}\ge 1$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 1$.
On entry, ${\mathbf{sdw}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{sdw}}\ge 3×{\mathbf{n}}+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(28,{\mathbf{n}}+17\right)$; that is, $〈\mathit{\text{value}}〉$.
On entry, ${\mathbf{tol}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{tol}}>0.0$.
On entry all left-hand boundary values were flagged as known.
On entry no left-hand boundary values were flagged as known.
${\mathbf{ifail}}=2$
In the integration with initial or final parameters, the step size was reduced too far for the integration to proceed. Either this routine is not a suitable method for solving the problem, or the initial choice of parameters is very poor.
${\mathbf{ifail}}=3$
In the integration with initial or final parameters, a suitable initial step could not be found. Either this routine is not suitable for solving the problem, or the initial choice of parameters is very poor.
${\mathbf{ifail}}=4$
An initial step-length could be found for integration to proceed with the current parameters.
${\mathbf{ifail}}=5$
The step-length required to calculate the Jacobian to sufficient accuracy is too small
${\mathbf{ifail}}=6$
The Jacobian has an insignificant column. Make sure that the solution vector depends on all the parameters.
${\mathbf{ifail}}=7$
An internal singular value decomposition has failed.
This error can be avoided by changing the initial parameter estimates.
${\mathbf{ifail}}=8$
The Newton iteration has failed to converge.
This can indicate a poor initial choice of parameters or a very difficult problem.
Consider varying elements of the parameter convergence control if the residuals are small; otherwise vary initial parameter estimates.
${\mathbf{ifail}}=10$
${\mathbf{ifail}}=11$
${\mathbf{ifail}}=12$
${\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

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 tol.

## 8Parallelism and Performance

d02haf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

The time taken by d02haf 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 it occurs in the routine, the error argument tol is used in ‘mixed’ form; that is tol always occurs in expressions of the form ${\mathbf{tol}}×\left(1+\left|{y}_{i}\right|\right)$. 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 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 unit numbers on which this output is to appear by calls of x04aaf (for error messages) or x04abf (for monitoring information) – see Section 10 for an example. Otherwise the default unit 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, you may consult the specification of d02saf, and especially the description of the argument monit there.
The computing time for integrating the differential equations can sometimes depend critically on the quality of the initial estimates. 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 should be checked for errors. If no errors can be found, an independent attempt should be made to improve the initial estimates. In practical problems it is not uncommon for the differential equation to have a singular point at one or both ends of the range. Suppose $a$ is a singular point; then the derivatives ${y}_{i}^{\prime }$ in (1) (in Section 3) cannot be evaluated at $a$, usually because one or more of the expressions for ${f}_{i}$ give overflow. In such a case it is necessary for you to take $a$ a short distance away from the singularity, and to find values for ${y}_{i}$ at the new value of $a$ (e.g., use the first one or two terms of an analytical (power series) solution). You should experiment with the new position of $a$; if it is taken too close to the singular point, the derivatives ${f}_{i}$ will be inaccurate, and the routine may sometimes fail with ${\mathbf{ifail}}={\mathbf{2}}$ or ${\mathbf{3}}$ or, in extreme cases, with an overflow condition. A more general treatment of singular solutions is provided by the subroutine d02hbf.
Another difficulty which often arises in practice is the case when one end of the range, $b$ say, is at infinity. You must approximate the end point by taking a finite value for $b$, which is obtained by estimating where the solution will reach its asymptotic state. The estimate can be checked by repeating the calculation with a larger value of $b$. If $b$ is very large, and if the matching point is also at $b$, the numerical solution may suffer a considerable loss of accuracy in integrating across the range, and the program may fail with ${\mathbf{ifail}}={\mathbf{6}}$ or ${\mathbf{8}}$. (In the former case, solutions from all initial values at $a$ are tending to the same curve at infinity.) The simplest remedy is to try to solve the equations with a smaller value of $b$, and then to increase $b$ in stages, using each solution to give boundary value estimates for the next calculation. For problems where some terms in the asymptotic form of the solution are known, d02hbf will be more successful.
If the unknown quantities are not boundary values, but are eigenvalues or the length of the range or some other parameters occurring in the differential equations, d02hbf may be used.

## 10Example

This example finds the angle at which a projectile must be fired for a given range.
The differential equations are:
 $y′ = tan⁡ϕ v′ = -0.032 tan⁡ϕ v - 0.02v cos⁡ϕ ϕ′ = -0.032 v2 ,$
with the following boundary conditions:
 $y= 0, v= 0.5 at x= 0, y= 0 at x= 5.$
The remaining boundary conditions are estimated as:
 $ϕ=1.15 at x=0, ϕ=1.2, v=0.46 at x=5.$
We write $y=\mathrm{Z}\left(1\right)$, $v=\mathrm{Z}\left(2\right)$, $\varphi =\mathrm{Z}\left(3\right)$. To check the accuracy of the results the problem is solved twice with ${\mathbf{tol}}=5.\text{0E−3}$ and $\text{5.0E−4}$ respectively. Note the call to x04abf before the call to d02haf.

### 10.1Program Text

Program Text (d02hafe.f90)

### 10.2Program Data

Program Data (d02hafe.d)

### 10.3Program Results

Program Results (d02hafe.r)