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_sys (d02jb)

Purpose

nag_ode_bvp_coll_sys (d02jb) solves a regular linear two-point boundary value problem for a system of ordinary differential equations by Chebyshev series using collocation and least squares.

Syntax

[c, ifail] = d02jb(n, cf, bc, x0, x1, k1, kp)
[c, ifail] = nag_ode_bvp_coll_sys(n, cf, bc, x0, x1, k1, kp)

Description

nag_ode_bvp_coll_sys (d02jb) calculates the solution of a regular two-point boundary value problem for a regular linear nnth-order system of first-order ordinary differential equations as a Chebyshev series in the interval (x0,x1)(x0,x1). The differential equation
y = A(x)y + r(x)
y=A(x)y+r(x)
is defined by cf, and the boundary conditions at the points x0x0 and x1x1 are defined by bc.
You specify the degree of Chebyshev series required, k11k1-1, and the number of collocation points, kp. The function sets up a system of linear equations for the Chebyshev coefficients, nn equations for each collocation point and one for each boundary condition. The boundary conditions are solved exactly, and the remaining equations are then solved by a least squares method. The result produced is a set of coefficients for a Chebyshev series solution for each component of the solution of the system of differential equations on an interval normalized to (1,1)(-1,1).
nag_fit_1dcheb_eval2 (e02ak) can be used to evaluate the components of the solution at any point on the interval (x0,x1)(x0,x1) – see Section [Example] for an example. nag_fit_1dcheb_deriv (e02ah) followed by nag_fit_1dcheb_eval2 (e02ak) can be used to evaluate their derivatives.

References

Picken S M (1970) Algorithms for the solution of differential equations in Chebyshev-series by the selected points method Report Math. 94 National Physical Laboratory

Parameters

Compulsory Input Parameters

1:     n – int64int32nag_int scalar
nn, the order of the system of differential equations.
Constraint: n1n1.
2:     cf – function handle or string containing name of m-file
cf defines the system of differential equations (see Section [Description]). It must return the value of a coefficient function ai,j(x)ai,j(x), of AA, at a given point xx, or of a right-hand side function ri(x)ri(x) if j = 0j=0.
[result] = cf(ii, j, x)

Input Parameters

1:     ii – int64int32nag_int scalar
2:     j – int64int32nag_int scalar
Indicate the function to be evaluated, namely ai,j(x)ai,j(x) if 1jn1jn, or ri(x)ri(x) if j = 0j=0.
1in1in, 0jn0jn.
3:     x – double scalar
The point at which the function is to be evaluated.

Output Parameters

1:     result – double scalar
The result of the function.
3:     bc – function handle or string containing name of m-file
bc defines the nn boundary conditions, which have the form yk(x0) = syk(x0)=s or yk(x1) = syk(x1)=s. The boundary conditions may be specified in any order.
[j, rhs] = bc(ii)

Input Parameters

1:     ii – int64int32nag_int scalar
The index of the boundary condition to be defined.

Output Parameters

1:     j – int64int32nag_int scalar
Must be set to k-k if the iith boundary condition is yk(x0) = syk(x0)=s, or to + k+k if it is yk(x1) = syk(x1)=s.
j must not be set to the same value kk for two different values of ii.
2:     rhs – double scalar
The value ss.
4:     x0 – double scalar
5:     x1 – double scalar
The left- and right-hand boundaries, x0x0 and x1x1, respectively.
Constraint: x1 > x0x1>x0.
6:     k1 – int64int32nag_int scalar
The number of coefficients to be returned in the Chebyshev series representation of the components of the solution (hence the degree of the polynomial approximation is k11k1-1).
Constraint: k12k12.
7:     kp – int64int32nag_int scalar
The number of collocation points to be used.
Constraint: kpk11kpk1-1.

Optional Input Parameters

None.

Input Parameters Omitted from the MATLAB Interface

ldc w lw iw liw

Output Parameters

1:     c(ldc,n) – double array
ldck1ldck1.
The computed Chebyshev coefficients of the kkth component of the solution, ykyk; that is, the computed solution is:
k1
yk = c(i,k)Ti1(x),  1kn
i = 1
yk=i=1k1cikTi-1(x),  1kn
where Ti(x)Ti(x) is the iith Chebyshev polynomial of the first kind, and denotes that the first coefficient, c(1,k)c1k, is halved.
2:     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:
  ifail = 1ifail=1
On entry,n < 1n<1,
orx0x1x0x1,
ork1 < 2k1<2,
orkp < k11kp<k1-1,
orldc < k1ldc<k1.
  ifail = 2ifail=2
On entry,lw < 2 × n × (kp + 1) × (n × k1 + 1) + 7 × n × k1lw<2×n×(kp+1)×(n×k1+1)+7×n×k1,
orliw < n × (k1 + 2)liw<n×(k1+2) (i.e., insufficient workspace).
  ifail = 3ifail=3
Either the boundary conditions are not linearly independent (that is, in bc the variable j is set to the same value kk for two different values of ii), or the rank of the matrix of equations for the coefficients is less than the number of unknowns. Increasing kp may overcome this latter problem.
  ifail = 4ifail=4
The least squares function nag_linsys_real_gen_lsqsol (f04am) has failed to correct the first approximate solution (see nag_linsys_real_gen_lsqsol (f04am)).

Accuracy

The Chebyshev coefficients are determined by a stable numerical method. The accuracy of the approximate solution may be checked by varying the degree of the polynomials and the number of collocation points (see Section [Further Comments]).

Further Comments

The time taken by nag_ode_bvp_coll_sys (d02jb) depends on the size and complexity of the differential system, the degree of the polynomial solution, and the number of matching points.
The collocation points in the interval (x0,x1)(x0,x1) are chosen to be the extrema of the appropriate shifted Chebyshev polynomial. If kp = k11kp=k1-1, then the least squares solution reduces to the solution of a system of linear equations, and true collocation results.
The accuracy of the solution may be checked by repeating the calculation with different values of k1 and with kp fixed but kpk11kpk1-1. If the Chebyshev coefficients decrease rapidly for each component (and consistently for various k1 and kp), the size of the last two or three gives an indication of the error. If the Chebyshev coefficients do not decay rapidly, it is likely that the solution cannot be well-represented by Chebyshev series. Note that the Chebyshev coefficients are calculated for the interval (1,1)(-1,1).
Linear systems of high-order equations in their original form, singular problems, and, indirectly, nonlinear problems can be solved using nag_ode_bvp_coll_nth_comp (d02tg).

Example

This example solves the equation
y + y = 1
y + y = 1
with boundary conditions
y(1) = y(1) = 0 .
y(-1) = y(1) = 0 .
The equation is written as the first-order system
(y1)
y2
=
(01)
1 0
(y1)
y2
+
(0)
1
y1 y2 = 0 1 -1 0 y1 y2 + 0 1
for solution by nag_ode_bvp_coll_sys (d02jb) and the boundary conditions are written
y1(1) = y1(1) = 0.
y1(- 1)=y1(1)=0.
We use k1 = 4k1=4, 66 and 88, and kp = 10kp=10 and 1515, so that the different Chebyshev series may be compared. The solution for k1 = 8k1=8 and kp = 15kp=15 is evaluated by nag_fit_1dcheb_eval2 (e02ak) at nine equally spaced points over the interval (1,1)(-1,1).
function nag_ode_bvp_coll_sys_example
% Set up initial values.
n = 2;
k1max = 8;
kpmax = 15;
lw = 2*(kpmax + n)*(k1max + 1) + 7*k1max;
liw = n * k1max + int32(2);
x0 = -1.0;
x1 = 1.0;
c = zeros(k1max, n);
w = zeros(1, lw);
y = zeros(1, n);
iw = zeros(1, liw);

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

fprintf('  KP   K1   Chebyshev coefficients\n');
for kp = 10:5:kpmax
    for k1 = 4:2:k1max
        [c, ifail] = nag_ode_bvp_coll_sys(int64(n), @cf, @bc, x0, x1, ...
            int64(k1), int64(kp));
        if (ifail ~= 0)
            % Incorrect parameters.  Print message and exit.
            fprintf('Warning: nag_ode_bvp_coll_nth returned with ifail = %d \n\n',ifail);
            return
        else
            % Output results.
            fprintf('%4d ',kp, k1);
            for jord = 1:n
                for kind = 1:k1
                    fprintf('%7.4f ',c(kind,jord));
                    if mod(kind, 8) == 0 && kind ~= k1
                        fprintf('\n          ');
                    end
                end
                fprintf('\n          ');
            end
            fprintf('\n');
        end
    end
end
% Now prepare to evaluate and plot the last solution.
fprintf('\n');
k1 = 8;
m = 9;
ia1 = 1;
xarray = zeros(m, 1);
yarray = zeros(m, n+1);

fprintf(['Last computed solution evaluated at %1d equally spaced ', ...
    'points\n\n'], m);
fprintf('    X       Y(1)      Y(2)\n');
for i = 1:m
    xarray(i) = (x0*(m-i) + x1*(i-1))/(m-1);
    fprintf('%8.4f  ', xarray(i));

    for jord = 1:n
        % Calling nag_fit_1dcheb_eval2 to evaluate the polynomial from its Chebyshev
        % representation.
        [yarray(i, jord), ifail] = nag_fit_1dcheb_eval2(int64(k1-1), x0, x1, ...
            c(:,jord), int64(ia1), xarray(i));
        fprintf('%8.4f  ',yarray(i, jord));
    end
    fprintf('\n');

    % Calculate the absolute error at this point.
    yarray(i,n+1) = abs(yarray(i,1) - 1.0 + cos(xarray(i))/cos(1.0));
end
% Plot results.
fig = figure('Number', 'off');
display_plot(xarray, yarray);

function [j, rhs] = bc(i)
% Define the boundary conditions.
rhs = 0;
if (i == 1)
    j = int64(1);
else
    j = int64(-1);
end
function result = cf(ii, jj, x)
% Define the differential equation to be solved.
if (jj == ii)
    result = 0;
elseif (ii == 1 && jj == 2)
    result = 1;
elseif (ii == 2 && jj == 1)
    result = -1;
elseif (ii == 1)
    result = 0;
else
    result = 1;
end
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 two of the curves, then add the other one.
[haxes, hline1, hline2] = plotyy(x, y(:,2), x, y(:,3));
hold(haxes(1), 'on');
hline3 = plot(x, y(:,1));
% Set the axis limits and the tick specifications to beautify the plot.
set(haxes(1), 'YLim', [-2 2]);
set(haxes(1), 'XMinorTick', 'on', 'YMinorTick', 'on');
set(haxes(1), 'YTick', [-2.0 -1.0 0.0 1.0 2.0]);
set(haxes(2), 'YLim', [0.0 2e-6]);
set(haxes(2), 'YMinorTick', 'on');
set(haxes(2), 'YTick', [0.0 5e-7 1e-6 1.5e-6 2e-6]);
for iaxis = 1:2
    % These properties must be the same for both sets of axes.
    set(haxes(iaxis), 'XLim', [-1 1]);
    set(haxes(iaxis), 'FontSize', 13);
end
set(gca, 'box', 'off'); % so ticks aren't shown on opposite axes.
% Add title.
title(['Two-point BVP for ODE System using Chebyshev series with ', ...
    'Collocation & Least-Squares'], titleFmt{:});
% Label the axes.
xlabel('x', labFmt{:});
ylabel(haxes(1), 'Solution', labFmt{:});
ylabel(haxes(2), 'Absolute Error', labFmt{:});
% Add a legend.
legend('y''','y','y error','Location','Best');
% Set some features of the three lines.
set(hline1, 'Marker', '+','Linestyle','--');
set(hline2, 'Marker', '*','Linestyle',':');
set(hline3, 'Marker', 'x','Linestyle','-');
 
nag_ode_bvp_coll_sys example program results

  KP   K1   Chebyshev coefficients
  10    4 -0.7798  0.0000  0.3899 -0.0000 
          -0.0000  1.5751  0.0000 -0.0629 
          
  10    6 -0.8326 -0.0000  0.4253  0.0000 -0.0090 -0.0000 
          -0.0000  1.6290  0.0000 -0.0724 -0.0000  0.0009 
          
  10    8 -0.8325 -0.0000  0.4253  0.0000 -0.0092 -0.0000  0.0001  0.0000 
          -0.0000  1.6289  0.0000 -0.0724 -0.0000  0.0009  0.0000 -0.0000 
          
  15    4 -0.7829  0.0000  0.3914 -0.0000 
           0.0000  1.5778  0.0000 -0.0631 
          
  15    6 -0.8326 -0.0000  0.4253  0.0000 -0.0090  0.0000 
           0.0000  1.6290  0.0000 -0.0724 -0.0000  0.0009 
          
  15    8 -0.8325 -0.0000  0.4253  0.0000 -0.0092  0.0000  0.0001 -0.0000 
           0.0000  1.6289  0.0000 -0.0724 -0.0000  0.0009  0.0000 -0.0000 
          

Last computed solution evaluated at 9 equally spaced points

    X       Y(1)      Y(2)
 -1.0000    0.0000   -1.5574  
 -0.7500   -0.3542   -1.2616  
 -0.5000   -0.6242   -0.8873  
 -0.2500   -0.7933   -0.4579  
  0.0000   -0.8508    0.0000  
  0.2500   -0.7933    0.4579  
  0.5000   -0.6242    0.8873  
  0.7500   -0.3542    1.2616  
  1.0000    0.0000    1.5574  

function d02jb_example
% Set up initial values.
n = 2;
k1max = 8;
kpmax = 15;
lw = 2*(kpmax + n)*(k1max + 1) + 7*k1max;
liw = n * k1max + int32(2);
x0 = -1.0;
x1 = 1.0;
c = zeros(k1max, n);
w = zeros(1, lw);
y = zeros(1, n);
iw = zeros(1, liw);

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

fprintf('  KP   K1   Chebyshev coefficients\n');
for kp = 10:5:kpmax
    for k1 = 4:2:k1max
        [c, ifail] = d02jb(int64(n), @cf, @bc, x0, x1, ...
            int64(k1), int64(kp));
        if (ifail ~= 0)
            % Incorrect parameters.  Print message and exit.
            fprintf('Warning: d02jaf returned with ifail = %d \n\n',ifail);
            return
        else
            % Output results.
            fprintf('%4d ',kp, k1);
            for jord = 1:n
                for kind = 1:k1
                    fprintf('%7.4f ',c(kind,jord));
                    if mod(kind, 8) == 0 && kind ~= k1
                        fprintf('\n          ');
                    end
                end
                fprintf('\n          ');
            end
            fprintf('\n');
        end
    end
end
% Now prepare to evaluate and plot the last solution.
fprintf('\n');
k1 = 8;
m = 9;
ia1 = 1;
xarray = zeros(m, 1);
yarray = zeros(m, n+1);

fprintf(['Last computed solution evaluated at %1d equally spaced ', ...
    'points\n\n'], m);
fprintf('    X       Y(1)      Y(2)\n');
for i = 1:m
    xarray(i) = (x0*(m-i) + x1*(i-1))/(m-1);
    fprintf('%8.4f  ', xarray(i));

    for jord = 1:n
        % Calling e02ak to evaluate the polynomial from its Chebyshev
        % representation.
        [yarray(i, jord), ifail] = e02ak(int64(k1-1), x0, x1, ...
            c(:,jord), int64(ia1), xarray(i));
        fprintf('%8.4f  ',yarray(i, jord));
    end
    fprintf('\n');

    % Calculate the absolute error at this point.
    yarray(i,n+1) = abs(yarray(i,1) - 1.0 + cos(xarray(i))/cos(1.0));
end
% Plot results.
fig = figure('Number', 'off');
display_plot(xarray, yarray);

function [j, rhs] = bc(i)
% Define the boundary conditions.
rhs = 0;
if (i == 1)
    j = int64(1);
else
    j = int64(-1);
end
function result = cf(ii, jj, x)
% Define the differential equation to be solved.
if (jj == ii)
    result = 0;
elseif (ii == 1 && jj == 2)
    result = 1;
elseif (ii == 2 && jj == 1)
    result = -1;
elseif (ii == 1)
    result = 0;
else
    result = 1;
end
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 two of the curves, then add the other one.
[haxes, hline1, hline2] = plotyy(x, y(:,2), x, y(:,3));
hold(haxes(1), 'on');
hline3 = plot(x, y(:,1));
% Set the axis limits and the tick specifications to beautify the plot.
set(haxes(1), 'YLim', [-2 2]);
set(haxes(1), 'XMinorTick', 'on', 'YMinorTick', 'on');
set(haxes(1), 'YTick', [-2.0 -1.0 0.0 1.0 2.0]);
set(haxes(2), 'YLim', [0.0 2e-6]);
set(haxes(2), 'YMinorTick', 'on');
set(haxes(2), 'YTick', [0.0 5e-7 1e-6 1.5e-6 2e-6]);
for iaxis = 1:2
    % These properties must be the same for both sets of axes.
    set(haxes(iaxis), 'XLim', [-1 1]);
    set(haxes(iaxis), 'FontSize', 13);
end
set(gca, 'box', 'off'); % so ticks aren't shown on opposite axes.
% Add title.
title(['Two-point BVP for ODE System using Chebyshev series with ', ...
    'Collocation & Least-Squares'], titleFmt{:});
% Label the axes.
xlabel('x', labFmt{:});
ylabel(haxes(1), 'Solution', labFmt{:});
ylabel(haxes(2), 'Absolute Error', labFmt{:});
% Add a legend.
legend('y''','y','y error','Location','Best');
% Set some features of the three lines.
set(hline1, 'Marker', '+','Linestyle','--');
set(hline2, 'Marker', '*','Linestyle',':');
set(hline3, 'Marker', 'x','Linestyle','-');
 
d02jb example program results

  KP   K1   Chebyshev coefficients
  10    4 -0.7798  0.0000  0.3899 -0.0000 
          -0.0000  1.5751  0.0000 -0.0629 
          
  10    6 -0.8326 -0.0000  0.4253  0.0000 -0.0090 -0.0000 
          -0.0000  1.6290  0.0000 -0.0724 -0.0000  0.0009 
          
  10    8 -0.8325 -0.0000  0.4253  0.0000 -0.0092 -0.0000  0.0001  0.0000 
          -0.0000  1.6289  0.0000 -0.0724 -0.0000  0.0009  0.0000 -0.0000 
          
  15    4 -0.7829  0.0000  0.3914 -0.0000 
           0.0000  1.5778  0.0000 -0.0631 
          
  15    6 -0.8326 -0.0000  0.4253  0.0000 -0.0090  0.0000 
           0.0000  1.6290  0.0000 -0.0724 -0.0000  0.0009 
          
  15    8 -0.8325 -0.0000  0.4253  0.0000 -0.0092  0.0000  0.0001 -0.0000 
           0.0000  1.6289  0.0000 -0.0724 -0.0000  0.0009  0.0000 -0.0000 
          

Last computed solution evaluated at 9 equally spaced points

    X       Y(1)      Y(2)
 -1.0000    0.0000   -1.5574  
 -0.7500   -0.3542   -1.2616  
 -0.5000   -0.6242   -0.8873  
 -0.2500   -0.7933   -0.4579  
  0.0000   -0.8508    0.0000  
  0.2500   -0.7933    0.4579  
  0.5000   -0.6242    0.8873  
  0.7500   -0.3542    1.2616  
  1.0000    0.0000    1.5574  


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