Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_ode_bvp_fd_nonlin_gen (d02ra)

## Purpose

nag_ode_bvp_fd_nonlin_gen (d02ra) solves a two-point boundary value problem with general boundary conditions for a system of ordinary differential equations, using a deferred correction technique and Newton iteration.

## Syntax

[np, x, y, abt, deleps, ifail] = d02ra(np, numbeg, nummix, tol, init, x, y, fcn, g, ijac, jacobf, jacobg, deleps, jaceps, jacgep, 'n', n, 'mnp', mnp)
[np, x, y, abt, deleps, ifail] = nag_ode_bvp_fd_nonlin_gen(np, numbeg, nummix, tol, init, x, y, fcn, g, ijac, jacobf, jacobg, deleps, jaceps, jacgep, 'n', n, 'mnp', mnp)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 22: n has been made optional
.

## Description

nag_ode_bvp_fd_nonlin_gen (d02ra) solves a two-point boundary value problem for a system of n$n$ ordinary differential equations in the interval [a,b]$\left[a,b\right]$ with b > a$b>a$. The system is written in the form
 yi ′ = fi (x,y1,y2, … ,yn) ,   i = 1,2, … ,n $yi′ = f i (x, y 1 , y 2 ,…, y n ) , i=1,2,…,n$ (1)
and the derivatives fi${f}_{i}$ are evaluated by fcn. With the differential equations (1) must be given a system of n$n$ (nonlinear) boundary conditions
 gi (y(a),y(b)) = 0 ,   i = 1,2, … ,n , $gi (y(a),y(b)) = 0 , i=1,2,…,n ,$
where
 y (x) = [ y1 (x) , y2 (x) , … , yn (x) ]T . $y ( x ) = [ y 1 ( x ) , y 2 ( x ) ,…, y n ( x ) ]T .$ (2)
The functions gi${g}_{i}$ are evaluated by g. The solution is computed using a finite difference technique with deferred correction allied to a Newton iteration to solve the finite difference equations. The technique used is described fully in Pereyra (1979).
You must supply an absolute error tolerance and may also supply an initial mesh for the finite difference equations and an initial approximate solution (alternatively a default mesh and approximation are used). The approximate solution is corrected using Newton iteration and deferred correction. Then, additional points are added to the mesh and the solution is recomputed with the aim of making the error everywhere less than your tolerance and of approximately equidistributing the error on the final mesh. The solution is returned on this final mesh.
If the solution is required at a few specific points then these should be included in the initial mesh. If, on the other hand, the solution is required at several specific points then you should use the interpolation functions provided in Chapter E01 if these points do not themselves form a convenient mesh.
The Newton iteration requires Jacobian matrices
 (( ∂ fi)/( ∂ yj)) , (( ∂ gi)/( ∂ yj(a)))  and  (( ∂ gi)/( ∂ yj(b))) . $( ∂fi ∂yj ) , ( ∂gi ∂yj(a) ) and ( ∂gi ∂yj(b) ) .$
These may be supplied through jacobf for ((fi)/(yj)) $\left(\frac{\partial {f}_{i}}{\partial {y}_{j}}\right)$ and jacobg for the others. Alternatively the Jacobians may be calculated by numerical differentiation using the algorithm described in Curtis et al. (1974).
For problems of the type (1) and (2) for which it is difficult to determine an initial approximation from which the Newton iteration will converge, a continuation facility is provided. You must set up a family of problems
 y′ = f (x,y,ε) ,   g (y(a),y(b),ε) = 0 , $y′ = f (x,y,ε) , g (y(a),y(b),ε) = 0 ,$ (3)
where f = [f1,f2,,fn]T $f={\left[{f}_{1},{f}_{2},\dots ,{f}_{n}\right]}^{\mathrm{T}}$ etc., and where ε$\epsilon$ is a continuation parameter. The choice ε = 0$\epsilon =0$ must give a problem (3) which is easy to solve and ε = 1$\epsilon =1$ must define the problem whose solution is actually required. The function solves a sequence of problems with ε$\epsilon$ values
 0 = ε1 < ε2 < ⋯ < εp = 1 . $0 = ε1 < ε2 < ⋯ < εp = 1 .$ (4)
The number p$p$ and the values εi${\epsilon }_{i}$ are chosen by the function so that each problem can be solved using the solution of its predecessor as a starting approximation. Jacobians (f)/(ε) $\frac{\partial f}{\partial \epsilon }$ and (g)/(ε) $\frac{\partial g}{\partial \epsilon }$ are required and they may be supplied by you via jaceps and jacgep respectively or may be computed by numerical differentiation.

## References

Curtis A R, Powell M J D and Reid J K (1974) On the estimation of sparse Jacobian matrices J. Inst. Maths. Applics. 13 117–119
Pereyra V (1979) PASVA3: An adaptive finite-difference Fortran program for first order nonlinear, ordinary boundary problems 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

## Parameters

### Compulsory Input Parameters

1:     np – int64int32nag_int scalar
Must be set to the number of points to be used in the initial mesh.
Constraint: 4npmnp$4\le {\mathbf{np}}\le {\mathbf{mnp}}$.
2:     numbeg – int64int32nag_int scalar
The number of left-hand boundary conditions (that is the number involving y(a)$y\left(a\right)$ only).
Constraint: 0numbeg < n$0\le {\mathbf{numbeg}}<{\mathbf{n}}$.
3:     nummix – int64int32nag_int scalar
The number of coupled boundary conditions (that is the number involving both y(a)$y\left(a\right)$ and y(b)$y\left(b\right)$).
Constraint: 0nummixnnumbeg$0\le {\mathbf{nummix}}\le {\mathbf{n}}-{\mathbf{numbeg}}$.
4:     tol – double scalar
A positive absolute error tolerance. If
 a = x1 < x2 < ⋯ < xnp = b $a=x1
is the final mesh, zj(xi)${z}_{j}\left({x}_{i}\right)$ is the j$j$th component of the approximate solution at xi${x}_{i}$, and yj(x)${y}_{j}\left(x\right)$ is the j$j$th component of the true solution of (1) and (2), then, except in extreme circumstances, it is expected that
 |zj(xi) − yj(xi)| ≤ tol,  i = 1,2, … ,np​ and ​j = 1,2, … ,n. $|zj(xi)-yj(xi)|≤tol, i=1,2,…,np​ and ​j=1,2,…,n.$ (5)
Constraint: tol > 0.0${\mathbf{tol}}>0.0$.
5:     init – int64int32nag_int scalar
Indicates whether you wish to supply an initial mesh and approximate solution (init = 1${\mathbf{init}}=1$) or whether default values are to be used, (init = 0${\mathbf{init}}=0$).
Constraint: init = 0${\mathbf{init}}=0$ or 1$1$.
6:     x(mnp) – double array
mnp, the dimension of the array, must satisfy the constraint mnp32${\mathbf{mnp}}\ge 32$.
You must set x(1) = a${\mathbf{x}}\left(1\right)=a$ and x(np) = b${\mathbf{x}}\left({\mathbf{np}}\right)=b$. If init = 0${\mathbf{init}}=0$ on entry a default equispaced mesh will be used, otherwise you must specify a mesh by setting x(i) = xi${\mathbf{x}}\left(\mathit{i}\right)={x}_{\mathit{i}}$, for i = 2,3,,np1$\mathit{i}=2,3,\dots ,{\mathbf{np}}-1$.
Constraints:
• if init = 0${\mathbf{init}}=0$, x(1) < x(np)${\mathbf{x}}\left(1\right)<{\mathbf{x}}\left({\mathbf{np}}\right)$;
• if init = 1${\mathbf{init}}=1$, x(1) < x(2) < < x(np)${\mathbf{x}}\left(1\right)<{\mathbf{x}}\left(2\right)<\cdots <{\mathbf{x}}\left({\mathbf{np}}\right)$.
7:     y(ldy,mnp) – double array
ldy, the first dimension of the array, must satisfy the constraint ldyn$\mathit{ldy}\ge {\mathbf{n}}$.
If init = 0${\mathbf{init}}=0$, then y need not be set.
If init = 1${\mathbf{init}}=1$, then the array y must contain an initial approximation to the solution such that y(j,i)${\mathbf{y}}\left(j,i\right)$ contains an approximation to
 yj(xi),  i = 1,2, … ,np​ and ​j = 1,2, … ,n. $yj(xi), i=1,2,…,np​ and ​j=1,2,…,n.$
8:     fcn – function handle or string containing name of m-file
fcn must evaluate the functions fi${f}_{i}$ (i.e., the derivatives yi${y}_{i}^{\prime }$) at a general point x$x$ for a given value of ε$\epsilon$, the continuation parameter (see Section [Description]).
[f] = fcn(x, eps, y, n)

Input Parameters

1:     x – double scalar
x$x$, the value of the independent variable.
2:     eps – double scalar
ε$\epsilon$, the value of the continuation parameter. This is 1$1$ if continuation is not being used.
3:     y(n) – double array
yi${y}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,\mathit{n}$, the values of the dependent variables at x$x$.
4:     n – int64int32nag_int scalar
n$n$, the number of equations.

Output Parameters

1:     f(n) – double array
The values of the derivatives fi${f}_{\mathit{i}}$ evaluated at x$x$ given ε$\epsilon$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,\mathit{n}$.
9:     g – function handle or string containing name of m-file
g must evaluate the boundary conditions in equation (3) and place them in the array bc.
[bc] = g(eps, ya, yb, n)

Input Parameters

1:     eps – double scalar
ε$\epsilon$, the value of the continuation parameter. This is 1$1$ if continuation is not being used.
2:     ya(n) – double array
The value yi(a)${y}_{\mathit{i}}\left(a\right)$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
3:     yb(n) – double array
The value yi(b)${y}_{\mathit{i}}\left(b\right)$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
4:     n – int64int32nag_int scalar
n$\mathit{n}$, the number of equations.

Output Parameters

1:     bc(n) – double array
The values gi (y(a),y(b),ε) ${g}_{\mathit{i}}\left(y\left(a\right),y\left(b\right),\epsilon \right)$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$. These must be ordered as follows:
 (i) first, the conditions involving only y(a)$y\left(a\right)$ (see numbeg); (ii) next, the nummix coupled conditions involving both y(a)$y\left(a\right)$ and y(b)$y\left(b\right)$ (see nummix); and, (iii) finally, the conditions involving only y(b)$y\left(b\right)$ (${\mathbf{n}}-{\mathbf{numbeg}}-{\mathbf{nummix}}$).
10:   ijac – int64int32nag_int scalar
Indicates whether or not you are supplying Jacobian evaluation functions.
ijac0${\mathbf{ijac}}\ne 0$
You must supply jacobf and jacobg and also, when continuation is used, jaceps and jacgep.
ijac = 0${\mathbf{ijac}}=0$
Numerical differentiation is used to calculate the Jacobian and the functions nag_ode_bvp_fd_nonlin_gen_dummy_jacobf (d02gaw), nag_ode_bvp_fd_nonlin_gen_dummy_jacobg (d02gax), nag_ode_bvp_fd_nonlin_gen_dummy_jaceps (d02gay) and nag_ode_bvp_fd_nonlin_gen_dummy_jacgep (d02gaz) respectively may be used as the dummy parameters.
11:   jacobf – function handle or string containing name of m-file
jacobf evaluates the Jacobian ((fi)/(yj)) $\left(\frac{\partial {f}_{\mathit{i}}}{\partial {y}_{\mathit{j}}}\right)$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$ and j = 1,2,,n$\mathit{j}=1,2,\dots ,n$, given x$x$ and yj${y}_{\mathit{j}}$, for j = 1,2,,n$\mathit{j}=1,2,\dots ,n$.
If all Jacobians are to be approximated internally by numerical differentiation then it must be replaced by the NAG defined null function pointer NULLFN.
If ijac = 0${\mathbf{ijac}}=0$, then numerical differentiation is used to calculate the Jacobian and the function nag_ode_bvp_fd_nonlin_gen_dummy_jacgep (d02gaz) may be substituted for this parameter.
[f] = jacobf(x, eps, y, n)

Input Parameters

1:     x – double scalar
x$x$, the value of the independent variable.
2:     eps – double scalar
ε$\epsilon$, the value of the continuation parameter. This is 1$1$ if continuation is not being used.
3:     y(n) – double array
yi${y}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,\mathit{n}$, the values of the dependent variables at x$x$.
4:     n – int64int32nag_int scalar
n$\mathit{n}$, the number of equations.

Output Parameters

1:     f(n,n) – double array
f(j,i)${\mathbf{f}}\left(\mathit{j},\mathit{i}\right)$ must be set to the value of (fi)/(yj) $\frac{\partial {f}_{\mathit{i}}}{\partial {y}_{\mathit{j}}}$, evaluated at the point (x,y)$\left(x,y\right)$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$ and j = 1,2,,n$\mathit{j}=1,2,\dots ,n$.
12:   jacobg – function handle or string containing name of m-file
jacobg evaluates the Jacobians ((gi)/(yj(a))) $\left(\frac{\partial {g}_{i}}{\partial {y}_{j}\left(a\right)}\right)$ and ((gi)/(yj(b))) $\left(\frac{\partial {g}_{i}}{\partial {y}_{j}\left(b\right)}\right)$. The ordering of the rows of aj and bj must correspond to the ordering of the boundary conditions described in the specification of g.
If all Jacobians are to be approximated internally by numerical differentiation then it must be replaced by the NAG defined null function pointer NULLFN.
If ijac = 0${\mathbf{ijac}}=0$, then numerical differentiation is used to calculate the Jacobian and the function nag_ode_bvp_fd_nonlin_gen_dummy_jaceps (d02gay) may be substituted for this parameter.
[aj, bj] = jacobg(eps, ya, yb, n)

Input Parameters

1:     eps – double scalar
ε$\epsilon$, the value of the continuation parameter. This is 1$1$ if continuation is not being used.
2:     ya(n) – double array
The value yi(a)${y}_{\mathit{i}}\left(a\right)$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
3:     yb(n) – double array
The value yi(b)${y}_{\mathit{i}}\left(b\right)$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
4:     n – int64int32nag_int scalar
n$\mathit{n}$, the number of equations.

Output Parameters

1:     aj(n,n) – double array
aj(i,j)${\mathbf{aj}}\left(\mathit{i},\mathit{j}\right)$ must be set to the value (gi)/(yj(a)) $\frac{\partial {g}_{\mathit{i}}}{\partial {y}_{\mathit{j}}\left(a\right)}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$ and j = 1,2,,n$\mathit{j}=1,2,\dots ,n$.
2:     bj(n,n) – double array
bj(i,j)${\mathbf{bj}}\left(\mathit{i},\mathit{j}\right)$ must be set to the value (gi)/(yj(b)) $\frac{\partial {g}_{\mathit{i}}}{\partial {y}_{\mathit{j}}\left(b\right)}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$ and j = 1,2,,n$\mathit{j}=1,2,\dots ,n$.
13:   deleps – double scalar
Must be given a value which specifies whether continuation is required. If deleps0.0${\mathbf{deleps}}\le 0.0$ or deleps1.0${\mathbf{deleps}}\ge 1.0$ then it is assumed that continuation is not required. If 0.0 < deleps < 1.0$0.0<{\mathbf{deleps}}<1.0$ then it is assumed that continuation is required unless deleps < sqrt(machine precision) when an error exit is taken. deleps is used as the increment ε2ε1${\epsilon }_{2}-{\epsilon }_{1}$ (see (4)) and the choice deleps = 0.1${\mathbf{deleps}}=0.1$ is recommended.
14:   jaceps – function handle or string containing name of m-file
jaceps evaluates the derivative (fi)/(ε) $\frac{\partial {f}_{i}}{\partial \epsilon }$ given x$x$ and y$y$ if continuation is being used.
If all Jacobians (derivatives) are to be approximated internally by numerical differentiation, or continuation is not being used, then it must be replaced by the NAG defined null function pointer NULLFN.
If ijac = 0${\mathbf{ijac}}=0$, then numerical differentiation is used to calculate the Jacobian and the function nag_ode_bvp_fd_nonlin_gen_dummy_jacobf (d02gaw) may be substituted for this parameter.
[f] = jaceps(x, eps, y, n)

Input Parameters

1:     x – double scalar
x$x$, the value of the independent variable.
2:     eps – double scalar
ε$\epsilon$, the value of the continuation parameter.
3:     y(n) – double array
The solution values yi${y}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$, at the point x$x$.
4:     n – int64int32nag_int scalar
n$\mathit{n}$, the number of equations.

Output Parameters

1:     f(n) – double array
f(i)${\mathbf{f}}\left(\mathit{i}\right)$ must contain the value (fi)/(ε) $\frac{\partial {f}_{\mathit{i}}}{\partial \epsilon }$ at the point (x,y)$\left(x,y\right)$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
15:   jacgep – function handle or string containing name of m-file
jacgep evaluates the derivatives (gi)/(ε) $\frac{\partial {g}_{i}}{\partial \epsilon }$ if continuation is being used.
If all Jacobians (derivatives) are to be approximated internally by numerical differentiation, or continuation is not being used, then it must be replaced by the NAG defined null function pointer NULLFN.
If ijac = 0${\mathbf{ijac}}=0$, then numerical differentiation is used to calculate the Jacobian and the function nag_ode_bvp_fd_nonlin_gen_dummy_jacobg (d02gax) may be substituted for this parameter.
[bcep] = jacgep(eps, ya, yb, n)

Input Parameters

1:     eps – double scalar
ε$\epsilon$, the value of the continuation parameter.
2:     ya(n) – double array
The value of yi(a)${y}_{\mathit{i}}\left(a\right)$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
3:     yb(n) – double array
The value of yi(b)${y}_{\mathit{i}}\left(b\right)$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
4:     n – int64int32nag_int scalar
n$\mathit{n}$, the number of equations.

Output Parameters

1:     bcep(n) – double array
bcep(i)${\mathbf{bcep}}\left(\mathit{i}\right)$ must contain the value of (gi)/(ε) $\frac{\partial {g}_{\mathit{i}}}{\partial \epsilon }$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The first dimension of the array y.
n$\mathit{n}$, the number of differential equations.
Constraint: n > 0${\mathbf{n}}>0$.
2:     mnp – int64int32nag_int scalar
Default: The dimension of the array x and the second dimension of the array y. (An error is raised if these dimensions are not equal.)
mnp must be set to the maximum permitted number of points in the finite difference mesh. If lwork or liwork are too small then internally mnp will be replaced by the maximum permitted by these values. (A warning message will be output if on entry ifail is set to obtain monitoring information.)
Constraint: mnp32${\mathbf{mnp}}\ge 32$.

### Input Parameters Omitted from the MATLAB Interface

ldy work lwork iwork liwork

### Output Parameters

1:     np – int64int32nag_int scalar
The number of points in the final mesh.
2:     x(mnp) – double array
x(1),x(2),,x(np)${\mathbf{x}}\left(1\right),{\mathbf{x}}\left(2\right),\dots ,{\mathbf{x}}\left({\mathbf{np}}\right)$ define the final mesh (with the returned value of np) and x(1) = a${\mathbf{x}}\left(1\right)=a$ and x(np) = b${\mathbf{x}}\left({\mathbf{np}}\right)=b$.
3:     y(ldy,mnp) – double array
ldyn$\mathit{ldy}\ge {\mathbf{n}}$.
The approximate solution zj(xi)${z}_{j}\left({x}_{i}\right)$ satisfying (5) on the final mesh, that is
 y(j,i) = zj(xi),  i = 1,2, … ,np​ and ​j = 1,2, … ,n, $yji=zj(xi), i=1,2,…,np​ and ​j=1,2,…,n,$
where np is the number of points in the final mesh. If an error has occurred then y contains the latest approximation to the solution. The remaining columns of y are not used.
4:     abt(n) – double array
abt(i)${\mathbf{abt}}\left(\mathit{i}\right)$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$, holds the largest estimated error (in magnitude) of the i$i$th component of the solution over all mesh points.
5:     deleps – double scalar
An overestimate of the increment εpεp1${\epsilon }_{p}-{\epsilon }_{p-1}$ (in fact the value of the increment which would have been tried if the restriction εp = 1${\epsilon }_{p}=1$ had not been imposed). If continuation was not requested then deleps = 0.0${\mathbf{deleps}}=0.0$.
If continuation is not requested then jaceps and jacgep may each be replaced by dummy actual parameters in the call to nag_ode_bvp_fd_nonlin_gen (d02ra). (nag_ode_bvp_fd_nonlin_gen_dummy_jacobf (d02gaw) and nag_ode_bvp_fd_nonlin_gen_dummy_jacobg (d02gax) respectively may be used as the dummy parameters.)
6:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

ifail = 1${\mathbf{ifail}}=1$
One or more of the parameters n, mnp, np, numbeg, nummix, tol, deleps, lwork or liwork is incorrectly set, or x(1)x(np)${\mathbf{x}}\left(1\right)\ge {\mathbf{x}}\left({\mathbf{np}}\right)$ or the mesh points x(i)${\mathbf{x}}\left(i\right)$ are not in strictly ascending order.
ifail = 2${\mathbf{ifail}}=2$
A finer mesh is required for the accuracy requested; that is mnp is not large enough. This error exit normally occurs when the problem being solved is difficult (for example, there is a boundary layer) and high accuracy is requested. A poor initial choice of mesh points will make this error exit more likely.
ifail = 3${\mathbf{ifail}}=3$
The Newton iteration has failed to converge. There are several possible causes for this error:
 (i) faulty coding in one of the Jacobian calculation functions; (ii) if ijac = 0${\mathbf{ijac}}=0$ then inaccurate Jacobians may have been calculated numerically (this is a very unlikely cause); or, (iii) a poor initial mesh or initial approximate solution has been selected either by you or by default or there are not enough points in the initial mesh. Possibly, you should try the continuation facility.
W ifail = 4${\mathbf{ifail}}=4$
The Newton iteration has reached round-off error level. It could be however that the answer returned is satisfactory. The error is likely to occur if too high an accuracy is requested.
ifail = 5${\mathbf{ifail}}=5$
The Jacobian calculated by jacobg (or the equivalent matrix calculated by numerical differentiation) is singular. This may occur due to faulty coding of jacobg or, in some circumstances, to a zero initial choice of approximate solution (such as is chosen when init = 0${\mathbf{init}}=0$).
ifail = 6${\mathbf{ifail}}=6$
There is no dependence on ε$\epsilon$ when continuation is being used. This can be due to faulty coding of jaceps or jacgep or, in some circumstances, to a zero initial choice of approximate solution (such as is chosen when init = 0${\mathbf{init}}=0$).
ifail = 7${\mathbf{ifail}}=7$
deleps is required to be less than machine precision for continuation to proceed. It is likely that either the problem (3) has no solution for some value near the current value of ε$\epsilon$ (see the advisory print out from nag_ode_bvp_fd_nonlin_gen (d02ra)) or that the problem is so difficult that even with continuation it is unlikely to be solved using this function. If the latter cause is suspected then using more mesh points initially may help.
ifail = 8${\mathbf{ifail}}=8$
ifail = 9${\mathbf{ifail}}=9$
A serious error has occurred in an internal call. Check all array subscripts and function parameter lists in calls to nag_ode_bvp_fd_nonlin_gen (d02ra). Seek expert help.

## Accuracy

The solution returned by the function will be accurate to your tolerance as defined by the relation (5) except in extreme circumstances. The final error estimate over the whole mesh for each component is given in the array abt. If too many points are specified in the initial mesh, the solution may be more accurate than requested and the error may not be approximately equidistributed.

There are too many factors present to quantify the timing. The time taken by nag_ode_bvp_fd_nonlin_gen (d02ra) is negligible only on very simple problems.
You are strongly recommended to set ifail to obtain self-explanatory error messages, and also monitoring information about the course of the computation. Monitoring information is written to a logical advisory message unit which normally default to the same unit number as the error message unit (see Section [Input/output in the Library] in the (essin) for details); the advisory message unit number can be changed by calling nag_file_set_unit_advisory (x04ab).
In the case where you wish to solve a sequence of similar problems, the use of the final mesh and solution from one case as the initial mesh is strongly recommended for the next.

## Example

This example solves the differential equation
 y ′ ′ ′ = − yy ′ ′ − 2ε(1 − y′2) $y′′′=-yy′′-2ε(1-y′2)$
with ε = 1$\epsilon =1$ and boundary conditions
 y(0) = y′(0) = 0,  y′(10) = 1 $y(0)=y′(0)=0, y′(10)=1$
to an accuracy specified by tol = 1.0e−4${\mathbf{tol}}=\text{1.0e−4}$. The continuation facility is used with the continuation parameter ε$\epsilon$ introduced as in the differential equation above and with deleps = 0.1${\mathbf{deleps}}=0.1$ initially. (The continuation facility is not needed for this problem and is used here for illustration.)
```function nag_ode_bvp_fd_nonlin_gen_example
% Initialize variables and arrays.
np = 17;
numbeg = 2;
nummix = 0;
tol = 0.0001;
init = 0;
x = zeros(40,1);
x(np) = 10;
y = zeros(3,40);
ijac = 1;
deleps = 0.1;
n = 3;

fprintf('nag_ode_bvp_fd_nonlin_gen example program results\n\n');

% Call NAG routine.
[npOut, xOut, yOut, abt, delepsOut, ifail] = nag_ode_bvp_fd_nonlin_gen(int64(np), ...
int64(numbeg), int64(nummix), tol, int64(init), x, y, ...
@fcn, @g, int64(ijac), @jacobf, ...
@jacobg, deleps, @jaceps, @jacgep);
if ifail ~= 0 && ifail ~= 4
% Unsuccessful call.  Print message and exit.
error('Warning: nag_ode_bvp_fd_nonlin_gen returned with ifail = %1d ',ifail);
end

% If ifail = 4 on exit, then the iteration has reached the round-off error
% level; it could be that the answer is still satisfactory.
if (ifail == 4)
fprintf('Iteration reached round-off error level: check answer.\n\n');
end

% Output results.
fprintf(['Calculation using analytic Jacobians \n\n', ...
'Solution on final mesh of %1.0f points\n\n'], npOut);
fprintf('     x(i)      y1(i)     y2(i)    y3(i)\n');
for j = 1:int32(npOut); i = 1:int32(n); % can't use int64 in loop range.
fprintf('%10.4f',xOut(j),yOut(i,j));
fprintf('\n');
end
fprintf('\n   Maximum estimated error by components\n           ');
fprintf('%9.2d ',abt);
fprintf('\n');
% Plot results.
fig = figure('Number', 'off');
display_plot(xOut(1:int32(npOut)), yOut(:,1:int32(npOut)))

function f = fcn(x, eps, y, n)
% Evaluate derivative vector.
f = zeros(n,1);
f(1) = y(2);
f(2) = y(3);
f(3) = -y(1)*y(3) - 2.0d0*(1.0-y(2)*y(2))*eps;
function bc = g(eps, ya, yb, n)
% Evaluate the boundary conditions.
bc = zeros(n,1);
bc(1) = ya(1);
bc(2) = ya(2);
bc(3) = yb(2) - 1;
function f = jaceps(x, eps, y, n)
% Evaluate the derivative w.r.t. eps.
f = zeros(n,1);
f(1) = 0.0d0;
f(2) = 0.0d0;
f(3) = -2.0d0*(1.0d0-y(2)*y(2));
function bcep = jacgep(eps, ya, yb, n)
% Evaluate the derivatives of the boundary conditions w.r.t. eps.
bcep = zeros(n,1);
function f = jacobf(x, eps, y, n)
% Evaluate the Jacobian.
f = zeros(n,n);
f(1,2) = 1.0;
f(2,3) = 1.0;
f(3,1) = -y(3);
f(3,2) = 4.0*y(2)*eps;
f(3,3) = -y(1);
function [aj, bj] = jacobg(eps, ya, yb, n)
% Evaluate the Jacobian for the boundary conditions.
aj = zeros(n,n);
bj = zeros(n,n);
aj(1,1) = 1.0;
aj(2,2) = 1.0;
bj(3,2) = 1.0;
function display_plot(x, y)
% Formatting for title and axis labels.
titleFmt = {'FontName', 'Helvetica', 'FontWeight', 'Bold', 'FontSize', 14};
labFmt = {'FontName', 'Helvetica', 'FontWeight', 'Bold', 'FontSize', 13};
set(gca, 'FontSize', 13); % for legend, axis tick labels, etc.
% Plot two of the curves, then add the other one.
[haxes, hline1, hline2] = plotyy(x, y(1,:), x, y(2,:));
% We want the third curve to be plotted on the right-hand y-axis.
axes(haxes(2));
hold on
hline3 = plot(x, y(3,:));
% Set the axis limits and the tick specifications to beautify the plot.
set(haxes(1), 'YLim', [0 10]);
set(haxes(1), 'YMinorTick', 'on');
set(haxes(1), 'YTick', [0 2 4 6 8 10]);
set(haxes(2), 'YLim', [0 2.0]);
set(haxes(2), 'YMinorTick', 'on');
% This is a kludge: we need to have the same number of tick marks on both
% y axes, otherwise the y1 ticks will be visible on the y2 axis (in spite
% of our asking for the box not to be drawn below, which doesn't seem to
% have the desired affect here).
set(haxes(2), 'YTick', [0 0.4 0.8 1.2 1.6 2.0]);
for iaxis = 1:2
% These properties must be the same for both sets of axes.
set(haxes(iaxis), 'XLim', [0 10]);
set(haxes(iaxis), 'XTick', [0 2 4 6 8 10]);
set(haxes(iaxis), 'FontSize', 13);
end
set(gca, 'box', 'off'); % so ticks aren't shown on opposite axes.
title('Solution of Third-order BVP', titleFmt{:});
% Label the axes.
xlabel('x', labFmt{:});
ylabel(haxes(1), 'y', labFmt{:});
ylabel(haxes(2), 'y'' and y''''', labFmt{:});
legend('y''','y''''','y','Location','Best')
% Set some features of the three lines.
set(hline1, 'Linewidth', 0.25, 'Marker', '+', 'Line', '-');
set(hline2, 'Linewidth', 0.25, 'Marker', 'x', 'Line', '--');
set(hline3, 'Linewidth', 0.25, 'Marker', '*', 'Line', ':');
```
```
nag_ode_bvp_fd_nonlin_gen example program results

Calculation using analytic Jacobians

Solution on final mesh of 33 points

x(i)      y1(i)     y2(i)    y3(i)
0.0000    0.0000    0.0000    1.6872
0.0625    0.0032    0.1016    1.5626
0.1250    0.0125    0.1954    1.4398
0.1875    0.0275    0.2816    1.3203
0.2500    0.0476    0.3605    1.2054
0.3750    0.1015    0.4976    0.9924
0.5000    0.1709    0.6097    0.8048
0.6250    0.2530    0.6999    0.6438
0.7031    0.3095    0.7467    0.5563
0.7812    0.3695    0.7871    0.4784
0.9375    0.4978    0.8513    0.3490
1.0938    0.6346    0.8977    0.2502
1.2500    0.7776    0.9308    0.1763
1.4583    0.9748    0.9598    0.1077
1.6667    1.1768    0.9773    0.0639
1.8750    1.3815    0.9876    0.0367
2.0312    1.5362    0.9922    0.0238
2.1875    1.6915    0.9952    0.0151
2.5000    2.0031    0.9983    0.0058
2.6562    2.1591    0.9990    0.0035
2.8125    2.3153    0.9994    0.0021
3.1250    2.6277    0.9998    0.0007
3.7500    3.2526    1.0000    0.0001
4.3750    3.8776    1.0000    0.0000
5.0000    4.5026    1.0000    0.0000
5.6250    5.1276    1.0000   -0.0000
6.2500    5.7526    1.0000    0.0000
6.8750    6.3776    1.0000   -0.0000
7.5000    7.0026    1.0000    0.0000
8.1250    7.6276    1.0000   -0.0000
8.7500    8.2526    1.0000    0.0000
9.3750    8.8776    1.0000   -0.0000
10.0000    9.5026    1.0000    0.0000

Maximum estimated error by components
6.92e-05  1.81e-05  6.42e-05

```
```function d02ra_example
% Initialize variables and arrays.
np = 17;
numbeg = 2;
nummix = 0;
tol = 0.0001;
init = 0;
x = zeros(40,1);
x(np) = 10;
y = zeros(3,40);
ijac = 1;
deleps = 0.1;
n = 3;

fprintf('d02ra example program results\n\n');

% Call NAG routine.
[npOut, xOut, yOut, abt, delepsOut, ifail] = d02ra(int64(np), ...
int64(numbeg), int64(nummix), tol, int64(init), x, y, ...
@fcn, @g, int64(ijac), @jacobf, ...
@jacobg, deleps, @jaceps, @jacgep);
if ifail ~= 0 && ifail ~= 4
% Unsuccessful call.  Print message and exit.
error('Warning: d02ra returned with ifail = %1d ',ifail);
end

% If ifail = 4 on exit, then the iteration has reached the round-off error
% level; it could be that the answer is still satisfactory.
if (ifail == 4)
fprintf('Iteration reached round-off error level: check answer.\n\n');
end

% Output results.
fprintf(['Calculation using analytic Jacobians \n\n', ...
'Solution on final mesh of %1.0f points\n\n'], npOut);
fprintf('     x(i)      y1(i)     y2(i)    y3(i)\n');
for j = 1:int32(npOut); i = 1:int32(n); % can't use int64 in loop range.
fprintf('%10.4f',xOut(j),yOut(i,j));
fprintf('\n');
end
fprintf('\n   Maximum estimated error by components\n           ');
fprintf('%9.2d ',abt);
fprintf('\n');
% Plot results.
fig = figure('Number', 'off');
display_plot(xOut(1:int32(npOut)), yOut(:,1:int32(npOut)))

function f = fcn(x, eps, y, n)
% Evaluate derivative vector.
f = zeros(n,1);
f(1) = y(2);
f(2) = y(3);
f(3) = -y(1)*y(3) - 2.0d0*(1.0-y(2)*y(2))*eps;
function bc = g(eps, ya, yb, n)
% Evaluate the boundary conditions.
bc = zeros(n,1);
bc(1) = ya(1);
bc(2) = ya(2);
bc(3) = yb(2) - 1;
function f = jaceps(x, eps, y, n)
% Evaluate the derivative w.r.t. eps.
f = zeros(n,1);
f(1) = 0.0d0;
f(2) = 0.0d0;
f(3) = -2.0d0*(1.0d0-y(2)*y(2));
function bcep = jacgep(eps, ya, yb, n)
% Evaluate the derivatives of the boundary conditions w.r.t. eps.
bcep = zeros(n,1);
function f = jacobf(x, eps, y, n)
% Evaluate the Jacobian.
f = zeros(n,n);
f(1,2) = 1.0;
f(2,3) = 1.0;
f(3,1) = -y(3);
f(3,2) = 4.0*y(2)*eps;
f(3,3) = -y(1);
function [aj, bj] = jacobg(eps, ya, yb, n)
% Evaluate the Jacobian for the boundary conditions.
aj = zeros(n,n);
bj = zeros(n,n);
aj(1,1) = 1.0;
aj(2,2) = 1.0;
bj(3,2) = 1.0;
function display_plot(x, y)
% Formatting for title and axis labels.
titleFmt = {'FontName', 'Helvetica', 'FontWeight', 'Bold', 'FontSize', 14};
labFmt = {'FontName', 'Helvetica', 'FontWeight', 'Bold', 'FontSize', 13};
set(gca, 'FontSize', 13); % for legend, axis tick labels, etc.
% Plot two of the curves, then add the other one.
[haxes, hline1, hline2] = plotyy(x, y(1,:), x, y(2,:));
% We want the third curve to be plotted on the right-hand y-axis.
axes(haxes(2));
hold on
hline3 = plot(x, y(3,:));
% Set the axis limits and the tick specifications to beautify the plot.
set(haxes(1), 'YLim', [0 10]);
set(haxes(1), 'YMinorTick', 'on');
set(haxes(1), 'YTick', [0 2 4 6 8 10]);
set(haxes(2), 'YLim', [0 2.0]);
set(haxes(2), 'YMinorTick', 'on');
% This is a kludge: we need to have the same number of tick marks on both
% y axes, otherwise the y1 ticks will be visible on the y2 axis (in spite
% of our asking for the box not to be drawn below, which doesn't seem to
% have the desired affect here).
set(haxes(2), 'YTick', [0 0.4 0.8 1.2 1.6 2.0]);
for iaxis = 1:2
% These properties must be the same for both sets of axes.
set(haxes(iaxis), 'XLim', [0 10]);
set(haxes(iaxis), 'XTick', [0 2 4 6 8 10]);
set(haxes(iaxis), 'FontSize', 13);
end
set(gca, 'box', 'off'); % so ticks aren't shown on opposite axes.
title('Solution of Third-order BVP', titleFmt{:});
% Label the axes.
xlabel('x', labFmt{:});
ylabel(haxes(1), 'y', labFmt{:});
ylabel(haxes(2), 'y'' and y''''', labFmt{:});
legend('y''','y''''','y','Location','Best')
% Set some features of the three lines.
set(hline1, 'Linewidth', 0.25, 'Marker', '+', 'Line', '-');
set(hline2, 'Linewidth', 0.25, 'Marker', 'x', 'Line', '--');
set(hline3, 'Linewidth', 0.25, 'Marker', '*', 'Line', ':');
```
```
d02ra example program results

Calculation using analytic Jacobians

Solution on final mesh of 33 points

x(i)      y1(i)     y2(i)    y3(i)
0.0000    0.0000    0.0000    1.6872
0.0625    0.0032    0.1016    1.5626
0.1250    0.0125    0.1954    1.4398
0.1875    0.0275    0.2816    1.3203
0.2500    0.0476    0.3605    1.2054
0.3750    0.1015    0.4976    0.9924
0.5000    0.1709    0.6097    0.8048
0.6250    0.2530    0.6999    0.6438
0.7031    0.3095    0.7467    0.5563
0.7812    0.3695    0.7871    0.4784
0.9375    0.4978    0.8513    0.3490
1.0938    0.6346    0.8977    0.2502
1.2500    0.7776    0.9308    0.1763
1.4583    0.9748    0.9598    0.1077
1.6667    1.1768    0.9773    0.0639
1.8750    1.3815    0.9876    0.0367
2.0312    1.5362    0.9922    0.0238
2.1875    1.6915    0.9952    0.0151
2.5000    2.0031    0.9983    0.0058
2.6562    2.1591    0.9990    0.0035
2.8125    2.3153    0.9994    0.0021
3.1250    2.6277    0.9998    0.0007
3.7500    3.2526    1.0000    0.0001
4.3750    3.8776    1.0000    0.0000
5.0000    4.5026    1.0000    0.0000
5.6250    5.1276    1.0000   -0.0000
6.2500    5.7526    1.0000    0.0000
6.8750    6.3776    1.0000   -0.0000
7.5000    7.0026    1.0000    0.0000
8.1250    7.6276    1.0000   -0.0000
8.7500    8.2526    1.0000    0.0000
9.3750    8.8776    1.0000   -0.0000
10.0000    9.5026    1.0000    0.0000

Maximum estimated error by components
6.92e-05  1.81e-05  6.42e-05

```