NAG Library Routine Document
D02SAF
1 Purpose
D02SAF solves a twopoint boundary value problem for a system of firstorder ordinary differential equations with boundary conditions, combined with additional algebraic equations. It uses initial value techniques and a modified Newton iteration in a shooting and matching method.
2 Specification
SUBROUTINE D02SAF ( 
P, M, N, N1, PE, PF, E, DP, NPOINT, SWP, LDSWP, ICOUNT, RANGE, BC, FCN, EQN, CONSTR, YMAX, MONIT, PRSOL, W, LDW, SDW, IFAIL) 
INTEGER 
M, N, N1, NPOINT, LDSWP, ICOUNT, LDW, SDW, IFAIL 
REAL (KIND=nag_wp) 
P(M), PE(M), PF(M), E(N), DP(M), SWP(LDSWP,6), YMAX, W(LDW,SDW) 
LOGICAL 
CONSTR 
EXTERNAL 
RANGE, BC, FCN, EQN, CONSTR, MONIT, PRSOL 

3 Description
D02SAF solves a twopoint boundary value problem for a system of
$n$ firstorder ordinary differential equations with separated boundary conditions by determining certain unknown parameters
${p}_{1},{p}_{2},\dots ,{p}_{m}$. (There may also be additional algebraic equations to be solved in the determination of the parameters and, if so, these equations are defined by
EQN.) The parameters may be, but need not be, boundary values; they may include eigenvalues, parameters in the coefficients of the differential equations, coefficients in series expansions or asymptotic expansions for boundary values, the length of the range of definition of the system of differential equations, etc.
It is assumed that we have a system of
$n$ differential equations of the form
where
$p={\left({p}_{1},{p}_{2},\dots ,{p}_{m}\right)}^{\mathrm{T}}$ is the vector of parameters, and that the derivative
$f$ is evaluated by
FCN. Also,
${n}_{1}$ of the equations are assumed to depend on
$p$. For
${n}_{1}<n$ the
$n{n}_{1}$ equations of the system are not involved in the matching process. These are the driving equations; they should be independent of
$p$ and of the solution of the other
${n}_{1}$ equations. In numbering the equations in
FCN and
BC the driving equations must be put
first (as they naturally occur in most applications). The range of definition [
$a,b$] of the differential equations is defined by
RANGE and may depend on the parameters
${p}_{1},{p}_{2},\dots ,{p}_{m}$ (that is, on
$p$).
RANGE must define the points
${x}_{1},{x}_{2},\dots ,{x}_{{\mathbf{NPOINT}}}$,
${\mathbf{NPOINT}}\ge 2$, which must satisfy
(or a similar relationship with all the inequalities reversed).
If
${\mathbf{NPOINT}}>2$ the points
${x}_{1},{x}_{2},\dots ,{x}_{{\mathbf{NPOINT}}}$ can be used to break up the range of definition. Integration is restarted at each of these points. This means that the differential equations
(1) can be defined differently in each subinterval
$\left[{x}_{\mathit{i}},{x}_{\mathit{i}+1}\right]$, for
$\mathit{i}=1,2,\dots ,{\mathbf{NPOINT}}1$. Also, since initial and maximum integration step sizes can be supplied on each subinterval (via the array
SWP), you can indicate parts of the range
$\left[a,b\right]$ where the solution
$y\left(x\right)$ may be difficult to obtain accurately and can take appropriate action.
The boundary conditions may also depend on the parameters and are applied at
$a={x}_{1}$ and
$b={x}_{{\mathbf{NPOINT}}}$. They are defined (in
BC) in the form
The boundary value problem is solved by determining the unknown parameters
$p$ by a shooting and matching technique. The differential equations are always integrated from
$a$ to
$b$ with initial values
$y\left(a\right)={g}_{1}\left(p\right)$. The solution vector thus obtained at
$x=b$ is subtracted from the vector
${g}_{2}\left(p\right)$ to give the
${n}_{1}$ residuals
${r}_{1}\left(p\right)$, ignoring the first
$n{n}_{1}$, driving equations. Because the direction of integration is always from
$a$ to
$b$, it is unnecessary, in
BC, to supply values for the first
$n{n}_{1}$ boundary values at
$b$, that is the first
$n{n}_{1}$ components of
${g}_{2}$ in
(3). For
${n}_{1}<m$ then
${r}_{1}\left(p\right)$. Together with the
$m{n}_{1}$ equations defined by
EQN,
these give a vector of residuals
$r$, which at the solution,
$p$, must satisfy
These equations are solved by a pseudoNewton iteration which uses a modified singular value decomposition of
$J=\frac{\partial r}{\partial p}$ when solving the linear equations which arise. The Jacobian
$J$ used in Newton's method is obtained by numerical differentiation. The parameters at each Newton iteration are accepted only if the norm
${\Vert {D}^{1}{\stackrel{~}{J}}^{+}r\Vert}_{2}$ is much reduced from its previous value. Here
${\stackrel{~}{J}}^{+}$ is the pseudoinverse, calculated from the singular value decomposition, of a modified version of the Jacobian
$J$ (
${J}^{+}$ is actually the inverse of the Jacobian in wellconditioned cases).
$D$ is a diagonal matrix with
where
PF is an array of floor values.
See
Deuflhard (1974) for further details of the variants of Newton's method used,
Gay (1976) for the modification of the singular value decomposition and
Gladwell (1979) for an overview of the method used.
Two facilities are provided to prevent the pseudoNewton iteration running into difficulty. First, you are permitted to specify constraints on the values of the parameters
$p$ via a
CONSTR. These constraints are only used to prevent the Newton iteration using values for
$p$ which would violate them; that is, they are not used to determine the values of
$p$. Secondly, you are permitted to specify a maximum value
${y}_{\mathrm{max}}$ for
${\Vert y\left(x\right)\Vert}_{\infty}$ at all points in the range
$\left[a,b\right]$. It is intended that this facility be used to prevent machine ‘overflow’ in the integrations of equation
(1) due to poor choices of the parameters
$p$ which might arise during the Newton iteration. When using this facility, it is presumed that you have an estimate of the likely size of
${\Vert y\left(x\right)\Vert}_{\infty}$ at all points
$x\in \left[a,b\right]$.
${y}_{\mathrm{max}}$ should then be chosen rather larger (say by a factor of
$10$) than this estimate.
You are strongly advised to supply a
MONIT (or to call the ‘default’ routine D02HBX, see
MONIT) to monitor the progress of the pseudoNewton iteration. You can output the solution of the problem
$y\left(x\right)$ by supplying a suitable
PRSOL (an example is given in
Section 9 of a routine designed to output the solution at equally spaced points).
D02SAF is designed to try all possible options before admitting failure and returning to you. Provided the routine can start the Newton iteration from the initial point $p$ it will exhaust all the options available to it (though you can override this by specifying a maximum number of iterations to be taken). The fact that all its options have been exhausted is the only error exit from the iteration. Other error exits are possible, however, whilst setting up the Newton iteration and when computing the final solution.
If you require more background information about the solution of boundary value problems by shooting methods you are recommended to read the appropriate chapters of
Hall and Watt (1976), and for a detailed description of D02SAF
Gladwell (1979) is recommended.
4 References
Deuflhard P (1974) A modified Newton method for the solution of illconditioned systems of nonlinear equations with application to multiple shooting Numer. Math. 22 289–315
Gay D (1976) On modifying singular values to solve possibly singular systems of nonlinear equations Working Paper 125 Computer Research Centre, National Bureau for Economics and Management Science, Cambridge, MA
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
Hall G and Watt J M (ed.) (1976) Modern Numerical Methods for Ordinary Differential Equations Clarendon Press, Oxford
5 Parameters
 1: P(M) – REAL (KIND=nag_wp) arrayInput/Output
On entry: ${\mathbf{P}}\left(\mathit{i}\right)$ must be set to an estimate of the $\mathit{i}$th parameter, ${p}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,m$.
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: M – INTEGERInput
On entry: $m$, the number of parameters.
Constraint:
${\mathbf{M}}>0$.
 3: N – INTEGERInput
On entry: $n$, the total number of differential equations.
Constraint:
${\mathbf{N}}>0$.
 4: N1 – INTEGERInput
On entry:
${n}_{1}$, the number of differential equations active in the matching process. The active equations must be placed last in the numbering in
FCN and
BC. The
first ${\mathbf{N}}{\mathbf{N1}}$ equations are used as the driving equations.
Constraint:
${\mathbf{N1}}\le {\mathbf{N}}$, ${\mathbf{N1}}\le {\mathbf{M}}$ and ${\mathbf{N1}}>0$.
 5: PE(M) – REAL (KIND=nag_wp) arrayInput
On entry:
${\mathbf{PE}}\left(\mathit{i}\right)$, for
$\mathit{i}=1,2,\dots ,m$, must be set to a positive value for use in the convergence test in the
$i$th parameter
${p}_{i}$. See the description of
PF for further details.
Constraint:
${\mathbf{PE}}\left(\mathit{i}\right)>0.0$, for $\mathit{i}=1,2,\dots ,m$.
 6: PF(M) – REAL (KIND=nag_wp) arrayInput/Output
On entry:
${\mathbf{PF}}\left(\mathit{i}\right)$, for
$\mathit{i}=1,2,\dots ,m$, should be set to a ‘floor’ value in the convergence test on the
$i$th parameter
${p}_{i}$. If
${\mathbf{PF}}\left(i\right)\le 0.0$ on entry then it is set to the small positive value
$\sqrt{\epsilon}$ (where
$\epsilon $ may in most cases be considered to be
machine precision); otherwise it is used unchanged.
The Newton iteration is presumed to have converged if a full Newton step is taken (
${\mathbf{ISTATE}}=1$ in the specification of
MONIT), the singular values of the Jacobian are not being significantly perturbed (also see
MONIT) and if the Newton correction
${C}_{i}$ satisfies
where
${p}_{i}$ is the current value of the
$i$th parameter. The values
${\mathbf{PF}}\left(i\right)$ are also used in determining the Newton iterates as discussed in
Section 3, see equation
(6).
On exit: the values actually used.
 7: E(N) – REAL (KIND=nag_wp) arrayInput
On entry: values for use in controlling the local error in the integration of the differential equations. If
${\mathit{err}}_{\mathit{i}}$ is an estimate of the local error in
${y}_{\mathit{i}}$, for
$\mathit{i}=1,2,\dots ,n$, then
where
$\epsilon $ may in most cases be considered to be
machine precision.
Suggested value:
${\mathbf{E}}\left(i\right)={10}^{5}$.
Constraint:
${\mathbf{E}}\left(\mathit{i}\right)>0.0$, for $\mathit{i}=1,2,\dots ,{\mathbf{N}}$.
 8: DP(M) – REAL (KIND=nag_wp) arrayInput/Output
On entry: a value to be used in perturbing the parameter
${p}_{i}$ in the numerical differentiation to estimate the Jacobian used in Newton's method. If
${\mathbf{DP}}\left(i\right)=0.0$ on entry, an estimate is made internally by setting
where
${p}_{i}$ is the initial value of the parameter supplied by you and
$\epsilon $ may in most cases be considered to be
machine precision. The estimate of the Jacobian,
$J$, is made using forward differences, that is for each
$\mathit{i}$, for
$\mathit{i}=1,2,\dots ,m$,
${p}_{\mathit{i}}$ is perturbed to
${p}_{\mathit{i}}+{\mathbf{DP}}\left(\mathit{i}\right)$ and the
$\mathit{i}$th column of
$J$ is estimated as
where the other components of
$p$ are unchanged (see
(3) for the notation used). If this fails to produce a Jacobian with significant columns, backward differences are tried by perturbing
${p}_{\mathit{i}}$ to
${p}_{\mathit{i}}{\mathbf{DP}}\left(\mathit{i}\right)$ and if this also fails then central differences are used with
${p}_{\mathit{i}}$ perturbed to
${p}_{\mathit{i}}+10.0\times {\mathbf{DP}}\left(\mathit{i}\right)$. If this also fails then the calculation of the Jacobian is abandoned. If the Jacobian has not previously been calculated then an error exit is taken. If an earlier estimate of the Jacobian is available then the current parameter set,
${p}_{\mathit{i}}$, for
$\mathit{i}=1,2,\dots ,m$, is abandoned in favour of the last parameter set from which useful progress was made and the singular values of the Jacobian used at the point are modified before proceeding with the Newton iteration. You are recommended to use the default value
${\mathbf{DP}}\left(i\right)=0.0$ unless you have prior knowledge of a better choice. If any of the perturbations described are likely to lead to an unfortunate set of parameter values then you should use the LOGICAL FUNCTION
CONSTR to prevent such perturbations (all changes of parameters are checked by a call to
CONSTR).
On exit: the values actually used.
 9: NPOINT – INTEGERInput
On entry: 2 plus the number of break points in the range of definition of the system of differential equations
(1).
Constraint:
${\mathbf{NPOINT}}\ge 2$.
 10: SWP(LDSWP,$6$) – REAL (KIND=nag_wp) arrayInput/Output
On entry:
${\mathbf{SWP}}\left(i,1\right)$ must contain an estimate for an initial step size for integration across the
$i$th subinterval
$\left[{\mathbf{X}}\left(\mathit{i}\right),{\mathbf{X}}\left(\mathit{i}+1\right)\right]$, for
$\mathit{i}=1,2,\dots ,{\mathbf{NPOINT}}1$, (see
RANGE).
${\mathbf{SWP}}\left(i,1\right)$ should have the same sign as
${\mathbf{X}}\left(i+1\right){\mathbf{X}}\left(i\right)$ if it is nonzero. If
${\mathbf{SWP}}\left(i,1\right)=0.0$, on entry, a default value for the initial step size is calculated internally. This is the recommended mode of entry.
${\mathbf{SWP}}\left(i,3\right)$ must contain a lower bound for the modulus of the step size on the
$\mathit{i}$th subinterval $\left[{\mathbf{X}}\left(\mathit{i}\right),{\mathbf{X}}\left(\mathit{i}+1\right)\right]$, for $\mathit{i}=1,2,\dots ,{\mathbf{NPOINT}}1$. If ${\mathbf{SWP}}\left(i,3\right)=0.0$ on entry, a very small default value is used. By setting ${\mathbf{SWP}}\left(i,3\right)>0.0$ but smaller than the expected step sizes (assuming you have some insight into the likely step sizes) expensive integrations with parameters $p$ far from the solution can be avoided.
${\mathbf{SWP}}\left(\mathit{i},2\right)$ must contain an upper bound on the modulus of the step size to be used in the integration on $\left[{\mathbf{X}}\left(\mathit{i}\right),{\mathbf{X}}\left(\mathit{i}+1\right)\right]$, for $\mathit{i}=1,2,\dots ,{\mathbf{NPOINT}}1$. If ${\mathbf{SWP}}\left(i,2\right)=0.0$ on entry no bound is assumed. This is the recommended mode of entry unless the solution is expected to have important features which might be ‘missed’ in the integration if the step size were permitted to be chosen freely.
On exit:
${\mathbf{SWP}}\left(\mathit{i},1\right)$ contains the initial step size used on the last integration on
$\left[{\mathbf{X}}\left(\mathit{i}\right),{\mathbf{X}}\left(\mathit{i}+1\right)\right]$, for
$\mathit{i}=1,2,\dots ,{\mathbf{NPOINT}}1$, (excluding integrations during the calculation of the Jacobian).
${\mathbf{SWP}}\left(\mathit{i},2\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{NPOINT}}1$, is usually unchanged. If the maximum step size ${\mathbf{SWP}}\left(i,2\right)$ is so small or the length of the range $\left[{\mathbf{X}}\left(i\right),{\mathbf{X}}\left(i+1\right)\right]$ is so short that on the last integration the step size was not controlled in the main by the size of the error tolerances ${\mathbf{E}}\left(i\right)$ but by these other factors, then ${\mathbf{SWP}}\left({\mathbf{NPOINT}},2\right)$ is set to the floating point value of $i$ if the problem last occurred in $\left[{\mathbf{X}}\left(i\right),{\mathbf{X}}\left(i+1\right)\right]$. Any results obtained when this value is returned as nonzero should be viewed with caution.
${\mathbf{SWP}}\left(\mathit{i},3\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{NPOINT}}1$, are unchanged.
If an error exit with
${\mathbf{IFAIL}}={\mathbf{4}}$,
${\mathbf{5}}$ or
${\mathbf{6}}$ (see
Section 6) occurs on the integration made from
${\mathbf{X}}\left(i\right)$ to
${\mathbf{X}}\left(i+1\right)$ the floating point value of
$i$ is returned in
${\mathbf{SWP}}\left({\mathbf{NPOINT}},1\right)$. The actual point
$x\in \left[{\mathbf{X}}\left(i\right),{\mathbf{X}}\left(i+1\right)\right]$ where the error occurred is returned in
${\mathbf{SWP}}\left(1,5\right)$ (see also the specification of
W). The floating point value of
NPOINT is returned in
${\mathbf{SWP}}\left({\mathbf{NPOINT}},1\right)$ if the error exit is caused by a call to
BC.
If an error exit occurs when estimating the Jacobian matrix (
${\mathbf{IFAIL}}={\mathbf{7}}$,
${\mathbf{8}}$,
${\mathbf{9}}$,
${\mathbf{10}}$,
${\mathbf{11}}$ or
${\mathbf{12}}$, see
Section 6) and if parameter
${p}_{i}$ was the cause of the failure then on exit
${\mathbf{SWP}}\left({\mathbf{NPOINT}},1\right)$ contains the floating point value of
$i$.
${\mathbf{SWP}}\left(\mathit{i},4\right)$ contains the point ${\mathbf{X}}\left(\mathit{i}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{NPOINT}}$, used at the solution $p$ or at the final values of $p$ if an error occurred.
SWP is also partly used as workspace.
 11: LDSWP – INTEGERInput
On entry: the first dimension of the array
SWP as declared in the (sub)program from which D02SAF is called.
Constraint:
${\mathbf{LDSWP}}\ge {\mathbf{NPOINT}}$.
 12: ICOUNT – INTEGERInput
On entry: an upper bound on the number of Newton iterations. If ${\mathbf{ICOUNT}}=0$ on entry, no check on the number of iterations is made (this is the recommended mode of entry).
Constraint:
${\mathbf{ICOUNT}}\ge 0$.
 13: RANGE – SUBROUTINE, supplied by the user.External Procedure
RANGE must specify the break points
${x}_{\mathit{i}}$, for
$\mathit{i}=1,2,\dots ,{\mathbf{NPOINT}}$, which may depend on the parameters
${p}_{\mathit{j}}$, for
$\mathit{j}=1,2,\dots ,m$.
The specification of
RANGE is:
INTEGER 
NPOINT, M 
REAL (KIND=nag_wp) 
X(NPOINT), P(M) 

 1: X(NPOINT) – REAL (KIND=nag_wp) arrayOutput
On exit: the
$\mathit{i}$th break point, for
$\mathit{i}=1,2,\dots ,{\mathbf{NPOINT}}$. The sequence
$\left({\mathbf{X}}\left(i\right)\right)$ must be strictly monotonic, that is either
or
 2: NPOINT – INTEGERInput
On entry: $2$ plus the number of break points in $\left(a,b\right)$.
 3: P(M) – REAL (KIND=nag_wp) arrayInput
On entry: the current estimate of the
$\mathit{i}$th parameter, for $\mathit{i}=1,2,\dots ,m$.
 4: M – INTEGERInput
On entry: $m$, the number of parameters.
RANGE must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02SAF is called. Parameters denoted as
Input must
not be changed by this procedure.
 14: BC – SUBROUTINE, supplied by the user.External Procedure
BC must place in
G1 and
G2 the boundary conditions at
$a$ and
$b$ respectively.
The specification of
BC is:
INTEGER 
M, N 
REAL (KIND=nag_wp) 
G1(N), G2(N), P(M) 

 1: G1(N) – REAL (KIND=nag_wp) arrayOutput
On exit: the value of
${y}_{\mathit{i}}\left(a\right)$, for $\mathit{i}=1,2,\dots ,n$, (where this may be a known value or a function of the parameters
${p}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,m$).
 2: G2(N) – REAL (KIND=nag_wp) arrayOutput
On exit: the value of
${y}_{\mathit{i}}\left(b\right)$, for
$\mathit{i}=1,2,\dots ,n$, (where these may be known values or functions of the parameters
${p}_{\mathit{j}}$, for
$\mathit{j}=1,2,\dots ,m$). If
$n>{n}_{1}$, so that there are some driving equations, then the first
$n{n}_{1}$ values of
G2 need not be set since they are never used.
 3: P(M) – REAL (KIND=nag_wp) arrayInput
On entry: an estimate of the
$\mathit{i}$th parameter, ${p}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,m$.
 4: M – INTEGERInput
On entry: $m$, the number of parameters.
 5: N – INTEGERInput
On entry: $n$, the number of differential equations.
BC must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02SAF is called. Parameters denoted as
Input must
not be changed by this procedure.
 15: 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 ,n$.
The specification of
FCN is:
SUBROUTINE FCN ( 
X, Y, F, N, P, M, I) 
INTEGER 
N, M, I 
REAL (KIND=nag_wp) 
X, Y(N), F(N), P(M) 

 1: X – REAL (KIND=nag_wp)Input
On entry: $x$, the value of the argument.
 2: Y(N) – 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(N) – REAL (KIND=nag_wp) arrayOutput
On exit: the derivative of
${y}_{\mathit{i}}$, for
$\mathit{i}=1,2,\dots ,n$, evaluated at
$x$.
${\mathbf{F}}\left(i\right)$ may depend upon the parameters
${p}_{\mathit{j}}$, for
$\mathit{j}=1,2,\dots ,m$. If there are any driving equations (see
Section 3) then these must be numbered first in the ordering of the components of
F.
 4: N – INTEGERInput
On entry: $\mathit{n}$, the number of equations.
 5: P(M) – 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 ,m$.
 6: M – INTEGERInput
On entry: $m$, the number of parameters.
 7: I – INTEGERInput
On entry: specifies the subinterval $\left[{x}_{i},{x}_{i+1}\right]$ on which the derivatives are to be evaluated.
FCN must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02SAF is called. Parameters denoted as
Input must
not be changed by this procedure.
 16: EQN – SUBROUTINE, supplied by the NAG Library or the user.External Procedure
EQN is used to describe the additional algebraic equations to be solved in the determination of the parameters,
${p}_{\mathit{i}}$, for
$\mathit{i}=1,2,\dots ,m$. If there are no additional algebraic equations (i.e.,
$m={n}_{1}$) then
EQN is never called and the dummy routine D02HBZ should be used as the actual argument.
The specification of
EQN is:
SUBROUTINE EQN ( 
E, Q, P, M) 
INTEGER 
Q, M 
REAL (KIND=nag_wp) 
E(Q), P(M) 

 1: E(Q) – REAL (KIND=nag_wp) arrayOutput
On exit: the vector of residuals, ${r}_{2}\left(p\right)$, that is the amount by which the current estimates of the parameters fail to satisfy the algebraic equations.
 2: Q – INTEGERInput
On entry: the number of algebraic equations, $m{n}_{1}$.
 3: P(M) – 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 ,m$.
 4: M – INTEGERInput
On entry: $m$, the number of parameters.
EQN must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02SAF is called. Parameters denoted as
Input must
not be changed by this procedure.
 17: CONSTR – LOGICAL FUNCTION, supplied by the user.External Procedure
CONSTR is used to prevent the pseudoNewton iteration running into difficulty.
CONSTR should return the value .TRUE. if the constraints are satisfied by the parameters
${p}_{1},{p}_{2},\dots ,{p}_{m}$. Otherwise
CONSTR should return the value .FALSE.. Usually the dummy function D02HBY, which returns the value .TRUE. at all times, will suffice and in the first instance this is recommended as the actual parameter.
The specification of
CONSTR is:
INTEGER 
M 
REAL (KIND=nag_wp) 
P(M) 

 1: P(M) – REAL (KIND=nag_wp) arrayInput
On entry: an estimate of the
$\mathit{i}$th parameter, ${p}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,m$.
 2: M – INTEGERInput
On entry: $m$, the number of parameters.
CONSTR must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02SAF is called. Parameters denoted as
Input must
not be changed by this procedure.
 18: YMAX – REAL (KIND=nag_wp)Input
On entry: a nonnegative value which is used as a bound on all values ${\Vert y\left(x\right)\Vert}_{\infty}$ where $y\left(x\right)$ is the solution at any point $x$ between ${\mathbf{X}}\left(1\right)$ and ${\mathbf{X}}\left({\mathbf{NPOINT}}\right)$ for the current parameters ${p}_{1},{p}_{2},\dots ,{p}_{m}$. If this bound is exceeded the integration is terminated and the current parameters are rejected. Such a rejection will result in an error exit if it prevents the initial residual or Jacobian, or the final solution, being calculated. If ${\mathbf{YMAX}}=0$ on entry, no bound on the solution $y$ is used; that is the integrations proceed without any checking on the size of ${\Vert y\Vert}_{\infty}$.
 19: MONIT – SUBROUTINE, supplied by the NAG Library or the user.External Procedure
MONIT enables you to monitor the values of various quantities during the calculation. It is called by D02SAF after every calculation of the norm
${\Vert {{\mathbf{D}}}^{1}{\stackrel{~}{J}}^{+}r\Vert}_{2}$ which determines the strategy of the Newton method, every time there is an internal error exit leading to a change of strategy, and before an error exit when calculating the initial Jacobian.
Usually the routine D02HBX will be adequate and you are advised to use this as the actual parameter for
MONIT in the first instance. (In this case a call to
X04ABF must be made before the call of D02SAF.) If no monitoring is required, the dummy routine D02SAS may be used.
The specification of
MONIT is:
INTEGER 
ISTATE, IFLAG, IFAIL1, M 
REAL (KIND=nag_wp) 
P(M), F(M), PNORM, PNORM1, EPS, D(M) 

 1: ISTATE – INTEGERInput
On entry: the state of the Newton iteration.
 ${\mathbf{ISTATE}}=0$
 The calculation of the residual, Jacobian and ${\Vert {{\mathbf{D}}}^{1}{\stackrel{~}{J}}^{+}r\Vert}_{2}$ are taking place.
 ${\mathbf{ISTATE}}=1$ to $5$
 During the Newton iteration a factor of ${2}^{\left({\mathbf{ISTATE}}+1\right)}$ of the Newton step is being used to try to reduce the norm.
 ${\mathbf{ISTATE}}=6$
 The current Newton step has been rejected and the Jacobian is being recalculated.
 ${\mathbf{ISTATE}}=6$ to $1$
 An internal error exit has caused the rejection of the current set of parameter values, $p$. ${\mathbf{ISTATE}}$ is the value which ISTATE would have taken if the error had not occurred.
 ${\mathbf{ISTATE}}=7$
 An internal error exit has occurred when calculating the initial Jacobian.
 2: IFLAG – INTEGERInput
On entry: whether or not the Jacobian being used has been calculated at the beginning of the current iteration. If the Jacobian has been updated then ${\mathbf{IFLAG}}=1$; otherwise ${\mathbf{IFLAG}}=2$. The Jacobian is only calculated when convergence to the current parameter values has been slow.
 3: IFAIL1 – INTEGERInput
On entry: if
$6\le {\mathbf{ISTATE}}\le 1$,
IFAIL1 specifies the
IFAIL error number that would be produced were control returned to you.
IFAIL1 is unspecified for values of
ISTATE outside this range.
 4: P(M) – 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 ,m$.
 5: M – INTEGERInput
On entry: $m$, the number of parameters.
 6: F(M) – REAL (KIND=nag_wp) arrayInput
On entry:
$r$, the residual corresponding to the current parameter values, provided
$1\le {\mathbf{ISTATE}}\le 5$ or
${\mathbf{ISTATE}}=7$.
F is unspecified for other values of
ISTATE.
 7: PNORM – REAL (KIND=nag_wp)Input
On entry: a quantity against which all reductions in norm are currently measured.
 8: PNORM1 – REAL (KIND=nag_wp)Input
On entry:
$p$, the norm of the current parameters. It is set for
$1\le {\mathbf{ISTATE}}\le 5$ and is undefined for other values of
ISTATE.
 9: EPS – REAL (KIND=nag_wp)Input
On entry: gives some indication of the convergence rate. It is the current singular value modification factor (see
Gay (1976)). It is zero initially and whenever convergence is proceeding steadily.
EPS is
${\epsilon}^{3/8}$ or greater (where
$\epsilon $ may in most cases be considered
machine precision) when the singular values of
$J$ are approximately zero or when convergence is not being achieved. The larger the value of
EPS the worse the convergence rate. When
EPS becomes too large the Newton iteration is terminated.
 10: D(M) – REAL (KIND=nag_wp) arrayInput
On entry:
$J$, the singular values of the current modified Jacobian matrix. If
${\mathbf{D}}\left(m\right)$ is small relative to
${\mathbf{D}}\left(1\right)$ for a number of Jacobians corresponding to different parameter values then the computed results should be viewed with suspicion. It could be that the matching equations do not depend significantly on some parameter (which could be due to a programming error in
FCN,
BC,
RANGE or
EQN). Alternatively, the system of differential equations may be very illconditioned when viewed as an initial value problem, in which case D02SAF is unsuitable. This may also be indicated by some singular values being very large. These values of
${\mathbf{D}}\left(\mathit{i}\right)$, for
$\mathit{i}=1,2,\dots ,m$, should not be changed.
MONIT must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02SAF is called. Parameters denoted as
Input must
not be changed by this procedure.
 20: PRSOL – SUBROUTINE, supplied by the NAG Library or the user.External Procedure
PRSOL can be used to obtain values of the solution
$y$ at a selected point
$z$ by integration across the final range
$\left[{\mathbf{X}}\left(1\right),{\mathbf{X}}\left({\mathbf{NPOINT}}\right)\right]$. If no output is required D02HBW can be used as the actual parameter.
The specification of
PRSOL is:
SUBROUTINE PRSOL ( 
Z, Y, N) 
INTEGER 
N 
REAL (KIND=nag_wp) 
Z, Y(N) 

 1: Z – REAL (KIND=nag_wp)Input/Output
On entry: contains
${x}_{1}$ on the first call. On subsequent calls
Z contains its previous output value.
On exit: the next point at which output is required. The new point must be nearer
${\mathbf{X}}\left({\mathbf{NPOINT}}\right)$ than the old.
If
Z is set to a point outside
$\left[{\mathbf{X}}\left(1\right),{\mathbf{X}}\left({\mathbf{NPOINT}}\right)\right]$ the process stops and control returns from D02SAF to the (sub)program from which D02SAF is called. Otherwise the next call to
PRSOL is made by D02SAF at the point
Z, with solution values
${y}_{1},{y}_{2},\dots ,{y}_{n}$ at
Z contained in
Y. If
Z is set to
${\mathbf{X}}\left({\mathbf{NPOINT}}\right)$ exactly, the final call to
PRSOL is made with
${y}_{1},{y}_{2},\dots ,{y}_{n}$ as values of the solution at
${\mathbf{X}}\left({\mathbf{NPOINT}}\right)$ produced by the integration. In general the solution values obtained at
${\mathbf{X}}\left({\mathbf{NPOINT}}\right)$ from
PRSOL will differ from the values obtained at this point by a call to
BC. The difference between the two solutions is the residual
$r$. You are reminded that the points
${\mathbf{X}}\left(1\right),{\mathbf{X}}\left(2\right),\dots ,{\mathbf{X}}\left({\mathbf{NPOINT}}\right)$ are available in the locations
${\mathbf{SWP}}\left(1,4\right),{\mathbf{SWP}}\left(2,4\right),\dots ,{\mathbf{SWP}}\left({\mathbf{NPOINT}},4\right)$ at all times.
 2: Y(N) – REAL (KIND=nag_wp) arrayInput
On entry: the solution value
${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$, at $z$.
 3: N – INTEGERInput
On entry: $\mathit{n}$, the total number of differential equations.
PRSOL must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02SAF is called. Parameters denoted as
Input must
not be changed by this procedure.
 21: W(LDW,SDW) – REAL (KIND=nag_wp) arrayOutput
On exit: in the case of an error exit of the type where the point of failure is returned in
${\mathbf{SWP}}\left(1,5\right)$, the solution at this point of failure is returned in
${\mathbf{W}}\left(\mathit{i},1\right)$, for
$\mathit{i}=1,2,\dots ,n$.
Otherwise
W is used for workspace.
 22: LDW – INTEGERInput
On entry: the first dimension of the array
W as declared in the (sub)program from which D02SAF is called.
Constraint:
${\mathbf{LDW}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{N}},{\mathbf{M}}\right)$.
 23: SDW – INTEGERInput
On entry: the second dimension of the array
W as declared in the (sub)program from which D02SAF is called.
Constraint:
${\mathbf{SDW}}\ge 3\times {\mathbf{M}}+12+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(11,{\mathbf{M}}\right)$.
 24: IFAIL – INTEGERInput/Output

On entry:
IFAIL must be set to
$0$,
$1\text{ or}1$. If you are unfamiliar with this parameter you should refer to
Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value
$1\text{ or}1$ is recommended. If the output of error messages is undesirable, then the value
$1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is
$0$.
When the value $\mathbf{1}\text{ 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).
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,
M,
LDSWP,
NPOINT,
ICOUNT,
LDW,
SDW,
E,
PE or
YMAX is incorrectly set.
 ${\mathbf{IFAIL}}=2$
The constraints have been violated by the initial parameters.
 ${\mathbf{IFAIL}}=3$
The condition
${\mathbf{X}}\left(1\right)<{\mathbf{X}}\left(2\right)<\cdots <{\mathbf{X}}\left({\mathbf{NPOINT}}\right)$ (or
${\mathbf{X}}\left(1\right)>{\mathbf{X}}\left(2\right)>\cdots >{\mathbf{X}}\left({\mathbf{NPOINT}}\right)$) has been violated on a call to
RANGE with the initial parameters.
 ${\mathbf{IFAIL}}=4$
In the integration from ${\mathbf{X}}\left(1\right)$ to ${\mathbf{X}}\left({\mathbf{NPOINT}}\right)$ with the initial or the final parameters, the step size was reduced too far for the integration to proceed. Consider reversing the order of the points ${\mathbf{X}}\left(1\right),{\mathbf{X}}\left(2\right),\dots ,{\mathbf{X}}\left({\mathbf{NPOINT}}\right)$. If this error exit still results, it is likely that D02SAF is not a suitable method for solving the problem, or the initial choice of parameters is very poor, or the accuracy requirement specified by ${\mathbf{E}}\left(\mathit{i}\right)$, for $\mathit{i}=1,2,\dots ,n$, is too stringent.
 ${\mathbf{IFAIL}}=5$
In the integration from ${\mathbf{X}}\left(1\right)$ to ${\mathbf{X}}\left({\mathbf{NPOINT}}\right)$ with the initial or final parameters, an initial step could not be found to start the integration on one of the intervals ${\mathbf{X}}\left(i\right)$ to ${\mathbf{X}}\left(i+1\right)$. Consider reversing the order of the points. If this error exit still results it is likely that D02SAF is not a suitable routine for solving the problem, or the initial choice of parameters is very poor, or the accuracy requirement specified by ${\mathbf{E}}\left(\mathit{i}\right)$, for $\mathit{i}=1,2,\dots ,n$, is much too stringent.
 ${\mathbf{IFAIL}}=6$
In the integration from
${\mathbf{X}}\left(1\right)$ to
${\mathbf{X}}\left({\mathbf{NPOINT}}\right)$ with the initial or final parameters, the solution exceeded
YMAX in magnitude (when
${\mathbf{YMAX}}>0.0$). It is likely that the initial choice of parameters was very poor or
YMAX was incorrectly set.
Note: on an error with
${\mathbf{IFAIL}}={\mathbf{4}}$,
${\mathbf{5}}$ or
${\mathbf{6}}$ with the initial parameters, the interval in which failure occurs is contained in
${\mathbf{SWP}}\left({\mathbf{NPOINT}},1\right)$. If a
MONIT similar to the one in
Section 9 is being used then it is a simple matter to distinguish between errors using the initial and final parameters. None of the error exits
${\mathbf{IFAIL}}={\mathbf{4}}$,
${\mathbf{5}}$ or
${\mathbf{6}}$ should occur on the
final integration (when computing the solution) as this integration has already been performed previously with exactly the same parameters
${p}_{\mathit{i}}$, for
$\mathit{i}=1,2,\dots ,m$. Seek expert help if this error occurs.
 ${\mathbf{IFAIL}}=7$
On calculating the initial approximation to the Jacobian, the constraints were violated.
 ${\mathbf{IFAIL}}=8$
On perturbing the parameters when calculating the initial approximation to the Jacobian, the condition ${\mathbf{X}}\left(1\right)<{\mathbf{X}}\left(2\right)<\cdots <{\mathbf{X}}\left({\mathbf{NPOINT}}\right)$ (or ${\mathbf{X}}\left(1\right)>{\mathbf{X}}\left(2\right)>\cdots >{\mathbf{X}}\left({\mathbf{NPOINT}}\right)$) is violated.
 ${\mathbf{IFAIL}}=9$
On calculating the initial approximation to the Jacobian, the integration step size was reduced too far to make further progress (see ${\mathbf{IFAIL}}={\mathbf{4}}$).
 ${\mathbf{IFAIL}}=10$
On calculating the initial approximation to the Jacobian, the initial integration step size on some interval was too small (see ${\mathbf{IFAIL}}={\mathbf{5}}$).
 ${\mathbf{IFAIL}}=11$
On calculating the initial approximation to the Jacobian, the solution of the system of differential equations exceeded
YMAX in magnitude (when
${\mathbf{YMAX}}>0.0$).
Note: all the error exits
${\mathbf{IFAIL}}={\mathbf{7}}$,
${\mathbf{8}}$,
${\mathbf{9}}$,
${\mathbf{10}}$ and
${\mathbf{11}}$ can be treated by reducing the size of some or all the elements of
DP.
 ${\mathbf{IFAIL}}=12$
On calculating the initial approximation to the Jacobian, a column of the Jacobian is found to be insignificant. This could be due to an element ${\mathbf{DP}}\left(i\right)$ being too small (but nonzero) or the solution having no dependence on one of the parameters (a programming error).
Note: on an error exit with ${\mathbf{IFAIL}}={\mathbf{7}}$, ${\mathbf{8}}$, ${\mathbf{9}}$, ${\mathbf{10}}$, ${\mathbf{11}}$ or ${\mathbf{12}}$, if a perturbation of the parameter ${p}_{i}$ is the cause of the error then ${\mathbf{SWP}}\left({\mathbf{NPOINT}},1\right)$ will contain the floating point value of $i$.
 ${\mathbf{IFAIL}}=13$
After calculating the initial approximation to the Jacobian, the calculation of its singular value decomposition failed. It is likely that the error will never occur as it is usually associated with the Jacobian having multiple singular values. To remedy the error it should only be necessary to change the initial parameters. If the error persists it is likely that the problem has not been correctly formulated.
 ${\mathbf{IFAIL}}=14$
The Newton iteration has failed to converge after exercising all its options. You are strongly recommended to monitor the progress of the iteration via
MONIT. There are many possible reasons for the iteration not converging. Amongst the most likely are:
(a) 
there is no solution; 
(b) 
the initial parameters are too far away from the correct parameters; 
(c) 
the problem is too illconditioned as an initial value problem for Newton's method to choose suitable corrections; 
(d) 
the accuracy requirements for convergence are too restrictive, that is some of the components of PE (and maybe PF) are too small – in this case the final value of this norm output via MONIT will usually be very small; or 
(e) 
the initial parameters are so close to the solution parameters $p$ that the Newton iteration cannot find improved parameters. The norm output by MONIT should be very small. 
 ${\mathbf{IFAIL}}=15$
The number of iterations permitted by
ICOUNT has been exceeded (in the case when
${\mathbf{ICOUNT}}>0$ on entry).
 ${\mathbf{IFAIL}}=16$
 ${\mathbf{IFAIL}}=17$
 ${\mathbf{IFAIL}}=18$
 ${\mathbf{IFAIL}}=19$
These indicate that there has been a serious error in an internal call. Check all subroutine calls and array dimensions. Seek expert help.
7 Accuracy
If the iteration converges, the accuracy to which the unknown parameters are determined is usually close to that specified by you. The accuracy of the solution (output via
PRSOL) depends on the error tolerances
${\mathbf{E}}\left(\mathit{i}\right)$, for
$\mathit{i}=1,2,\dots ,n$. You are strongly recommended to vary all tolerances to check the accuracy of the parameters
$p$ and the solution
$y$.
The time taken by D02SAF depends on the complexity of the system of differential equations and on the number of iterations required. In practice, the integration of the differential system
(1) is usually by far the most costly process involved. The computing time for integrating the differential equations can sometimes depend critically on the quality of the initial estimates for the parameters
$p$. If it seems that too much computing time is required and, in particular, if the values of the residuals (output in
MONIT) are much larger than expected given your knowledge of the expected solution, then the coding of
FCN,
EQN,
RANGE and
BC should be checked for errors. If no errors can be found then an independent attempt should be made to improve the initial estimates
$p$.
In the case of an error exit in the integration of the differential system indicated by
${\mathbf{IFAIL}}={\mathbf{4}}$,
${\mathbf{5}}$,
${\mathbf{9}}$ or
${\mathbf{10}}$ you are strongly recommended to perform trial integrations with
D02PFF to determine the effects of changes of the local error tolerances and of changes to the initial choice of the parameters
${p}_{\mathit{i}}$, for
$\mathit{i}=1,2,\dots ,m$, (that is the initial choice of
$p$).
It is possible that by following the advice given in
Section 6 an error exit with
${\mathbf{IFAIL}}={\mathbf{7}}$,
${\mathbf{8}}$,
${\mathbf{9}}$,
${\mathbf{10}}$ or
${\mathbf{11}}$ might be followed by one with
${\mathbf{IFAIL}}={\mathbf{12}}$ (or viceversa) where the advice given is the opposite. If you are unable to refine the choice of
${\mathbf{DP}}\left(\mathit{i}\right)$, for
$\mathit{i}=1,2,\dots ,n$, such that both these types of exits are avoided then the problem should be rescaled if possible or the method must be abandoned.
The choice of the ‘floor’ values
${\mathbf{PF}}\left(\mathit{i}\right)$, for $\mathit{i}=1,2,\dots ,m$, may be critical in the convergence of the Newton iteration. For each value $i$, the initial choice of ${p}_{i}$ and the choice of ${\mathbf{PF}}\left(i\right)$ should not both be very small unless it is expected that the final parameter ${p}_{i}$ will be very small and that it should be determined accurately in a relative sense.
For many problems it is critical that a good initial estimate be found for the parameters
$p$ or the iteration will not converge or may even break down with an error exit. There are many mathematical techniques which obtain good initial estimates for
$p$ in simple cases but which may fail to produce useful estimates in harder cases. If no such technique is available it is recommended that you try a continuation (homotopy) technique preferably based on a physical parameter (e.g., the Reynolds or Prandtl number is often a suitable continuation parameter). In a continuation method a sequence of problems is solved, one for each choice of the continuation parameter, starting with the problem of interest. At each stage the parameters
$p$ calculated at earlier stages are used to compute a good initial estimate for the parameters at the current stage (see
Hall and Watt (1976) for more details).
9 Example
This example intends to illustrate the use of the break point and equation solving facilities of D02SAF. Most of the facilities which are common to D02SAF and
D02HBF are illustrated in the example in the specification of
D02HBF (which should also be consulted).
The program solves a projectile problem in two media determining the position of change of media, ${p}_{3}$, and the gravity and viscosity in the second medium (${p}_{2}$ represents gravity and ${p}_{4}$ represents viscosity).
9.1 Program Text
Program Text (d02safe.f90)
9.2 Program Data
Program Data (d02safe.d)
9.3 Program Results
Program Results (d02safe.r)