Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_ode_bvp_coll_nth (d02ja)

## Purpose

nag_ode_bvp_coll_nth (d02ja) solves a regular linear two-point boundary value problem for a single n$n$th-order ordinary differential equation by Chebyshev series using collocation and least squares.

## Syntax

[c, ifail] = d02ja(n, cf, bc, x0, x1, k1, kp)
[c, ifail] = nag_ode_bvp_coll_nth(n, cf, bc, x0, x1, k1, kp)

## Description

nag_ode_bvp_coll_nth (d02ja) calculates the solution of a regular two-point boundary value problem for a single n$n$th-order linear ordinary differential equation as a Chebyshev series in the interval (x0,x1)$\left({x}_{0},{x}_{1}\right)$. The differential equation
 fn + 1(x)y(n)(x) + fn(x)y(n − 1)(x) + ⋯ + f1(x)y(x) = f0(x) $fn+1(x)y (n) (x)+fn(x)y (n-1) (x)+⋯+f1(x)y(x)=f0(x)$
is defined by cf, and the boundary conditions at the points x0${x}_{0}$ and x1${x}_{1}$ are defined by bc.
You specify the degree of Chebyshev series required, k11${\mathbf{k1}}-1$, and the number of collocation points, kp. The function sets up a system of linear equations for the Chebyshev coefficients, one equation 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 of the differential equation on an interval normalized to (1,1)$\left(-1,1\right)$.
nag_fit_1dcheb_eval2 (e02ak) can be used to evaluate the solution at any point on the interval (x0,x1)$\left({x}_{0},{x}_{1}\right)$ – see Section [Example] for an example. nag_fit_1dcheb_deriv (e02ah) followed by nag_fit_1dcheb_eval2 (e02ak) can be used to evaluate its 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
n$n$, the order of the differential equation.
Constraint: n1${\mathbf{n}}\ge 1$.
2:     cf – function handle or string containing name of m-file
cf defines the differential equation (see Section [Description]). It must return the value of a function fj(x)${f}_{j}\left(x\right)$ at a given point x$x$, where, for 1jn + 1$1\le j\le n+1$, fj(x)${f}_{j}\left(x\right)$ is the coefficient of y(j1)(x)${y}^{\left(j-1\right)}\left(x\right)$ in the equation, and f0(x)${f}_{0}\left(x\right)$ is the right-hand side.
[result] = cf(j, x)

Input Parameters

1:     j – int64int32nag_int scalar
The index of the function fj${f}_{j}$ to be evaluated.
2:     x – double scalar
The point at which fj${f}_{j}$ 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 boundary conditions, each of which has the form y(k1)(x1) = sk${y}^{\left(k-1\right)}\left({x}_{1}\right)={s}_{k}$ or y(k1)(x0) = sk${y}^{\left(k-1\right)}\left({x}_{0}\right)={s}_{k}$. 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 boundary condition is y(k1)(x0) = sk${y}^{\left(k-1\right)}\left({x}_{0}\right)={s}_{k}$, and to + k$+k$ if it is y(k1)(x1) = sk${y}^{\left(k-1\right)}\left({x}_{1}\right)={s}_{k}$.
j must not be set to the same value k$k$ for two different values of ii.
2:     rhs – double scalar
Must be set to the value sk${s}_{k}$.
4:     x0 – double scalar
5:     x1 – double scalar
The left- and right-hand boundaries, x0${x}_{0}$ and x1${x}_{1}$, respectively.
Constraint: x1 > x0${\mathbf{x1}}>{\mathbf{x0}}$.
6:     k1 – int64int32nag_int scalar
The number of coefficients to be returned in the Chebyshev series representation of the solution (hence the degree of the polynomial approximation is k11${\mathbf{k1}}-1$).
Constraint: k1n + 1${\mathbf{k1}}\ge {\mathbf{n}}+1$.
7:     kp – int64int32nag_int scalar
The number of collocation points to be used.
Constraint: kpk1n${\mathbf{kp}}\ge {\mathbf{k1}}-{\mathbf{n}}$.

None.

w lw iw

### Output Parameters

1:     c(k1) – double array
The computed Chebyshev coefficients; that is, the computed solution is:
 k1 ∑′ c(i)Ti − 1(x) i = 1
$∑′i=1k1ciTi-1(x)$
where Ti(x)${T}_{i}\left(x\right)$ is the i$i$th Chebyshev polynomial of the first kind, and ${\sum }^{\prime }$ denotes that the first coefficient, c(1)${\mathbf{c}}\left(1\right)$, is halved.
2:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
 On entry, n < 1${\mathbf{n}}<1$, or x0 ≥ x1${\mathbf{x0}}\ge {\mathbf{x1}}$, or k1 < n + 1${\mathbf{k1}}<{\mathbf{n}}+1$, or kp < k1 − n${\mathbf{kp}}<{\mathbf{k1}}-{\mathbf{n}}$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, lw < 2 × (kp + n) × (k1 + 1) + 7 × k1$\mathit{lw}<2×\left({\mathbf{kp}}+{\mathbf{n}}\right)×\left({\mathbf{k1}}+1\right)+7×{\mathbf{k1}}$ (insufficient workspace).
ifail = 3${\mathbf{ifail}}=3$
Either the boundary conditions are not linearly independent (that is, in bc the variable j is set to the same value k$k$ 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 = 4${\mathbf{ifail}}=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 polynomial and the number of collocation points (see Section [Further Comments]).

The time taken by nag_ode_bvp_coll_nth (d02ja) depends on the complexity of the differential equation, the degree of the polynomial solution, and the number of matching points.
The collocation points in the interval (x0,x1)$\left({x}_{0},{x}_{1}\right)$ are chosen to be the extrema of the appropriate shifted Chebyshev polynomial. If kp = k1n${\mathbf{kp}}={\mathbf{k1}}-{\mathbf{n}}$, 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 kpk1n${\mathbf{kp}}\gg {\mathbf{k1}}-{\mathbf{n}}$. If the Chebyshev coefficients decrease rapidly (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)$\left(-1,1\right)$.
Systems of regular linear differential equations can be solved using nag_ode_bvp_coll_sys (d02jb). It is necessary before using nag_ode_bvp_coll_sys (d02jb) to write the differential equations as a first-order system. 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 .$
We use k1 = 4${\mathbf{k1}}=4$, 6$6$ and 8$8$, and kp = 10${\mathbf{kp}}=10$ and 15$15$, so that the different Chebyshev series may be compared. The solution for k1 = 8${\mathbf{k1}}=8$ and kp = 15${\mathbf{kp}}=15$ is evaluated by nag_fit_1dcheb_eval2 (e02ak) at nine equally spaced points over the interval (1,1)$\left(-1,1\right)$.
```function nag_ode_bvp_coll_nth_example
% Set up initial values.
n = 2;
k1max = 8;
kpmax = 15;
lw = 2*(kpmax+n)*(k1max+1)+7*k1max;
x0 = -1;
x1 = 1;
k1 = 4;
kp = 10;
c = zeros(1,kp);
p = zeros(1,kp);

fprintf('nag_ode_bvp_coll_nth 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_nth(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 kind = 1:k1
fprintf('%8.4f  ',c(kind));
if mod(kind, 6) == 0 && kind ~= k1
fprintf('\n          ');
end
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(k1,1);
yarray = zeros(k1,1);

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

% Calling nag_fit_1dcheb_eval2 to evaluate the polynomial from its Chebyshev
% representation.
[y, ifail] = nag_fit_1dcheb_eval2(int64(k1-1), x0, x1, c, int64(ia1), x);

% Output results, and save them for plotting.
fprintf('%8.4f  %8.4f  \n',x,y);
xarray(i) = x;
yarray(i) = y;
end
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(j, x)
% Define the differential equation to be solved.
if (j == 2)
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 results.
plot(x, y, '-+');
title({'Two-point Boundary-value Problem for ODE',...
'by Chebyshev-series using Collocation and Least-squares'}, ...
titleFmt{:})
% Label the axes.
xlabel('x', labFmt{:});
ylabel('y', labFmt{:});
```
```
nag_ode_bvp_coll_nth example program results

KP   K1   Chebyshev coefficients
10    4  -0.6108   -0.0000    0.3054    0.0000
10    6  -0.8316   -0.0000    0.4246    0.0000   -0.0088   -0.0000
10    8  -0.8325   -0.0000    0.4253    0.0000   -0.0092    0.0000
0.0001   -0.0000
15    4  -0.6174   -0.0000    0.3087    0.0000
15    6  -0.8316   -0.0000    0.4246    0.0000   -0.0088   -0.0000
15    8  -0.8325   -0.0000    0.4253    0.0000   -0.0092   -0.0000
0.0001   -0.0000

Last computed solution evaluated at 9 equally spaced points

X         Y
-1.0000    0.0000
-0.7500   -0.3542
-0.5000   -0.6242
-0.2500   -0.7933
0.0000   -0.8508
0.2500   -0.7933
0.5000   -0.6242
0.7500   -0.3542
1.0000    0.0000

```
```function d02ja_example
% Set up initial values.
n = 2;
k1max = 8;
kpmax = 15;
lw = 2*(kpmax+n)*(k1max+1)+7*k1max;
x0 = -1;
x1 = 1;
k1 = 4;
kp = 10;
c = zeros(1,kp);
p = zeros(1,kp);

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

fprintf('  KP   K1   Chebyshev coefficients\n');
for kp = 10:5:kpmax
for k1 = 4:2:k1max

[c, ifail] = d02ja(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 kind = 1:k1
fprintf('%8.4f  ',c(kind));
if mod(kind, 6) == 0 && kind ~= k1
fprintf('\n          ');
end
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(k1,1);
yarray = zeros(k1,1);

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

% Calling e02ak to evaluate the polynomial from its Chebyshev
% representation.
[y, ifail] = e02ak(int64(k1-1), x0, x1, c, int64(ia1), x);

% Output results, and save them for plotting.
fprintf('%8.4f  %8.4f  \n',x,y);
xarray(i) = x;
yarray(i) = y;
end
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(j, x)
% Define the differential equation to be solved.
if (j == 2)
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 results.
plot(x, y, '-+');
title({'Two-point Boundary-value Problem for ODE',...
'by Chebyshev-series using Collocation and Least-squares'}, ...
titleFmt{:})
% Label the axes.
xlabel('x', labFmt{:});
ylabel('y', labFmt{:});
```
```
d02ja example program results

KP   K1   Chebyshev coefficients
10    4  -0.6108   -0.0000    0.3054    0.0000
10    6  -0.8316   -0.0000    0.4246    0.0000   -0.0088   -0.0000
10    8  -0.8325   -0.0000    0.4253    0.0000   -0.0092    0.0000
0.0001   -0.0000
15    4  -0.6174   -0.0000    0.3087    0.0000
15    6  -0.8316   -0.0000    0.4246    0.0000   -0.0088   -0.0000
15    8  -0.8325   -0.0000    0.4253    0.0000   -0.0092   -0.0000
0.0001   -0.0000

Last computed solution evaluated at 9 equally spaced points

X         Y
-1.0000    0.0000
-0.7500   -0.3542
-0.5000   -0.6242
-0.2500   -0.7933
0.0000   -0.8508
0.2500   -0.7933
0.5000   -0.6242
0.7500   -0.3542
1.0000    0.0000

```