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_coll_nlin_interp (d02ty)

Purpose

nag_ode_bvp_coll_nlin_interp (d02ty) interpolates on the solution of a general two-point boundary value problem computed by nag_ode_bvp_coll_nlin (d02tk).

Syntax

[y, rwork, ifail] = d02ty(x, neq, mmax, rwork, iwork)
[y, rwork, ifail] = nag_ode_bvp_coll_nlin_interp(x, neq, mmax, rwork, iwork)

Description

nag_ode_bvp_coll_nlin_interp (d02ty) and its associated functions (nag_ode_bvp_coll_nlin (d02tk), nag_ode_bvp_coll_nlin_setup (d02tv), nag_ode_bvp_coll_nlin_contin (d02tx) and nag_ode_bvp_coll_nlin_diag (d02tz)) solve the two-point boundary value problem for a nonlinear mixed order system of ordinary differential equations
y1(m1) (x) = f1 (x,y1,y1(1),,y1(m11),y2,,yn(mn1))
y2(m2) (x) = f2 (x,y1,y1(1),,y1(m11),y2,,yn(mn1))
yn(mn) (x) = fn (x,y1,y1(1),,y1(m11),y2,,yn(mn1))
y1(m1) (x) = f1 (x,y1,y1(1),,y1(m1-1),y2,,yn(mn-1)) y2(m2) (x) = f2 (x,y1,y1(1),,y1(m1-1),y2,,yn(mn-1)) yn(mn) (x) = fn (x,y1,y1(1),,y1(m1-1),y2,,yn(mn-1))
over an interval [a,b][a,b] subject to pp ( > 0>0) nonlinear boundary conditions at aa and qq ( > 0>0) nonlinear boundary conditions at bb, where p + q = i = 1n mi p+q = i=1 n mi . Note that yi(m)(x)yi (m) (x) is the mmth derivative of the iith solution component. Hence yi(0)(x) = yi(x)yi (0) (x)=yi(x). The left boundary conditions at aa are defined as
gi(z(y(a))) = 0,  i = 1,2,,p,
gi(z(y(a)))=0,  i=1,2,,p,
and the right boundary conditions at bb as
gj(z(y(b))) = 0,  j = 1,2,,q,
g-j(z(y(b)))=0,  j=1,2,,q,
where y = (y1,y2,,yn)y=(y1,y2,,yn) and
z(y(x)) = (y1(x), y1(1) (x) ,, y1(m11) (x) ,y2(x),, yn(mn1) (x) ) .
z(y(x)) = (y1(x), y1(1) (x) ,, y1(m1-1) (x) ,y2(x),, yn(mn-1) (x) ) .
First, nag_ode_bvp_coll_nlin_setup (d02tv) must be called to specify the initial mesh, error requirements and other details. Then, nag_ode_bvp_coll_nlin (d02tk) can be used to solve the boundary value problem. After successful computation, nag_ode_bvp_coll_nlin_diag (d02tz) can be used to ascertain details about the final mesh and other details of the solution procedure, and nag_ode_bvp_coll_nlin_interp (d02ty) can be used to compute the approximate solution anywhere on the interval [a,b][a,b] using interpolation.
The functions are based on modified versions of the codes COLSYS and COLNEW (see Ascher et al. (1979) and Ascher and Bader (1987)). A comprehensive treatment of the numerical solution of boundary value problems can be found in Ascher et al. (1988) and Keller (1992).

References

Ascher U M and Bader G (1987) A new basis implementation for a mixed order boundary value ODE solver SIAM J. Sci. Stat. Comput. 8 483–500
Ascher U M, Christiansen J and Russell R D (1979) A collocation solver for mixed order systems of boundary value problems Math. Comput. 33 659–679
Ascher U M, Mattheij R M M and Russell R D (1988) Numerical Solution of Boundary Value Problems for Ordinary Differential Equations Prentice–Hall
Grossman C (1992) Enclosures of the solution of the Thomas–Fermi equation by monotone discretization J. Comput. Phys. 98 26–32
Keller H B (1992) Numerical Methods for Two-point Boundary-value Problems Dover, New York

Parameters

Compulsory Input Parameters

1:     x – double scalar
xx, the independent variable.
Constraint: axbaxb.
2:     neq – int64int32nag_int scalar
The number of differential equations.
Constraint: neqneq must be the same value as supplied to nag_ode_bvp_coll_nlin_setup (d02tv).
3:     mmax – int64int32nag_int scalar
The maximal order of the differential equations, max (mi)max(mi), for i = 1,2,,neqi=1,2,,neq.
Constraint: mmaxmmax must contain the maximum value of the components of the parameter m as supplied to nag_ode_bvp_coll_nlin_setup (d02tv).
4:     rwork( : :) – double array
Note: the dimension of the array rwork must be at least lrworklrwork (see nag_ode_bvp_coll_nlin_setup (d02tv)).
This must be the same array as supplied to nag_ode_bvp_coll_nlin (d02tk) and must remain unchanged between calls.
5:     iwork( : :) – int64int32nag_int array
Note: the dimension of the array iwork must be at least liworkliwork (see nag_ode_bvp_coll_nlin_setup (d02tv)).
This must be the same array as supplied to nag_ode_bvp_coll_nlin (d02tk) and must remain unchanged between calls.

Optional Input Parameters

None.

Input Parameters Omitted from the MATLAB Interface

None.

Output Parameters

1:     y(neq,0 : mmax10:mmax-1) – double array
y(i,j)yij contains an approximation to yi(j)(x)yi (j) (x), for i = 1,2,,neqi=1,2,,neq and j = 0,1,,mi1j=0,1,,mi-1. The remaining elements of y (where mi < mmaxmi<mmax) are initialized to 0.00.0.
2:     rwork( : :) – double array
Note: the dimension of the array rwork must be at least lrworklrwork (see nag_ode_bvp_coll_nlin_setup (d02tv)).
Contains information about the solution for use on subsequent calls to associated functions.
3:     ifail – int64int32nag_int scalar
ifail = 0ifail=0 unless the function detects an error (see [Error Indicators and Warnings]).

Error Indicators and Warnings

Note: nag_ode_bvp_coll_nlin_interp (d02ty) may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the function:
  ifail = 1ifail=1
On entry, an invalid value for neq, mmax (max (mi)max(mi) for some ii) or x (outside the range [a,b][a,b]) was detected, or an invalid call to nag_ode_bvp_coll_nlin_interp (d02ty) was made, for example without a previous call to the solver function nag_ode_bvp_coll_nlin (d02tk).
  ifail = 2ifail=2
The solver function nag_ode_bvp_coll_nlin (d02tk) did not converge to a solution or did not satisfy the error requirements. The last solution computed by nag_ode_bvp_coll_nlin (d02tk), for which convergence was obtained, has been used for interpolation by nag_ode_bvp_coll_nlin_interp (d02ty). The results returned by nag_ode_bvp_coll_nlin_interp (d02ty) should be treated with extreme caution as regarding either their quality or accuracy. See Section [Further Comments].

Accuracy

If nag_ode_bvp_coll_nlin_interp (d02ty) returns the value ifail = 0ifail=0, the computed values of the solution components yiyi should be of similar accuracy to that specified by the parameter tols of nag_ode_bvp_coll_nlin_setup (d02tv). Note that during the solution process the error in the derivatives yi(j)yi(j), for j = 1,2,,mi1j=1,2,,mi-1, has not been controlled and that the derivative values returned by nag_ode_bvp_coll_nlin_interp (d02ty) are computed via differentiation of the piecewise polynomial approximation to yiyi. See also Section [Further Comments].

Further Comments

If nag_ode_bvp_coll_nlin_interp (d02ty) returns the value ifail = 2ifail=2, and the solver function nag_ode_bvp_coll_nlin (d02tk) returned ifail = 5ifail=5, then the accuracy of the interpolated values may be proportional to the quantity ermx as returned by nag_ode_bvp_coll_nlin_diag (d02tz).
If nag_ode_bvp_coll_nlin (d02tk) returned any other nonzero value for ifail, then nothing can be said regarding either the quality or accuracy of the values computed by nag_ode_bvp_coll_nlin_interp (d02ty).

Example

The following example is used to illustrate that a system with singular coefficients can be treated without modification of the system definition. See also nag_ode_bvp_coll_nlin (d02tk), nag_ode_bvp_coll_nlin_setup (d02tv), nag_ode_bvp_coll_nlin_contin (d02tx) and nag_ode_bvp_coll_nlin_diag (d02tz), for the illustration of other facilities.
Consider the Thomas–Fermi equation used in the investigation of potentials and charge densities of ionized atoms. See Grossman (1992), for example, and the references therein. The equation is
y = x1 / 2y3 / 2
y=x-1/2y3/2
with boundary conditions
y(0) = 1,   y(a) = 0,   a > 0.
y(0)= 1,   y(a)= 0,   a> 0.
The coefficient x1 / 2x-1/2 implies a singularity at the left-hand boundary x = 0x=0.
We use the initial approximation y(x) = 1x / ay(x)=1-x/a, which satisfies the boundary conditions, on a uniform mesh of six points. For illustration we choose a = 1a=1, as in Grossman (1992). Note that in ffun and fjac (see nag_ode_bvp_coll_nlin (d02tk)) we have taken the precaution of setting the function value and Jacobian value to 0.00.0 in case a value of yy becomes negative, although starting from our initial solution profile this proves unnecessary during the solution phase. Of course the true solution y(x)y(x) is positive for all x < ax<a.
function nag_ode_bvp_coll_nlin_interp_example
global a; % For communication with local functions

% Initialize variables and arrays.
neq = 1;
nlbc = 1;
nrbc = 1;
ncol = 4;
mmax = 2;
m = [2];
tols = [1.0e-05];

% Set values for problem-specific physical parameters.
a = 1.0;

% Set up the mesh.
nmesh = 6;
mxmesh = 100;
ipmesh = zeros(mxmesh, 1);
mesh = zeros(mxmesh, 1);

% Set location of mesh boundaries, then calculate initial spacing.
mesh(1) = 0.0;
mesh(nmesh) = a;
mstep = (mesh(nmesh) - mesh(1))/double(nmesh-1);
for i = 2:nmesh-1
    mesh(i) = mstep*double(i-1);
    ipmesh(i) = 2;
end

% Specify mesh end points as fixed.
ipmesh(1)     = 1;
ipmesh(nmesh) = 1;

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

% nag_ode_bvp_coll_nlin_setup is a setup routine to be called prior to nag_ode_bvp_coll_nlin.
[work, iwork, ifail] = nag_ode_bvp_coll_nlin_setup(int64(m), int64(nlbc), int64(nrbc), ...
    int64(ncol), tols, int64(nmesh), mesh, int64(ipmesh));
if ifail ~= 0
    % Unsuccessful call.  Print message and exit.
    error('Warning: nag_ode_bvp_coll_nlin_setup returned with ifail = %1d ',ifail);
end
fprintf('\n Tolerance = %8.1e  A = %8.2f\n\n', ...
    tols(1), a);

% Call nag_ode_bvp_coll_nlin to solve BVP for this set of parameters.
[work, iwork, ifail] = nag_ode_bvp_coll_nlin(@ffun, @fjac, ...
    @gafun, @gbfun, @gajac, @gbjac,...
    @guess, work, iwork);
if ifail ~= 0
    % Unsuccessful call.  Print message and exit.
    error('Warning: nag_ode_bvp_coll_nlin returned with ifail = %1d ',ifail);
end

% Call nag_ode_bvp_coll_nlin_diag to extract mesh from solution.
[nmesh, mesh, ipmesh, ermx, iermx, ijermx, ifail] = nag_ode_bvp_coll_nlin_diag( ...
    int64(mxmesh), work, iwork);

% Output mesh results.
fprintf(' Used a mesh of %d points\n', nmesh);
fprintf([' Maximum error = %10.2e in interval %d for component %d\n\n', ...
    ' Mesh points:\n'], ermx, iermx, ijermx);
for imesh = 1:int32(nmesh) % can't use int64 in loop range.
    fprintf( '%4d(%d) %6.4f', imesh, ipmesh(imesh), mesh(imesh));
    if mod(imesh, 4) == 0
        fprintf('\n');
    end
end

% Output solution, and store it for plotting.
xarray = zeros(nmesh, 1);
yarray = zeros(nmesh, 2);
fprintf('\n\n Computed solution\n');
fprintf('       x     solution   derivative\n');
for imesh = 1:int32(nmesh) % can't use int64 in loop range.
    % Call nag_ode_bvp_coll_nlin_interp to perform interpolation on the solution.
    [y, work, ifail] = nag_ode_bvp_coll_nlin_interp(mesh(imesh), int64(neq), int64(mmax), ...
        work, iwork);
    fprintf(' %8.2f %10.5f %10.5f\n', mesh(imesh), y(1,1), y(1,2));
    xarray(imesh) = mesh(imesh);
    for jcomp = 1:2
        yarray(imesh, jcomp) = y(1, jcomp);
    end
end
% Plot results.
fig = figure('Number', 'off');
display_plot(xarray, yarray);

function [f] = ffun(x, y, neq, m)
% Evaluate derivative functions (rhs of system of ODEs).

f = zeros(neq, 1);
if y(1,1) < 0.0
    f(1,1) = 0.0;
    fprintf(' In ffun\n');
else
    f(1,1) = y(1,1)^1.5/sqrt(x);
end
function [dfdy] = fjac(x, y, neq, m)
% Evaluate Jacobians (partial derivatives of f).

dfdy = zeros(neq, neq, 1);
if y(1,1) < 0.0
    dfdy(1,1,1) = 0.0;
    fprintf(' In fjac\n');
else
    dfdy(1,1,1) = 1.5*sqrt(y(1,1))/sqrt(x);
end
function [ga] = gafun(ya, neq, m, nlbc)
% Evaluate boundary conditions at left-hand end of range.

ga = zeros(nlbc, 1);
ga(1,1) = ya(1,1) - 1.0;
function [dgady] = gajac(ya, neq, m, nlbc)
% Evaluate Jacobians (partial derivatives of ga).

dgady = zeros(nlbc, neq, 1);
dgady(1,1,1) = 1.0;
function [gb] = gbfun(yb, neq, m, nrbc)
% Evaluate boundary conditions at right-hand end of range.

gb = zeros(nrbc, 1);
gb(1,1) = yb(1,1);
function [dgbdy] = gbjac(yb, neq, m, nrbc)
% Evaluate Jacobians (partial derivatives of gb).

dgbdy = zeros(nrbc, neq, 1);
dgbdy(1,1,1) = 1.0;
function [y, dym] = guess(x, neq, m)
% Evaluate initial approximations to solution components and derivatives.

global a; % For communication with main routine.
y = zeros(neq, 2);
dym = zeros(neq, 1);
y(1,1) =  1.0 - x/a;
y(1,2) = -1.0/a;

dym(1) = 0.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 both curves.
[hline] = plot(x, y(:,1), x, y(:,2));
% Add title.
title(['Thomas-Fermi Equation for Determining Effective Nuclear ', ...
    'Charge in Heavy Atoms'], titleFmt{:});
% Label the axes.
xlabel('Relative Distance', labFmt{:});
ylabel('Effective Nuclear Charge', labFmt{:});
% Add a legend.
legend('Nuclear Charge','Charge Gradient','Location','Best')
% Set some features of the three lines.
set(hline(1), 'Linewidth', 0.25, 'Marker', '+', 'Line', '-');
set(hline(2), 'Linewidth', 0.25, 'Marker', 'x', 'Line', '--');
 
nag_ode_bvp_coll_nlin_interp example program results


 Tolerance =  1.0e-05  A =     1.00

 Used a mesh of 11 points
 Maximum error =   3.09e-06 in interval 1 for component 1

 Mesh points:
   1(1) 0.0000   2(3) 0.1000   3(2) 0.2000   4(3) 0.3000
   5(2) 0.4000   6(3) 0.5000   7(2) 0.6000   8(3) 0.7000
   9(2) 0.8000  10(3) 0.9000  11(1) 1.0000

 Computed solution
       x     solution   derivative
     0.00    1.00000   -1.84496
     0.10    0.84944   -1.32330
     0.20    0.72721   -1.13911
     0.30    0.61927   -1.02776
     0.40    0.52040   -0.95468
     0.50    0.42754   -0.90583
     0.60    0.33867   -0.87372
     0.70    0.25239   -0.85369
     0.80    0.16764   -0.84248
     0.90    0.08368   -0.83756
     1.00    0.00000   -0.83655

function d02ty_example
global a; % For communication with local functions

% Initialize variables and arrays.
neq = 1;
nlbc = 1;
nrbc = 1;
ncol = 4;
mmax = 2;
m = [2];
tols = [1.0e-05];

% Set values for problem-specific physical parameters.
a = 1.0;

% Set up the mesh.
nmesh = 6;
mxmesh = 100;
ipmesh = zeros(mxmesh, 1);
mesh = zeros(mxmesh, 1);

% Set location of mesh boundaries, then calculate initial spacing.
mesh(1) = 0.0;
mesh(nmesh) = a;
mstep = (mesh(nmesh) - mesh(1))/double(nmesh-1);
for i = 2:nmesh-1
    mesh(i) = mstep*double(i-1);
    ipmesh(i) = 2;
end

% Specify mesh end points as fixed.
ipmesh(1)     = 1;
ipmesh(nmesh) = 1;

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

% d02tv is a setup routine to be called prior to d02tk.
[work, iwork, ifail] = d02tv(int64(m), int64(nlbc), int64(nrbc), ...
    int64(ncol), tols, int64(nmesh), mesh, int64(ipmesh));
if ifail ~= 0
    % Unsuccessful call.  Print message and exit.
    error('Warning: d02tv returned with ifail = %1d ',ifail);
end
fprintf('\n Tolerance = %8.1e  A = %8.2f\n\n', ...
    tols(1), a);

% Call d02tk to solve BVP for this set of parameters.
[work, iwork, ifail] = d02tk(@ffun, @fjac, ...
    @gafun, @gbfun, @gajac, @gbjac,...
    @guess, work, iwork);
if ifail ~= 0
    % Unsuccessful call.  Print message and exit.
    error('Warning: d02tk returned with ifail = %1d ',ifail);
end

% Call d02tz to extract mesh from solution.
[nmesh, mesh, ipmesh, ermx, iermx, ijermx, ifail] = d02tz( ...
    int64(mxmesh), work, iwork);

% Output mesh results.
fprintf(' Used a mesh of %d points\n', nmesh);
fprintf([' Maximum error = %10.2e in interval %d for component %d\n\n', ...
    ' Mesh points:\n'], ermx, iermx, ijermx);
for imesh = 1:int32(nmesh) % can't use int64 in loop range.
    fprintf( '%4d(%d) %6.4f', imesh, ipmesh(imesh), mesh(imesh));
    if mod(imesh, 4) == 0
        fprintf('\n');
    end
end

% Output solution, and store it for plotting.
xarray = zeros(nmesh, 1);
yarray = zeros(nmesh, 2);
fprintf('\n\n Computed solution\n');
fprintf('       x     solution   derivative\n');
for imesh = 1:int32(nmesh) % can't use int64 in loop range.
    % Call d02ty to perform interpolation on the solution.
    [y, work, ifail] = d02ty(mesh(imesh), int64(neq), int64(mmax), ...
        work, iwork);
    fprintf(' %8.2f %10.5f %10.5f\n', mesh(imesh), y(1,1), y(1,2));
    xarray(imesh) = mesh(imesh);
    for jcomp = 1:2
        yarray(imesh, jcomp) = y(1, jcomp);
    end
end
% Plot results.
fig = figure('Number', 'off');
display_plot(xarray, yarray);

function [f] = ffun(x, y, neq, m)
% Evaluate derivative functions (rhs of system of ODEs).

f = zeros(neq, 1);
if y(1,1) < 0.0
    f(1,1) = 0.0;
    fprintf(' In ffun\n');
else
    f(1,1) = y(1,1)^1.5/sqrt(x);
end
function [dfdy] = fjac(x, y, neq, m)
% Evaluate Jacobians (partial derivatives of f).

dfdy = zeros(neq, neq, 1);
if y(1,1) < 0.0
    dfdy(1,1,1) = 0.0;
    fprintf(' In fjac\n');
else
    dfdy(1,1,1) = 1.5*sqrt(y(1,1))/sqrt(x);
end
function [ga] = gafun(ya, neq, m, nlbc)
% Evaluate boundary conditions at left-hand end of range.

ga = zeros(nlbc, 1);
ga(1,1) = ya(1,1) - 1.0;
function [dgady] = gajac(ya, neq, m, nlbc)
% Evaluate Jacobians (partial derivatives of ga).

dgady = zeros(nlbc, neq, 1);
dgady(1,1,1) = 1.0;
function [gb] = gbfun(yb, neq, m, nrbc)
% Evaluate boundary conditions at right-hand end of range.

gb = zeros(nrbc, 1);
gb(1,1) = yb(1,1);
function [dgbdy] = gbjac(yb, neq, m, nrbc)
% Evaluate Jacobians (partial derivatives of gb).

dgbdy = zeros(nrbc, neq, 1);
dgbdy(1,1,1) = 1.0;
function [y, dym] = guess(x, neq, m)
% Evaluate initial approximations to solution components and derivatives.

global a; % For communication with main routine.
y = zeros(neq, 2);
dym = zeros(neq, 1);
y(1,1) =  1.0 - x/a;
y(1,2) = -1.0/a;

dym(1) = 0.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 both curves.
[hline] = plot(x, y(:,1), x, y(:,2));
% Add title.
title(['Thomas-Fermi Equation for Determining Effective Nuclear ', ...
    'Charge in Heavy Atoms'], titleFmt{:});
% Label the axes.
xlabel('Relative Distance', labFmt{:});
ylabel('Effective Nuclear Charge', labFmt{:});
% Add a legend.
legend('Nuclear Charge','Charge Gradient','Location','Best')
% Set some features of the three lines.
set(hline(1), 'Linewidth', 0.25, 'Marker', '+', 'Line', '-');
set(hline(2), 'Linewidth', 0.25, 'Marker', 'x', 'Line', '--');
 
d02ty example program results


 Tolerance =  1.0e-05  A =     1.00

 Used a mesh of 11 points
 Maximum error =   3.09e-06 in interval 1 for component 1

 Mesh points:
   1(1) 0.0000   2(3) 0.1000   3(2) 0.2000   4(3) 0.3000
   5(2) 0.4000   6(3) 0.5000   7(2) 0.6000   8(3) 0.7000
   9(2) 0.8000  10(3) 0.9000  11(1) 1.0000

 Computed solution
       x     solution   derivative
     0.00    1.00000   -1.84496
     0.10    0.84944   -1.32330
     0.20    0.72721   -1.13911
     0.30    0.61927   -1.02776
     0.40    0.52040   -0.95468
     0.50    0.42754   -0.90583
     0.60    0.33867   -0.87372
     0.70    0.25239   -0.85369
     0.80    0.16764   -0.84248
     0.90    0.08368   -0.83756
     1.00    0.00000   -0.83655


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