hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_ode_bvp_fd_nonlin_gen (d02ra)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

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 n ordinary differential equations in the interval a,b with b>a. The system is written in the form
yi = f i x, y 1 , y 2 ,, y n ,   i=1,2,,n (1)
and the derivatives fi are evaluated by fcn. With the differential equations (1) must be given a system of n (nonlinear) boundary conditions
gi ya,yb = 0 ,   i=1,2,,n ,  
where
y x = y 1 x , y 2 x ,, y n x T . (2)
The functions gi 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 yja  and  gi yjb .  
These may be supplied through jacobf for fi yj  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 ya,yb,ε = 0 , (3)
where f = f 1 , f 2 ,, f n T  etc., and where ε is a continuation parameter. The choice ε=0 must give a problem (3) which is easy to solve and ε=1 must define the problem whose solution is actually required. The function solves a sequence of problems with ε values
0 = ε1 < ε2 < < εp = 1 . (4)
The number p and the values ε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 ε  and g ε  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.
2:     numbeg int64int32nag_int scalar
The number of left-hand boundary conditions (that is the number involving ya only).
Constraint: 0numbeg<n.
3:     nummix int64int32nag_int scalar
The number of coupled boundary conditions (that is the number involving both ya and yb).
Constraint: 0nummixn-numbeg.
4:     tol – double scalar
A positive absolute error tolerance. If
a=x1<x2<<xnp=b  
is the final mesh, zjxi is the jth component of the approximate solution at xi, and yjx is the jth component of the true solution of (1) and (2), then, except in extreme circumstances, it is expected that
zjxi-yjxitol,  i=1,2,,np​ and ​j=1,2,,n. (5)
Constraint: tol>0.0.
5:     init int64int32nag_int scalar
Indicates whether you wish to supply an initial mesh and approximate solution (init=1) or whether default values are to be used, (init=0).
Constraint: init=0 or 1.
6:     xmnp – double array
You must set x1=a and xnp=b. If init=0 on entry a default equispaced mesh will be used, otherwise you must specify a mesh by setting xi=xi, for i=2,3,,np-1.
Constraints:
  • if init=0, x1<xnp;
  • if init=1, x1<x2<<xnp.
7:     yldymnp – double array
ldy, the first dimension of the array, must satisfy the constraint ldyn.
If init=0, then y need not be set.
If init=1, then the array y must contain an initial approximation to the solution such that yji contains an approximation to
yjxi,  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 (i.e., the derivatives yi) at a general point x for a given value of ε, the continuation parameter (see Description).
[f] = fcn(x, eps, y, n)

Input Parameters

1:     x – double scalar
x, the value of the independent variable.
2:     eps – double scalar
ε, the value of the continuation parameter. This is 1 if continuation is not being used.
3:     yn – double array
yi, for i=1,2,,n, the values of the dependent variables at x.
4:     n int64int32nag_int scalar
n, the number of equations.

Output Parameters

1:     fn – double array
The values of the derivatives fi evaluated at x given ε, for i=1,2,,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
ε, the value of the continuation parameter. This is 1 if continuation is not being used.
2:     yan – double array
The value yia, for i=1,2,,n.
3:     ybn – double array
The value yib, for i=1,2,,n.
4:     n int64int32nag_int scalar
n, the number of equations.

Output Parameters

1:     bcn – double array
The values gi ya,yb,ε , for i=1,2,,n. These must be ordered as follows:
(i) first, the conditions involving only ya (see numbeg);
(ii) next, the nummix coupled conditions involving both ya and yb (see nummix); and,
(iii) finally, the conditions involving only yb (n-numbeg-nummix).
10:   ijac int64int32nag_int scalar
Indicates whether or not you are supplying Jacobian evaluation functions.
ijac0
You must supply jacobf and jacobg and also, when continuation is used, jaceps and jacgep.
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 arguments.
11:   jacobf – function handle or string containing name of m-file
jacobf evaluates the Jacobian fi yj , for i=1,2,,n and j=1,2,,n, given x and yj, for j=1,2,,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, 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:     x – double scalar
x, the value of the independent variable.
2:     eps – double scalar
ε, the value of the continuation parameter. This is 1 if continuation is not being used.
3:     yn – double array
yi, for i=1,2,,n, the values of the dependent variables at x.
4:     n int64int32nag_int scalar
n, the number of equations.

Output Parameters

1:     fnn – double array
fji must be set to the value of fi yj , evaluated at the point x,y, for i=1,2,,n and j=1,2,,n.
12:   jacobg – function handle or string containing name of m-file
jacobg evaluates the Jacobians gi yja  and gi yjb . 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, 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:     eps – double scalar
ε, the value of the continuation parameter. This is 1 if continuation is not being used.
2:     yan – double array
The value yia, for i=1,2,,n.
3:     ybn – double array
The value yib, for i=1,2,,n.
4:     n int64int32nag_int scalar
n, the number of equations.

Output Parameters

1:     ajnn – double array
ajij must be set to the value gi yja , for i=1,2,,n and j=1,2,,n.
2:     bjnn – double array
bjij must be set to the value gi yjb , for i=1,2,,n and j=1,2,,n.
13:   deleps – double scalar
Must be given a value which specifies whether continuation is required. If deleps0.0 or deleps1.0 then it is assumed that continuation is not required. If 0.0<deleps<1.0 then it is assumed that continuation is required unless deleps<machine precision when an error exit is taken. deleps is used as the increment ε2-ε1 (see (4)) and the choice deleps=0.1 is recommended.
14:   jaceps – function handle or string containing name of m-file
jaceps evaluates the derivative fi ε  given x and 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, 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:     x – double scalar
x, the value of the independent variable.
2:     eps – double scalar
ε, the value of the continuation parameter.
3:     yn – double array
The solution values yi, for i=1,2,,n, at the point x.
4:     n int64int32nag_int scalar
n, the number of equations.

Output Parameters

1:     fn – double array
fi must contain the value fi ε at the point x,y, for i=1,2,,n.
15:   jacgep – function handle or string containing name of m-file
jacgep evaluates the derivatives gi ε  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, 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:     eps – double scalar
ε, the value of the continuation parameter.
2:     yan – double array
The value of yia, for i=1,2,,n.
3:     ybn – double array
The value of yib, for i=1,2,,n.
4:     n int64int32nag_int scalar
n, the number of equations.

Output Parameters

1:     bcepn – double array
bcepi must contain the value of gi ε , for i=1,2,,n.

Optional Input Parameters

1:     n int64int32nag_int scalar
Default: the first dimension of the array y.
n, the number of differential equations.
Constraint: 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.

Output Parameters

1:     np int64int32nag_int scalar
The number of points in the final mesh.
2:     xmnp – double array
x1,x2,,xnp define the final mesh (with the returned value of np) and x1=a and xnp=b.
3:     yldymnp – double array
The approximate solution zjxi satisfying (5) on the final mesh, that is
yji=zjxi,  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:     abtn – double array
abti, for i=1,2,,n, holds the largest estimated error (in magnitude) of the ith component of the solution over all mesh points.
5:     deleps – double scalar
An overestimate of the increment εp-εp-1 (in fact the value of the increment which would have been tried if the restriction εp=1 had not been imposed). If continuation was not requested then deleps=0.0.
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:     ifail int64int32nag_int scalar
ifail=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
One or more of the arguments n, mnp, np, numbeg, nummix, tol, deleps, lwork or liwork is incorrectly set, or x1xnp or the mesh points xi are not in strictly ascending order.
   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
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 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
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
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).
   ifail=6
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 init=0).
   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 ε (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
   ifail=9
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.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
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
y=-yy-2ε1-y2  
with ε=1 and boundary conditions
y0=y0=0,  y10=1  
to an accuracy specified by tol=1.0e−4. The continuation facility is used with the continuation parameter ε introduced as in the differential equation above and with deleps=0.1 initially. (The continuation facility is not needed for this problem and is used here for illustration.)
function d02ra_example


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

% Initialize variables and arrays.
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;

% Call NAG routine.
[np, x, y, abt, deleps, ifail] = ...
  d02ra(...
        np, numbeg, nummix, tol, init, x, y, @fcn, @g, ijac, @jacobf, ...
        @jacobg, deleps, @jaceps, @jacgep);

% Output results.
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');
% Plot results.
fig1 = figure;
display_plot(x(1:np), y(:,1:np))


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*(1-y(2)^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;
  f(2) = 0;
  f(3) = -2*(1-y(2)^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;
  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)
  % Evaluate the Jacobian for the boundary conditions.
  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)
  % 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:10]);
  set(haxes(2), 'YLim', [0 2]);
  set(haxes(2), 'YMinorTick', 'on');
  set(haxes(2), 'YTick', [0:0.4:2]);
  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:10]);
  end
  set(gca, 'box', 'off'); 
  % Add title.
  title('Solution of Third-order BVP');
  % Label the axes.
  xlabel('x');
  ylabel(haxes(1), 'y');
  ylabel(haxes(2), 'y'' and y''''');
  % Add a legend.
  legend('y''','y''''','y','Location','North')
  % Set some features of the three lines.
  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
d02ra_fig1.png

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015