PDF version (NAG web site
, 64-bit version, 64-bit version)
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:
At Mark 22: |
n was made optional |
Description
nag_ode_bvp_fd_nonlin_gen (d02ra) solves a two-point boundary value problem for a system of
ordinary differential equations in the interval
with
. The system is written in the form
and the derivatives
are evaluated by
fcn. With the differential equations
(1) must be given a system of
(nonlinear) boundary conditions
where
The functions
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
These may be supplied through
jacobf for
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
where
etc., and where
is a continuation parameter. The choice
must give a problem
(3) which is easy to solve and
must define the problem whose solution is actually required. The function solves a sequence of problems with
values
The number
and the values
are chosen by the function so that each problem can be solved using the solution of its predecessor as a starting approximation. Jacobians
and
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:
– int64int32nag_int scalar
-
Must be set to the number of points to be used in the initial mesh.
Constraint:
.
- 2:
– int64int32nag_int scalar
-
The number of left-hand boundary conditions (that is the number involving only).
Constraint:
.
- 3:
– int64int32nag_int scalar
-
The number of coupled boundary conditions (that is the number involving both and ).
Constraint:
.
- 4:
– double scalar
-
A positive absolute error tolerance. If
is the final mesh,
is the
th component of the approximate solution at
, and
is the
th component of the true solution of
(1) and
(2), then, except in extreme circumstances, it is expected that
Constraint:
.
- 5:
– int64int32nag_int scalar
-
Indicates whether you wish to supply an initial mesh and approximate solution () or whether default values are to be used, ().
Constraint:
or .
- 6:
– double array
-
You must set and . If on entry a default equispaced mesh will be used, otherwise you must specify a mesh by setting
, for .
Constraints:
- if , ;
- if , .
- 7:
– double array
-
ldy, the first dimension of the array, must satisfy the constraint
.
If
, then
y need not be set.
If
, then the array
y must contain an initial approximation to the solution such that
contains an approximation to
- 8:
– function handle or string containing name of m-file
-
fcn must evaluate the functions
(i.e., the derivatives
) at a general point
for a given value of
, the continuation parameter (see
Description).
[f] = fcn(x, eps, y, n)
Input Parameters
- 1:
– double scalar
-
, the value of the independent variable.
- 2:
– double scalar
-
, the value of the continuation parameter. This is if continuation is not being used.
- 3:
– double array
-
, for , the values of the dependent variables at .
- 4:
– int64int32nag_int scalar
-
, the number of equations.
Output Parameters
- 1:
– double array
-
The values of the derivatives
evaluated at given , for .
- 9:
– 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:
– double scalar
-
, the value of the continuation parameter. This is if continuation is not being used.
- 2:
– double array
-
The value
, for .
- 3:
– double array
-
The value
, for .
- 4:
– int64int32nag_int scalar
-
, the number of equations.
Output Parameters
- 1:
– double array
-
The values
, for
. These must be ordered as follows:
(i) |
first, the conditions involving only (see numbeg); |
(ii) |
next, the nummix coupled conditions involving both and (see nummix); and, |
(iii) |
finally, the conditions involving only (). |
- 10:
– int64int32nag_int scalar
-
Indicates whether or not you are supplying Jacobian evaluation functions.
- You must supply jacobf and jacobg and also, when continuation is used, jaceps and jacgep.
- 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 arguments.
- 11:
– function handle or string containing name of m-file
-
jacobf evaluates the Jacobian
, for
and
, given
and
, for
.
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 , 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 argument.
[f] = jacobf(x, eps, y, n)
Input Parameters
- 1:
– double scalar
-
, the value of the independent variable.
- 2:
– double scalar
-
, the value of the continuation parameter. This is if continuation is not being used.
- 3:
– double array
-
, for , the values of the dependent variables at .
- 4:
– int64int32nag_int scalar
-
, the number of equations.
Output Parameters
- 1:
– double array
-
must be set to the value of , evaluated at the point , for and .
- 12:
– function handle or string containing name of m-file
-
jacobg evaluates the Jacobians
and
. 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 , 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 argument.
[aj, bj] = jacobg(eps, ya, yb, n)
Input Parameters
- 1:
– double scalar
-
, the value of the continuation parameter. This is if continuation is not being used.
- 2:
– double array
-
The value
, for .
- 3:
– double array
-
The value
, for .
- 4:
– int64int32nag_int scalar
-
, the number of equations.
Output Parameters
- 1:
– double array
-
must be set to the value , for and .
- 2:
– double array
-
must be set to the value , for and .
- 13:
– double scalar
-
Must be given a value which specifies whether continuation is required. If
or
then it is assumed that continuation is not required. If
then it is assumed that continuation is required unless
when an error exit is taken.
deleps is used as the increment
(see
(4)) and the choice
is recommended.
- 14:
– function handle or string containing name of m-file
-
jaceps evaluates the derivative
given
and
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 , 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 argument.
[f] = jaceps(x, eps, y, n)
Input Parameters
- 1:
– double scalar
-
, the value of the independent variable.
- 2:
– double scalar
-
, the value of the continuation parameter.
- 3:
– double array
-
The solution values
, for , at the point .
- 4:
– int64int32nag_int scalar
-
, the number of equations.
Output Parameters
- 1:
– double array
-
must contain the value at the point , for .
- 15:
– function handle or string containing name of m-file
-
jacgep evaluates the derivatives
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 , 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 argument.
[bcep] = jacgep(eps, ya, yb, n)
Input Parameters
- 1:
– double scalar
-
, the value of the continuation parameter.
- 2:
– double array
-
The value of
, for .
- 3:
– double array
-
The value of
, for .
- 4:
– int64int32nag_int scalar
-
, the number of equations.
Output Parameters
- 1:
– double array
-
must contain the value of , for .
Optional Input Parameters
- 1:
– int64int32nag_int scalar
-
Default:
the first dimension of the array
y.
, the number of differential equations.
Constraint:
.
- 2:
– 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:
.
Output Parameters
- 1:
– int64int32nag_int scalar
-
The number of points in the final mesh.
- 2:
– double array
-
define the final mesh (with the returned value of
np) and
and
.
- 3:
– double array
-
The approximate solution
satisfying
(5) on the final mesh, that is
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:
– double array
-
, for , holds the largest estimated error (in magnitude) of the th component of the solution over all mesh points.
- 5:
– double scalar
-
An overestimate of the increment
(in fact the value of the increment which would have been tried if the restriction
had not been imposed). If continuation was not requested then
.
If continuation is not requested then
jaceps and
jacgep may each be replaced by dummy actual arguments 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 arguments.)
- 6:
– int64int32nag_int scalar
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.
-
-
One or more of the arguments
n,
mnp,
np,
numbeg,
nummix,
tol,
deleps,
lwork or
liwork is incorrectly set, or
or the mesh points
are not in strictly ascending order.
-
-
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.
-
-
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 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
-
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.
-
-
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
).
-
-
There is no dependence on
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
).
-
-
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
(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.
-
-
-
A serious error has occurred in an internal call. Check all array subscripts and function argument lists in calls to nag_ode_bvp_fd_nonlin_gen (d02ra). Seek expert help.
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
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.
Further Comments
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.
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
with
and boundary conditions
to an accuracy specified by
. The continuation facility is used with the continuation parameter
introduced as in the differential equation above and with
initially. (The continuation facility is not needed for this problem and is used here for illustration.)
Open in the MATLAB editor:
d02ra_example
function d02ra_example
fprintf('d02ra example results\n\n');
n = 3;
np = int64(17);
mnp = int64(40);
numbeg = int64(2);
nummix = int64(0);
tol = 0.0001;
init = int64(0);
x = zeros(mnp,1);
x(np) = 10;
y = zeros(n,mnp);
ijac = int64(1);
deleps = 0.1;
[np, x, y, abt, deleps, ifail] = ...
d02ra(...
np, numbeg, nummix, tol, init, x, y, @fcn, @g, ijac, @jacobf, ...
@jacobg, deleps, @jaceps, @jacgep);
fprintf(['Calculation using analytic Jacobians \n\n'], ...
['Solution on final mesh of %4d points\n\n'], np);
fprintf(' x y_1 y_2 y_3\n');
for j = 1:np
fprintf('%10.4f',x(j),y(1:n,j));
fprintf('\n');
end
fprintf('\n Maximum estimated error by components\n');
fprintf('%12.2e', abt);
fprintf('\n');
fig1 = figure;
display_plot(x(1:np), y(:,1:np))
function f = fcn(x, eps, y, n)
f = zeros(n,1);
f(1) = y(2);
f(2) = y(3);
f(3) = -y(1)*y(3) - 2*(1-y(2)^2)*eps;
function bc = g(eps, ya, yb, n)
bc = zeros(n,1);
bc(1) = ya(1);
bc(2) = ya(2);
bc(3) = yb(2) - 1;
function f = jaceps(x, eps, y, n)
f = zeros(n,1);
f(1) = 0;
f(2) = 0;
f(3) = -2*(1-y(2)^2);
function bcep = jacgep(eps, ya, yb, n)
bcep = zeros(n,1);
function f = jacobf(x, eps, y, n)
f = zeros(n,n);
f(1,2) = 1;
f(2,3) = 1;
f(3,1) = -y(3);
f(3,2) = 4*y(2)*eps;
f(3,3) = -y(1);
function [aj, bj] = jacobg(eps, ya, yb, n)
aj = zeros(n,n);
bj = zeros(n,n);
aj(1,1) = 1;
aj(2,2) = 1;
bj(3,2) = 1;
function display_plot(x, y)
[haxes, hline1, hline2] = plotyy(x, y(1,:), x, y(2,:));
axes(haxes(2));
hold on
hline3 = plot(x, y(3,:));
set(haxes(1), 'YLim', [0 10]);
set(haxes(1), 'YMinorTick', 'on');
set(haxes(1), 'YTick', [0:2:10]);
set(haxes(2), 'YLim', [0 2]);
set(haxes(2), 'YMinorTick', 'on');
set(haxes(2), 'YTick', [0:0.4:2]);
for iaxis = 1:2
set(haxes(iaxis), 'XLim', [0 10]);
set(haxes(iaxis), 'XTick', [0:2:10]);
end
set(gca, 'box', 'off');
title('Solution of Third-order BVP');
xlabel('x');
ylabel(haxes(1), 'y');
ylabel(haxes(2), 'y'' and y''''');
legend('y''','y''''','y','Location','North')
set(hline1,'Linewidth',0.25,'Marker','+','LineStyle','-','Color','red');
set(hline2,'Linewidth',0.25,'Marker','x','LineStyle','--','Color','green');
set(hline3,'Linewidth',0.25,'Marker','*','LineStyle',':','Color','blue');
d02ra example results
Calculation using analytic Jacobians
x y_1 y_2 y_3
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
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015