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_fixedbc (d02ga)

## Purpose

nag_ode_bvp_fd_nonlin_fixedbc (d02ga) solves a two-point boundary value problem with assigned boundary values for a system of ordinary differential equations, using a deferred correction technique and a Newton iteration.

## Syntax

[x, y, np, ifail] = d02ga(u, v, a, b, tol, fcn, x, np, 'n', n, 'mnp', mnp)
[x, y, np, ifail] = nag_ode_bvp_fd_nonlin_fixedbc(u, v, a, b, tol, fcn, x, np, 'n', n, 'mnp', mnp)

## Description

nag_ode_bvp_fd_nonlin_fixedbc (d02ga) solves a two-point boundary value problem for a system of n$\mathit{n}$ differential equations in the interval [a,b$a,b$]. The system is written in the form:
 yi ′ = fi(x,y1,y2, … ,yn),  i = 1,2, … ,n $yi′=fi(x,y1,y2,…,yn), i=1,2,…,n$ (1)
and the derivatives fi${f}_{i}$ are evaluated by fcn. Initially, n$\mathit{n}$ boundary values of the variables yi${y}_{i}$ must be specified, some at a$a$ and some at b$b$. You must supply estimates of the remaining n$\mathit{n}$ boundary values and all the boundary values are used in constructing an initial approximation to the solution. This approximate solution is corrected by a finite difference technique with deferred correction allied with a Newton iteration to solve the finite difference equations. The technique used is described fully in Pereyra (1979). The Newton iteration requires a Jacobian matrix (fi)/(yj) $\frac{\partial {f}_{i}}{\partial {y}_{j}}$ and this is calculated by numerical differentiation using an algorithm described in Curtis et al. (1974).
You supply an absolute error tolerance and may also supply an initial mesh for the construction of the finite difference equations (alternatively a default mesh is used). The algorithm constructs a solution on a mesh defined by adding points to the initial mesh. This solution is chosen so that the error is everywhere less than your tolerance and so that the error is approximately equidistributed 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.

## 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:     u(n,2$2$) – double array
n, the first dimension of the array, must satisfy the constraint n2${\mathbf{n}}\ge 2$.
u(i,1)${\mathbf{u}}\left(\mathit{i},1\right)$ must be set to the known or estimated value of yi${y}_{\mathit{i}}$ at a$a$ and u(i,2)${\mathbf{u}}\left(\mathit{i},2\right)$ must be set to the known or estimated value of yi${y}_{\mathit{i}}$ at b$b$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,\mathit{n}$.
2:     v(n,2$2$) – double array
n, the first dimension of the array, must satisfy the constraint n2${\mathbf{n}}\ge 2$.
v(i,j)${\mathbf{v}}\left(\mathit{i},\mathit{j}\right)$ must be set to 0.0$0.0$ if u(i,j)${\mathbf{u}}\left(\mathit{i},\mathit{j}\right)$ is a known value and to 1.0$1.0$ if u(i,j)${\mathbf{u}}\left(\mathit{i},\mathit{j}\right)$ is an estimated value, for i = 1,2,,n$\mathit{i}=1,2,\dots ,\mathit{n}$ and j = 1,2$\mathit{j}=1,2$.
Constraint: precisely n$\mathit{n}$ of the v(i,j)${\mathbf{v}}\left(i,j\right)$ must be set to 0.0$0.0$, i.e., precisely n$\mathit{n}$ of the u(i,j)${\mathbf{u}}\left(i,j\right)$ must be known values, and these must not be all at a$a$ or all at b$b$.
3:     a – double scalar
a$a$, the left-hand boundary point.
4:     b – double scalar
b$b$, the right-hand boundary point.
Constraint: b > a${\mathbf{b}}>{\mathbf{a}}$.
5:     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 equation (1) (see Section [Description]) and the boundary conditions, then, except in extreme cases, 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.$ (2)
Constraint: tol > 0.0${\mathbf{tol}}>0.0$.
6:     fcn – function handle or string containing name of m-file
fcn must evaluate the functions fi${f}_{\mathit{i}}$ (i.e., the derivatives yi${y}_{\mathit{i}}^{\prime }$), for i = 1,2,,n$\mathit{i}=1,2,\dots ,\mathit{n}$, at a general point x$x$.
[f] = fcn(x, y)

Input Parameters

1:     x – double scalar
x$x$, the value of the argument.
2:     y( : $:$) – double array
yi${y}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,\mathit{n}$, the value of the argument.

Output Parameters

1:     f( : $:$) – double array
The values of fi${f}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,\mathit{n}$.
7:     x(mnp) – double array
mnp, the dimension of the array, must satisfy the constraint mnp32${\mathbf{mnp}}\ge 32$.
If np4${\mathbf{np}}\ge 4$ (see np), the first np elements must define an initial mesh. Otherwise the elements of x need not be set.
Constraint:
 a = x(1) < x(2) < ⋯ < x(np) = b,   np ≥ 4.$a=x1 (3)
8:     np – int64int32nag_int scalar
Determines whether a default or user-supplied mesh is used.
np = 0${\mathbf{np}}=0$
A default value of 4$4$ for np and a corresponding equispaced mesh x(1),x(2),,x(np)${\mathbf{x}}\left(1\right),{\mathbf{x}}\left(2\right),\dots ,{\mathbf{x}}\left({\mathbf{np}}\right)$ are used.
np4${\mathbf{np}}\ge 4$
You must define an initial mesh using the array x as described.
Constraint: np = 0${\mathbf{np}}=0$ or 4npmnp$4\le {\mathbf{np}}\le {\mathbf{mnp}}$.

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The first dimension of the arrays u, v. (An error is raised if these dimensions are not equal.)
n$\mathit{n}$, the number of equations.
Constraint: n2${\mathbf{n}}\ge 2$.
2:     mnp – int64int32nag_int scalar
Default: The dimension of the array x.
The maximum permitted number of mesh points.
Constraint: mnp32${\mathbf{mnp}}\ge 32$.

w lw iw liw

### Output Parameters

1:     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) satisfying the relation (3).
2:     y(n,mnp) – double array
The approximate solution zj(xi)${z}_{j}\left({x}_{i}\right)$ satisfying (2), 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.
The remaining columns of y are not used.
3:     np – int64int32nag_int scalar
The number of points in the final (returned) mesh.
4:     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, tol, np, mnp, lw or liw has been incorrectly set, or ba${\mathbf{b}}\le {\mathbf{a}}$, or the condition (3) on x is not satisfied, or the number of known boundary values (specified by v) is not n.
ifail = 2${\mathbf{ifail}}=2$
The Newton iteration has failed to converge. This could be due to there being too few points in the initial mesh or to the initial approximate solution being too inaccurate. If this latter reason is suspected you should use nag_ode_bvp_fd_nonlin_gen (d02ra) instead. If the warning ‘Jacobian matrix is singular’ is printed this could be due to specifying zero estimated boundary values and these should be varied. This warning could also be printed in the unlikely event of the Jacobian matrix being calculated inaccurately. If you cannot make changes to prevent the warning then nag_ode_bvp_fd_nonlin_gen (d02ra) should be used.
W ifail = 3${\mathbf{ifail}}=3$
The Newton iteration has reached round-off level. It could be, however, that the answer returned is satisfactory. This error might occur if too much accuracy is requested.
W ifail = 4${\mathbf{ifail}}=4$
A finer mesh is required for the accuracy requested; that is mnp is not large enough.
ifail = 5${\mathbf{ifail}}=5$
A serious error has occurred in a call to nag_ode_bvp_fd_nonlin_fixedbc (d02ga). Check all array subscripts and function parameter lists in calls to nag_ode_bvp_fd_nonlin_fixedbc (d02ga). Seek expert help.

## Accuracy

The solution returned by the function will be accurate to your tolerance as defined by the relation (2) except in extreme circumstances. 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.

The time taken by nag_ode_bvp_fd_nonlin_fixedbc (d02ga) depends on the difficulty of the problem, the number of mesh points (and meshes) used, the number of Newton iterations and the number of deferred corrections.
You are strongly recommended to set ifail to obtain self-explanatory error messages, and also monitoring information about the course of the computation. You may select the channel numbers on which this output is to appear by calls of nag_file_set_unit_error (x04aa) (for error messages) or nag_file_set_unit_advisory (x04ab) (for monitoring information) – see Section [Example] for an example. Otherwise the default channel numbers will be used.
A common cause of convergence problems in the Newton iteration is that you have specified too few points in the initial mesh. Although the function adds points to the mesh to improve accuracy it is unable to do so until the solution on the initial mesh has been calculated in the Newton iteration.
If you specify zero known and estimated boundary values, the function constructs a zero initial approximation and in many cases the Jacobian is singular when evaluated for this approximation, leading to the breakdown of the Newton iteration.
You may be unable to provide a sufficiently good choice of initial mesh and estimated boundary values, and hence the Newton iteration may never converge. In this case the continuation facility provided in nag_ode_bvp_fd_nonlin_gen (d02ra) is recommended.
In the case where you wish to solve a sequence of similar problems, the final mesh from solving one case is strongly recommended as the initial mesh for the next.

## Example

This example solves the differential equation
 y ′ ′ ′ = − yy ′ ′ − β(1 − y′2) $y′′′=-yy′′-β(1-y′2)$
with boundary conditions
 y(0) = y′(0) = 0,   y′(10) = 1 $y(0)=y′(0)= 0, y′(10)=1$
for β = 0.0$\beta =0.0$ and β = 0.2$\beta =0.2$ to an accuracy specified by tol = 1.0e−3${\mathbf{tol}}=\text{1.0e−3}$. We solve first the simpler problem with β = 0.0$\beta =0.0$ using an equispaced mesh of 26$26$ points and then we solve the problem with β = 0.2$\beta =0.2$ using the final mesh from the first problem.
Note the call to nag_file_set_unit_advisory (x04ab) prior to the call to nag_ode_bvp_fd_nonlin_fixedbc (d02ga).
function nag_ode_bvp_fd_nonlin_fixedbc_example
% For communication with fcn & display_plot.
global beta;

% Initialize variables and arrays.
u = [0, 10; 0, 1; 0, 0];
v = [0, 1; 0, 0; 1, 1];
a = 0;
b = 10;
tol = 0.001;
x = zeros(40, 1);
np = 26;
beta = double(0);
n = 3;
for i = 2:np-1
x(i) = (double(np-i)*a+double(i-1)*b)/double(np-1);
end
x(np) = b;

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

for k = 1:2

[xOut, y, npOut, ifail] = nag_ode_bvp_fd_nonlin_fixedbc(u, v, a, b, tol, @fcn, ...
x, int64(np));
if ifail ~= 0
% Problems in solution.  Print message and exit.
disp(['Solution on final mesh of ',num2str(npOut)]);
error('Warning: nag_ode_bvp_fd_nonlin_fixedbc returned with ifail = %1d ',ifail);
end

fprintf('Problem with beta = %1.2f \n\n',beta);
fprintf('Solution on final mesh of %1.0f points\n\n',npOut);
disp('  X(I)       Y1(I)         Y2(I)         Y3(I)');
for i = 1:int32(npOut); j = 1:int32(n); % can't use int64 in loop range.
fprintf('%6.3f     %8.5f      %8.5f      %8.5f\n',...
x(i),y(1,i),y(2,i),y(3,i));
end

% Plot results.
xplot = x(1:int32(npOut)); % can't use int64 in loop range.
yplot = y(:, 1:int32(npOut)); % can't use int64 in loop range.
fig = figure('Number', 'off');
display_plot(xplot, yplot);
disp(' ');
beta = double(beta + 0.2);
end

function f = fcn(x,y)
% For communication with the main routine.
global beta;

% Evaluate the derivatives.
f = zeros(3,1);
f(1) = y(2);
f(2) = y(3);
f(3) = -y(1)*y(3)- beta*(1.0-y(2)*y(2));
function display_plot(x, y)
% For communication with the main routine.
global beta;
% 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 the three curves.
plot(x, y(1,:), '-+', ...
x, y(2,:), '--x', ...
x, y(3,:), ':*');
title(['Two-point Boundary-value Problem using Deferred Correction', ...
'Technique (\beta = ',num2str(beta),')'], titleFmt{:});
% Label the axes.
xlabel('x', labFmt{:});
ylabel('Solution', labFmt{:});
legend('y','y''','y''''','Location','Best');


nag_ode_bvp_fd_nonlin_fixedbc example program results

Problem with beta = 0.00

Solution on final mesh of 26 points

X(I)       Y1(I)         Y2(I)         Y3(I)
0.000      0.00000       0.00000       0.46948
0.400      0.03754       0.18762       0.46725
0.800      0.14970       0.37191       0.45109
1.200      0.33363       0.54504       0.41042
1.600      0.58277       0.69635       0.34245
2.000      0.88639       0.81635       0.25584
2.400      1.23094       0.90088       0.16781
2.800      1.60263       0.95288       0.09525
3.200      1.98997       0.98045       0.04636
3.600      2.38505       0.99296       0.01928
4.000      2.78340       0.99780       0.00686
4.400      3.18292       0.99940       0.00210
4.800      3.58279       0.99986       0.00055
5.200      3.98276       0.99997       0.00012
5.600      4.38276       1.00000       0.00002
6.000      4.78276       1.00000       0.00000
6.400      5.18276       1.00000       0.00000
6.800      5.58276       1.00000       0.00000
7.200      5.98276       1.00000      -0.00000
7.600      6.38276       1.00000       0.00000
8.000      6.78276       1.00000      -0.00000
8.400      7.18276       1.00000       0.00000
8.800      7.58276       1.00000      -0.00000
9.200      7.98276       1.00000       0.00000
9.600      8.38276       1.00000      -0.00000
10.000      8.78276       1.00000      -0.00000

Problem with beta = 0.20

Solution on final mesh of 26 points

X(I)       Y1(I)         Y2(I)         Y3(I)
0.000      0.00000       0.00000       0.68654
0.400      0.05279       0.25843       0.60395
0.800      0.20203       0.48144       0.50912
1.200      0.43245       0.66359       0.40014
1.600      0.72676       0.80075       0.28602
2.000      1.06701       0.89385       0.18212
2.400      1.43680       0.94979       0.10167
2.800      1.82330       0.97911       0.04923
3.200      2.21802       0.99240       0.02056
3.600      2.61623       0.99759       0.00740
4.000      3.01569       0.99933       0.00230
4.400      3.41555       0.99984       0.00062
4.800      3.81552       0.99997       0.00014
5.200      4.21551       0.99999       0.00003
5.600      4.61551       1.00000       0.00000
6.000      5.01551       1.00000       0.00000
6.400      5.41551       1.00000      -0.00000
6.800      5.81551       1.00000      -0.00000
7.200      6.21551       1.00000      -0.00000
7.600      6.61551       1.00000      -0.00000
8.000      7.01551       1.00000      -0.00000
8.400      7.41551       1.00000      -0.00000
8.800      7.81551       1.00000      -0.00000
9.200      8.21551       1.00000      -0.00000
9.600      8.61551       1.00000      -0.00000
10.000      9.01551       1.00000      -0.00000


function d02ga_example
% For communication with fcn & display_plot.
global beta;

% Initialize variables and arrays.
u = [0, 10; 0, 1; 0, 0];
v = [0, 1; 0, 0; 1, 1];
a = 0;
b = 10;
tol = 0.001;
x = zeros(40, 1);
np = 26;
beta = double(0);
n = 3;
for i = 2:np-1
x(i) = (double(np-i)*a+double(i-1)*b)/double(np-1);
end
x(np) = b;

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

for k = 1:2

[xOut, y, npOut, ifail] = d02ga(u, v, a, b, tol, @fcn, ...
x, int64(np));
if ifail ~= 0
% Problems in solution.  Print message and exit.
disp(['Solution on final mesh of ',num2str(npOut)]);
error('Warning: d02ga returned with ifail = %1d',ifail);
end

fprintf('Problem with beta = %1.2f \n\n',beta);
fprintf('Solution on final mesh of %1.0f points\n\n',npOut);
disp('  X(I)       Y1(I)         Y2(I)         Y3(I)');
for i = 1:int32(npOut); j = 1:int32(n); % can't use int64 in loop range.
fprintf('%6.3f     %8.5f      %8.5f      %8.5f\n',...
x(i),y(1,i),y(2,i),y(3,i));
end

% Plot results.
xplot = x(1:int32(npOut)); % can't use int64 in loop range.
yplot = y(:, 1:int32(npOut)); % can't use int64 in loop range.
fig = figure('Number', 'off');
display_plot(xplot, yplot);
disp(' ');
beta = double(beta + 0.2);
end

function f = fcn(x,y)
% For communication with the main routine.
global beta;

% Evaluate the derivatives.
f = zeros(3,1);
f(1) = y(2);
f(2) = y(3);
f(3) = -y(1)*y(3)- beta*(1.0-y(2)*y(2));
function display_plot(x, y)
% For communication with the main routine.
global beta;
% 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 the three curves.
plot(x, y(1,:), '-+', ...
x, y(2,:), '--x', ...
x, y(3,:), ':*');
title(['Two-point Boundary-value Problem using Deferred Correction', ...
'Technique (\beta = ',num2str(beta),')'], titleFmt{:});
% Label the axes.
xlabel('x', labFmt{:});
ylabel('Solution', labFmt{:});
legend('y','y''','y''''','Location','Best');


d02ga example program results

Problem with beta = 0.00

Solution on final mesh of 26 points

X(I)       Y1(I)         Y2(I)         Y3(I)
0.000      0.00000       0.00000       0.46948
0.400      0.03754       0.18762       0.46725
0.800      0.14970       0.37191       0.45109
1.200      0.33363       0.54504       0.41042
1.600      0.58277       0.69635       0.34245
2.000      0.88639       0.81635       0.25584
2.400      1.23094       0.90088       0.16781
2.800      1.60263       0.95288       0.09525
3.200      1.98997       0.98045       0.04636
3.600      2.38505       0.99296       0.01928
4.000      2.78340       0.99780       0.00686
4.400      3.18292       0.99940       0.00210
4.800      3.58279       0.99986       0.00055
5.200      3.98276       0.99997       0.00012
5.600      4.38276       1.00000       0.00002
6.000      4.78276       1.00000       0.00000
6.400      5.18276       1.00000       0.00000
6.800      5.58276       1.00000       0.00000
7.200      5.98276       1.00000      -0.00000
7.600      6.38276       1.00000       0.00000
8.000      6.78276       1.00000      -0.00000
8.400      7.18276       1.00000       0.00000
8.800      7.58276       1.00000      -0.00000
9.200      7.98276       1.00000       0.00000
9.600      8.38276       1.00000      -0.00000
10.000      8.78276       1.00000      -0.00000

Problem with beta = 0.20

Solution on final mesh of 26 points

X(I)       Y1(I)         Y2(I)         Y3(I)
0.000      0.00000       0.00000       0.68654
0.400      0.05279       0.25843       0.60395
0.800      0.20203       0.48144       0.50912
1.200      0.43245       0.66359       0.40014
1.600      0.72676       0.80075       0.28602
2.000      1.06701       0.89385       0.18212
2.400      1.43680       0.94979       0.10167
2.800      1.82330       0.97911       0.04923
3.200      2.21802       0.99240       0.02056
3.600      2.61623       0.99759       0.00740
4.000      3.01569       0.99933       0.00230
4.400      3.41555       0.99984       0.00062
4.800      3.81552       0.99997       0.00014
5.200      4.21551       0.99999       0.00003
5.600      4.61551       1.00000       0.00000
6.000      5.01551       1.00000       0.00000
6.400      5.41551       1.00000      -0.00000
6.800      5.81551       1.00000      -0.00000
7.200      6.21551       1.00000      -0.00000
7.600      6.61551       1.00000      -0.00000
8.000      7.01551       1.00000      -0.00000
8.400      7.41551       1.00000      -0.00000
8.800      7.81551       1.00000      -0.00000
9.200      8.21551       1.00000      -0.00000
9.600      8.61551       1.00000      -0.00000
10.000      9.01551       1.00000      -0.00000