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_lin_gen (d02gb)

Purpose

nag_ode_bvp_fd_lin_gen (d02gb) solves a general linear two-point boundary value problem for a system of ordinary differential equations, using a deferred correction technique.

Syntax

[c, d, gam, x, y, np, ifail] = d02gb(a, b, tol, fcnf, fcng, c, d, gam, x, np, 'n', n, 'mnp', mnp)
[c, d, gam, x, y, np, ifail] = nag_ode_bvp_fd_lin_gen(a, b, tol, fcnf, fcng, c, d, gam, x, np, 'n', n, 'mnp', mnp)

Description

nag_ode_bvp_fd_lin_gen (d02gb) solves a linear two-point boundary value problem for a system of nn ordinary differential equations in the interval [a,ba,b]. The system is written in the form
y = F(x)y + g(x)
y=F(x)y+g(x)
(1)
and the boundary conditions are written in the form
Cy(a) + Dy(b) = γ .
Cy(a)+Dy(b)=γ .
(2)
Here F(x)F(x), CC and DD are nn by nn matrices, and g(x)g(x) and γγ are nn-component vectors. The approximate solution to (1) and (2) is found using a finite difference method with deferred correction. The algorithm is a specialization of that used in function nag_ode_bvp_fd_nonlin_gen (d02ra) which solves a nonlinear version of (1) and (2). The nonlinear version of the algorithm is described fully in Pereyra (1979).
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

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:     a – double scalar
aa, the left-hand boundary point.
2:     b – double scalar
bb, the right-hand boundary point.
Constraint: b > ab>a.
3:     tol – double scalar
A positive absolute error tolerance. If
a = x1 < x2 < < xnp = b
a=x1<x2<<xnp=b
is the final mesh, z(x)z(x) is the approximate solution from nag_ode_bvp_fd_lin_gen (d02gb) and y(x)y(x) is the true solution of equations (1) and (2) then, except in extreme cases, it is expected that
zytol
z-ytol
(3)
where
u = max max |ui(xj)|.
1in 1jnp
u = max 1in max 1jnp |ui(xj)| .
Constraint: tol > 0.0tol>0.0.
4:     fcnf – function handle or string containing name of m-file
fcnf must evaluate the matrix F(x)F(x) in (1) at a general point xx.
[f] = fcnf(x)

Input Parameters

1:     x – double scalar
xx, the value of the independent variable.

Output Parameters

1:     f( : :) – double array
f(j,i)fji must contain the (i,j)(i,j)th element of the matrix F(x)F(x), for i = 1,2,,ni=1,2,,n and j = 1,2,,nj=1,2,,n. (See Section [Example] for an example.)
5:     fcng – function handle or string containing name of m-file
fcng must evaluate the vector g(x)g(x) in (1) at a general point xx.
[g] = fcng(x)

Input Parameters

1:     x – double scalar
xx, the value of the independent variable.

Output Parameters

1:     g( : :) – double array
The iith element of the vector g(x)g(x), for i = 1,2,,ni=1,2,,n. (See Section [Example] for an example.)
6:     c(n,n) – double array
7:     d(n,n) – double array
8:     gam(n) – double array
n, the first dimension of the array, must satisfy the constraint n2n2.
The arrays c and d must be set to the matrices CC and DD in (2)). gam must be set to the vector γγ in (2).
9:     x(mnp) – double array
mnp, the dimension of the array, must satisfy the constraint mnp32mnp32.
If np4np4 (see np), the first np elements must define an initial mesh. Otherwise the elements of xx need not be set.
Constraint:
a = x(1) < x(2) < < x(np) = b,   np4 .
a=x1<x2<<xnp=b,   np4 .
(4)
10:   np – int64int32nag_int scalar
Determines whether a default mesh or user-supplied mesh is used.
np = 0np=0
A default value of 44 for np and a corresponding equispaced mesh x(1),x(2),,x(np)x1,x2,,xnp are used.
np4np4
You must define an initial mesh x as in (4).

Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the array gam and the first dimension of the arrays c, d and the second dimension of the arrays c, d. (An error is raised if these dimensions are not equal.)
The number of equations; that is nn is the order of system (1).
Constraint: n2n2.
2:     mnp – int64int32nag_int scalar
Default: The dimension of the array x.
The maximum permitted number of mesh points.
Constraint: mnp32mnp32.

Input Parameters Omitted from the MATLAB Interface

w lw iw liw

Output Parameters

1:     c(n,n) – double array
2:     d(n,n) – double array
3:     gam(n) – double array
The rows of c and d and the components of gam are reordered so that the boundary conditions are in the order:
(i) conditions on y(a)y(a) only;
(ii) condition involving y(a)y(a) and y(b)y(b); and
(iii) conditions on y(b)y(b) only.
The function will be slightly more efficient if the arrays c, d and gam are ordered in this way before entry, and in this event they will be unchanged on exit.
Note that the problems (1) and (2) must be of boundary value type, that is neither CC nor DD may be identically zero. Note also that the rank of the matrix [C,D][C,D] must be nn for the problem to be properly posed. Any violation of these conditions will lead to an error exit.
4:     x(mnp) – double array
x(1),x(2),,x(np)x1,x2,,xnp define the final mesh (with the returned value of np) satisfying the relation (4).
5:     y(n,mnp) – double array
The approximate solution z(x)z(x) satisfying (3), 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.
6:     np – int64int32nag_int scalar
The number of points in the final (returned) mesh.
7:     ifail – int64int32nag_int scalar
ifail = 0ifail=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 = 1ifail=1
One or more of the parameters n, tol, np, mnp, lw or liw is incorrectly set, baba or the condition (4) on x is not satisfied.
  ifail = 2ifail=2
There are three possible reasons for this error exit to be taken:
(i) one of the matrices CC or DD is identically zero (that is, the problem is of initial value and not boundary value type). In this case, iw(1) = 0iw1=0 on exit;
(ii) a row of CC and the corresponding row of DD are identically zero (that is, the boundary conditions are rank deficient). In this case, on exit iw(1)iw1 contains the index of the first such row encountered; and
(iii) more than nn of the columns of the nn by 2n2n matrix [C,D][C,D] are identically zero (that is, the boundary conditions are rank deficient). In this case, on exit iw(1)iw1 contains minus the number of non-identically zero columns.
W ifail = 3ifail=3
The function has failed to find a solution to the specified accuracy. There are a variety of possible reasons including:
(i) the boundary conditions are rank deficient, which may be indicated by the message that the Jacobian is singular. However this is an unlikely explanation for the error exit as all rank deficient boundary conditions should lead instead to error exits with either ifail = 2ifail=2 or 55; see also (iv);
(ii) not enough mesh points are permitted in order to attain the required accuracy. This is indicated by np = mnpnp=mnp on return from a call to nag_ode_bvp_fd_lin_gen (d02gb). This difficulty may be aggravated by a poor initial choice of mesh points;
(iii) the accuracy requested cannot be attained on the computer being used; and
(iv) an unlikely combination of values of FxFx has led to a singular Jacobian. The error should not persist if more mesh points are allowed.
  ifail = 4ifail=4
A serious error has occurred in a call to nag_ode_bvp_fd_lin_gen (d02gb). Check all array subscripts and function parameter lists in calls to nag_ode_bvp_fd_lin_gen (d02gb). Seek expert help.
  ifail = 5ifail=5
There are two possible reasons for this error exit which occurs when checking the rank of the boundary conditions by reduction to a row echelon form:
(i) at least one row of the nn by 2n2n matrix [C,D][C,D] is a linear combination of the other rows and hence the boundary conditions are rank deficient. The index of the first such row encountered is given by iw(1)iw1 on exit; and
(ii) as (i) but the rank deficiency implied by this error exit has only been determined up to a numerical tolerance. Minus the index of the first such row encountered is given by iw(1)iw1 on exit.
In case (ii) there is some doubt as to the rank deficiency of the boundary conditions. However even if the boundary conditions are not rank deficient they are not posed in a suitable form for use with this function.
For example, if
C =
(10)
1 ε
,  D =
(10)
1 0
,  γ =
(γ1)
γ2
C= 1 0 1 ε ,  D= 1 0 1 0 ,  γ= γ1 γ2
and εε is small enough, this error exit is likely to be taken. A better form for the boundary conditions in this case would be
C =
(10)
0 1
,  D =
(10)
0 0
,  γ =
(γ1)
ε1(γ2γ1)
.
C= 1 0 0 1 ,  D= 1 0 0 0 ,  γ= γ1 ε-1(γ2-γ1) .

Accuracy

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

Further Comments

The time taken by nag_ode_bvp_fd_lin_gen (d02gb) depends on the difficulty of the problem, the number of mesh points (and meshes) used 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.
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 problem (written as a first-order system)
ε y + y = 0
ε y+y=0
with boundary conditions
y(0) = 0,  y(1) = 1
y(0)=0,  y(1)=1
for the cases ε = 101ε=10-1 and ε = 102ε=10-2 using the default initial mesh in the first case, and the final mesh of the first case as initial mesh for the second (more difficult) case. We give the solution and the error at each mesh point to illustrate the accuracy of the method given the accuracy request tol = 1.0e−3tol=1.0e−3.
Note the call to nag_file_set_unit_advisory (x04ab) prior to the call to nag_ode_bvp_fd_lin_gen (d02gb).
function nag_ode_bvp_fd_lin_gen_example
% For communication with f.
global eps;

% Set up initial values before calling NAG routine for the first time.
mnp = 70;
a = 0;
b = 1;
tol = 0.001;
c = [1, 0; 0, 0];
d = [0, 0; 1, 0];
gam = [0; 1];
x = zeros(1,mnp);
np = 0;
n = 2;
xkeep = zeros(1,2);
npkeep = zeros(2);
ykeep = zeros(1,2);
espkeep = zeros(2);

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

for i = 1:2
    eps = double(10)^(-i);

    [c, d, gam, x, y, np, ifail] = ...
        nag_ode_bvp_fd_lin_gen(a, b, tol, @f, @g, c, d, gam, x,...
        int64(np), 'n', int64(n), 'mnp', int64(mnp));
    if (ifail ~= 0)
        % Problems in integration, or incorrect parameters.  Print
        % message and exit.
        error('Warning: nag_ode_bvp_fd_lin_gen returned with ifail = %d ',ifail);
    else
        % Output results.
        fprintf('\nProblem with epsilon = %1.2e\n\n',eps);
        fprintf('Approximate solution on final mesh of %1.0f points \n',np)
        disp('      X(I)       Y(1,I)');
        for j = 1:int32(np) % can't use int64 in loop range
            fprintf('     %1.4f    %8.4f\n',x(j),y(1,j));
            xkeep(j,i) = x(j);
            ykeep(j,i) = y(1,j);
        end
    end
    % Store the number of points and epsilon for use in plot.
    npkeep(i) = np;
    espkeep(i) = eps;
end
% Plot results.
fig = figure('Number', 'off');
display_plot(xkeep, ykeep, npkeep, espkeep)

function f = f(x)
% For communication with main routine.
global eps;

% Evaluate matrix.
f=zeros(2,2);
f(1,1) = 0;
f(1,2) = 1;
f(2,1) = 0;
f(2,2) = -1/eps;
function g = g(x)
% Evaluate vector.
g=zeros(2,1);
function display_plot(xkeep, ykeep, npkeep, espkeep)
% 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 two curves.
plot(xkeep(1:npkeep(1),1), ykeep(1:npkeep(1),1), '-+', ...
     xkeep(1:npkeep(2),2), ykeep(1:npkeep(2),2), ':x');
set(gca, 'YLim', [0 1.1]);
% Add title.
title('Solution to Simple Second-order Problem', titleFmt{:});
% Label the axes.
xlabel('x', labFmt{:});
ylabel('Solution', labFmt{:});
% Label the curves.
legend(['\epsilon = ', num2str(espkeep(1))], ...
    ['\epsilon = ', num2str(espkeep(2))], ...
    'Location', 'Best');
 
nag_ode_bvp_fd_lin_gen example program results

Problem with epsilon = 1.00e-01

Approximate solution on final mesh of 15 points 
      X(I)       Y(1,I)
     0.0000      0.0000
     0.0278      0.2425
     0.0556      0.4263
     0.1111      0.6708
     0.1667      0.8112
     0.2222      0.8917
     0.2778      0.9379
     0.3333      0.9644
     0.4444      0.9883
     0.5556      0.9962
     0.6667      0.9988
     0.7500      0.9995
     0.8333      0.9998
     0.9167      0.9999
     1.0000      1.0000

Problem with epsilon = 1.00e-02

Approximate solution on final mesh of 49 points 
      X(I)       Y(1,I)
     0.0000      0.0000
     0.0009      0.0884
     0.0019      0.1690
     0.0028      0.2425
     0.0037      0.3095
     0.0046      0.3706
     0.0056      0.4262
     0.0065      0.4770
     0.0074      0.5232
     0.0083      0.5654
     0.0093      0.6038
     0.0111      0.6708
     0.0130      0.7265
     0.0148      0.7727
     0.0167      0.8111
     0.0185      0.8431
     0.0204      0.8696
     0.0222      0.8916
     0.0241      0.9100
     0.0259      0.9252
     0.0278      0.9378
     0.0306      0.9529
     0.0333      0.9643
     0.0361      0.9730
     0.0389      0.9795
     0.0417      0.9845
     0.0444      0.9883
     0.0472      0.9911
     0.0500      0.9933
     0.0528      0.9949
     0.0556      0.9961
     0.0648      0.9985
     0.0741      0.9994
     0.0833      0.9998
     0.0926      0.9999
     0.1019      1.0000
     0.1111      1.0000
     0.1389      1.0000
     0.1667      1.0000
     0.2222      1.0000
     0.2778      1.0000
     0.3333      1.0000
     0.4444      1.0000
     0.5556      1.0000
     0.6667      1.0000
     0.7500      1.0000
     0.8333      1.0000
     0.9167      1.0000
     1.0000      1.0000

function d02gb_example
% For communication with f.
global eps;

% Set up initial values before calling NAG routine for the first time.
mnp = 70;
a = 0;
b = 1;
tol = 0.001;
c = [1, 0; 0, 0];
d = [0, 0; 1, 0];
gam = [0; 1];
x = zeros(1,mnp);
np = 0;
n = 2;
xkeep = zeros(1,2);
npkeep = zeros(2);
ykeep = zeros(1,2);
espkeep = zeros(2);

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

for i = 1:2
    eps = double(10)^(-i);

    [c, d, gam, x, y, np, ifail] = ...
        d02gb(a, b, tol, @f, @g, c, d, gam, x,...
        int64(np), 'n', int64(n), 'mnp', int64(mnp));
    if (ifail ~= 0)
        % Problems in integration, or incorrect parameters.  Print
        % message and exit.
        error('Warning: d02gb returned with ifail = %d',ifail);
    else
        % Output results.
        fprintf('\nProblem with epsilon = %1.2e\n\n',eps);
        fprintf('Approximate solution on final mesh of %1.0f points \n',np)
        disp('      X(I)       Y(1,I)');
        for j = 1:int32(np) % can't use int64 in loop range
            fprintf('     %1.4f    %8.4f\n',x(j),y(1,j));
            xkeep(j,i) = x(j);
            ykeep(j,i) = y(1,j);
        end
    end
    % Store the number of points and epsilon for use in plot.
    npkeep(i) = np;
    espkeep(i) = eps;
end
% Plot results.
fig = figure('Number', 'off');
display_plot(xkeep, ykeep, npkeep, espkeep)

function f = f(x)
% For communication with main routine.
global eps;

% Evaluate matrix.
f=zeros(2,2);
f(1,1) = 0;
f(1,2) = 1;
f(2,1) = 0;
f(2,2) = -1/eps;
function g = g(x)
% Evaluate vector.
g=zeros(2,1);
function display_plot(xkeep, ykeep, npkeep, espkeep)
% 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 two curves.
plot(xkeep(1:npkeep(1),1), ykeep(1:npkeep(1),1), '-+', ...
     xkeep(1:npkeep(2),2), ykeep(1:npkeep(2),2), ':x');
set(gca, 'YLim', [0 1.1]);
% Add title.
title('Solution to Simple Second-order Problem', titleFmt{:});
% Label the axes.
xlabel('x', labFmt{:});
ylabel('Solution', labFmt{:});
% Label the curves.
legend(['\epsilon = ', num2str(espkeep(1))], ...
    ['\epsilon = ', num2str(espkeep(2))], ...
    'Location', 'Best');
 
d02gb example program results

Problem with epsilon = 1.00e-01

Approximate solution on final mesh of 15 points 
      X(I)       Y(1,I)
     0.0000      0.0000
     0.0278      0.2425
     0.0556      0.4263
     0.1111      0.6708
     0.1667      0.8112
     0.2222      0.8917
     0.2778      0.9379
     0.3333      0.9644
     0.4444      0.9883
     0.5556      0.9962
     0.6667      0.9988
     0.7500      0.9995
     0.8333      0.9998
     0.9167      0.9999
     1.0000      1.0000

Problem with epsilon = 1.00e-02

Approximate solution on final mesh of 49 points 
      X(I)       Y(1,I)
     0.0000      0.0000
     0.0009      0.0884
     0.0019      0.1690
     0.0028      0.2425
     0.0037      0.3095
     0.0046      0.3706
     0.0056      0.4262
     0.0065      0.4770
     0.0074      0.5232
     0.0083      0.5654
     0.0093      0.6038
     0.0111      0.6708
     0.0130      0.7265
     0.0148      0.7727
     0.0167      0.8111
     0.0185      0.8431
     0.0204      0.8696
     0.0222      0.8916
     0.0241      0.9100
     0.0259      0.9252
     0.0278      0.9378
     0.0306      0.9529
     0.0333      0.9643
     0.0361      0.9730
     0.0389      0.9795
     0.0417      0.9845
     0.0444      0.9883
     0.0472      0.9911
     0.0500      0.9933
     0.0528      0.9949
     0.0556      0.9961
     0.0648      0.9985
     0.0741      0.9994
     0.0833      0.9998
     0.0926      0.9999
     0.1019      1.0000
     0.1111      1.0000
     0.1389      1.0000
     0.1667      1.0000
     0.2222      1.0000
     0.2778      1.0000
     0.3333      1.0000
     0.4444      1.0000
     0.5556      1.0000
     0.6667      1.0000
     0.7500      1.0000
     0.8333      1.0000
     0.9167      1.0000
     1.0000      1.0000


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–2013