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_nth_comp (d02tg)

## Purpose

nag_ode_bvp_coll_nth_comp (d02tg) solves a system of linear ordinary differential equations by least squares fitting of a series of Chebyshev polynomials using collocation.

## Syntax

[c, ifail] = d02tg(m, l, x0, x1, k1, kp, coeff, bdyc, 'n', n)
[c, ifail] = nag_ode_bvp_coll_nth_comp(m, l, x0, x1, k1, kp, coeff, bdyc, 'n', n)

## Description

nag_ode_bvp_coll_nth_comp (d02tg) calculates an approximate solution of a linear or linearized system of ordinary differential equations as a Chebyshev series. Suppose there are n$n$ differential equations for n$n$ variables y1,y2,,yn${y}_{1},{y}_{2},\dots ,{y}_{n}$, over the range (x0,x1)$\left({x}_{0},{x}_{1}\right)$. Let the i$i$th equation be
 mi + 1 n ∑ ∑ fkji(x)yk(j − 1)(x) = ri(x) j = 1 k = 1
$∑ j=1 mi+1 ∑ k=1 n fkji(x) y k(j-1) (x)= ri(x)$
where yk(j)(x) = (djyk(x))/(dxj) ${y}_{k}^{\left(j\right)}\left(x\right)=\frac{{d}^{j}{y}_{k}\left(x\right)}{d{x}^{j}}$. coeff evaluates the coefficients fkji(x)${f}_{kj}^{i}\left(x\right)$ and the right-hand side ri(x)${r}^{i}\left(x\right)$ for each i$i$, 1in$1\le i\le n$, at any point x$x$. The boundary conditions may be applied either at the end points or at intermediate points; they are written in the same form as the differential equations, and specified by bdyc. For example the j$j$th boundary condition out of those associated with the i$i$th differential equation takes the form
 li + 1 n ∑ ∑ fkjij(xij)yk(j − 1)(xij) = rij(xij), j = 1 k = 1
$∑ j=1 li+1 ∑ k=1 n f kj ij (xij) yk(j-1) (xij)=rij (xij) ,$
where xij${x}^{ij}$ lies between x0${x}_{0}$ and x1${x}_{1}$. It is assumed in this function that certain of the boundary conditions are associated with each differential equation. This is for your convenience; the grouping does not affect the results.
The degree of the polynomial solution must be the same for all variables. You specify the degree required, k11${k}_{1}-1$, and the number of collocation points, kp${k}_{p}$, in the range. The function sets up a system of linear equations for the Chebyshev coefficients, with n$n$ equations for each collocation point and one for each boundary condition. The collocation points are chosen at the extrema of a shifted Chebyshev polynomial of degree kp1${k}_{p}-1$. The boundary conditions are satisfied exactly, and the remaining equations are solved by a least squares method. The result produced is a set of Chebyshev coefficients for the n$n$ functions y1,y2,,yn${y}_{1},{y}_{2},\dots ,{y}_{n}$, with the range normalized to [1,1]$\left[-1,1\right]$.
nag_fit_1dcheb_eval2 (e02ak) can be used to evaluate the components of the solution at any point on the range [x0,x1]$\left[{x}_{0},{x}_{1}\right]$ (see Section [Example] for an example). nag_fit_1dcheb_deriv (e02ah) and nag_fit_1dcheb_integ (e02aj) may be used to obtain Chebyshev series representations of derivatives and integrals (respectively) of the components of the solution.

## 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:     m(n) – int64int32nag_int array
n, the dimension of the array, must satisfy the constraint n1${\mathbf{n}}\ge 1$.
m(i)${\mathbf{m}}\left(\mathit{i}\right)$ must be set to the highest order derivative occurring in the i$\mathit{i}$th equation, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
Constraint: m(i)1${\mathbf{m}}\left(\mathit{i}\right)\ge 1$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
2:     l(n) – int64int32nag_int array
n, the dimension of the array, must satisfy the constraint n1${\mathbf{n}}\ge 1$.
l(i)${\mathbf{l}}\left(\mathit{i}\right)$ must be set to the number of boundary conditions associated with the i$\mathit{i}$th equation, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
Constraint: l(i)0${\mathbf{l}}\left(\mathit{i}\right)\ge 0$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
3:     x0 – double scalar
The left-hand boundary, x0${x}_{0}$.
4:     x1 – double scalar
The right-hand boundary, x1${x}_{1}$.
Constraint: x1 > x0${\mathbf{x1}}>{\mathbf{x0}}$.
5:     k1 – int64int32nag_int scalar
The number of coefficients, k1${k}_{1}$, to be returned in the Chebyshev series representation of the solution (hence, the degree of the polynomial approximation is k11${\mathbf{k1}}-1$).
Constraint: k11 + max1in  m(i)${\mathbf{k1}}\ge 1+\underset{1\le i\le {\mathbf{n}}}{\mathrm{max}}\phantom{\rule{0.25em}{0ex}}{\mathbf{m}}\left(i\right)$.
6:     kp – int64int32nag_int scalar
The number of collocation points to be used, kp${k}_{p}$.
Constraint: n × kpn × k1 + i = 1n l(i) ${\mathbf{n}}×{\mathbf{kp}}\ge {\mathbf{n}}×{\mathbf{k1}}+\sum _{i=1}^{{\mathbf{n}}}{\mathbf{l}}\left(i\right)$.
7:     coeff – function handle or string containing name of m-file
coeff defines the system of differential equations (see Section [Description]). It must evaluate the coefficient functions fkji(x)${f}_{kj}^{i}\left(x\right)$ and the right-hand side function ri(x)${r}^{i}\left(x\right)$ of the i$i$th equation at a given point. Only nonzero entries of the array a and rhs need be specifically assigned, since all elements are set to zero by nag_ode_bvp_coll_nth_comp (d02tg) before calling coeff.
[a, rhs] = coeff(x, ii, a, ia, ia1, rhs)

Input Parameters

1:     x – double scalar
x$x$, the point at which the functions must be evaluated.
2:     ii – int64int32nag_int scalar
The equation for which the coefficients and right-hand side are to be evaluated.
3:     a(ia,ia1) – double array
All elements of a are set to zero.
4:     ia – int64int32nag_int scalar
5:     ia1 – int64int32nag_int scalar
The first dimension of the array a and the second dimension of the array a as declared in the (sub)program from which nag_ode_bvp_coll_nth_comp (d02tg) is called.
6:     rhs – double scalar
Is set to zero.

Output Parameters

1:     a(ia,ia1) – double array
a(k,j)${\mathbf{a}}\left(k,j\right)$ must contain the value fkji(x)${f}_{kj}^{i}\left(x\right)$, for 1kn$1\le k\le n$, 1jmi + 1$1\le j\le {m}_{i}+1$.
2:     rhs – double scalar
It must contain the value ri(x)${r}^{i}\left(x\right)$.
8:     bdyc – function handle or string containing name of m-file
bdyc defines the boundary conditions (see Section [Description]). It must evaluate the coefficient functions fkjij${f}_{kj}^{ij}$ and right-hand side function rij${r}^{ij}$ in the j$j$th boundary condition associated with the i$i$th equation, at the point xij${x}^{ij}$ at which the boundary condition is applied. Only nonzero entries of the array a and rhs need be specifically assigned, since all elements are set to zero by nag_ode_bvp_coll_nth_comp (d02tg) before calling bdyc.
[x, a, rhs] = bdyc(ii, j, a, ia, ia1, rhs)

Input Parameters

1:     ii – int64int32nag_int scalar
The differential equation with which the condition is associated.
2:     j – int64int32nag_int scalar
The boundary condition for which the coefficients and right-hand side are to be evaluated.
3:     a(ia,ia1) – double array
All elements of a are set to zero.
4:     ia – int64int32nag_int scalar
5:     ia1 – int64int32nag_int scalar
The first dimension of the array a and the second dimension of the array a as declared in the (sub)program from which nag_ode_bvp_coll_nth_comp (d02tg) is called.
6:     rhs – double scalar
Is set to zero.

Output Parameters

1:     x – double scalar
xij${x}^{ij}$, the value at which the boundary condition is applied.
2:     a(ia,ia1) – double array
The value fkjij (xij) ${f}_{kj}^{ij}\left({x}^{ij}\right)$, for 1kn$1\le k\le n$, 1jmi + 1$1\le j\le {m}_{i}+1$.
3:     rhs – double scalar
The value rij (xij) ${r}^{ij}\left({x}^{ij}\right)$.

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the arrays m, l. (An error is raised if these dimensions are not equal.)
n$n$, the number of differential equations in the system.
Constraint: n1${\mathbf{n}}\ge 1$.

ldc w lw iw liw

### Output Parameters

1:     c(ldc,n) – double array
ldck1$\mathit{ldc}\ge {\mathbf{k1}}$.
The k$k$th column of c contains the computed Chebyshev coefficients of the k$k$th component of the solution, yk${y}_{k}$; that is, the computed solution is:
 k1 yk = ∑′ c(i,k)Ti − 1(x),  1 ≤ k ≤ n, i = 1
$yk=∑′i=1k1cikTi-1(x), 1≤k≤n,$
where Ti(x)${T}_{i}\left(x\right)$ is the Chebyshev polynomial of the first kind and ${\sum }^{\prime }$ denotes that the first coefficient, c(1,k)${\mathbf{c}}\left(1,k\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 m(i) < 1${\mathbf{m}}\left(i\right)<1$ for some i$i$, or l(i) < 0${\mathbf{l}}\left(i\right)<0$ for some i$i$, or x0 ≥ x1${\mathbf{x0}}\ge {\mathbf{x1}}$, or k1 < 1 + m(i)${\mathbf{k1}}<1+{\mathbf{m}}\left(i\right)$ for some i$i$, or n × kp < n × k1 + ∑ i = 1nl(i) ${\mathbf{n}}×{\mathbf{kp}}<{\mathbf{n}}×{\mathbf{k1}}+\sum _{i=1}^{n}{\mathbf{l}}\left(i\right)$, or ldc < k1$\mathit{ldc}<{\mathbf{k1}}$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, lw is too small (see Section [Parameters]), or liw < n × k1$\mathit{liw}<{\mathbf{n}}×{\mathbf{k1}}$.
ifail = 3${\mathbf{ifail}}=3$
Either the boundary conditions are not linearly independent, 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)). Increasing kp may remove this difficulty.

## Accuracy

Estimates of the accuracy of the solution may be obtained by using the checks described in Section [Further Comments]. The Chebyshev coefficients are calculated by a stable numerical method.

## Further Comments

The time taken by nag_ode_bvp_coll_nth_comp (d02tg) depends on the complexity of the system of differential equations, the degree of the polynomial solution and the number of matching points.
If the number of matching points kp${k}_{p}$ is equal to the number of coefficients k1${k}_{1}$ minus the average number of boundary conditions 1/n i = 1n li $\frac{1}{n}\sum _{i=1}^{n}{l}_{i}$, then the least squares solution reduces to simple solution of linear equations and true collocation results. The accuracy of the solution may be checked by repeating the calculation with different values of k1${k}_{1}$. If the Chebyshev coefficients decrease rapidly, the size of the last two or three gives an indication of the error. If they do not decrease rapidly, it may be desirable to use a different method. Note that the Chebyshev coefficients are calculated for the range normalized to [1,1]$\left[-1,1\right]$.
Generally the number of boundary conditions required is equal to the sum of the orders of the n$n$ differential equations. However, in some cases fewer boundary conditions are needed, because the assumption of a polynomial solution is equivalent to one or more boundary conditions (since it excludes singular solutions).
A system of nonlinear differential equations must be linearized before using the function. The calculation is repeated iteratively. On each iteration the linearized equation is used. In the example in Section [Example], the y$y$ variables are to be determined at the current iteration whilst the z$z$ variables correspond to the solution determined at the previous iteration, (or the initial approximation on the first iteration). For a starting approximation, we may take, say, a linear function, and set up the appropriate Chebyshev coefficients before starting the iteration. For example, if y1 = ax + b${y}_{1}=ax+b$ in the range (x0,x1)$\left({x}_{0},{x}_{1}\right)$, we set B$\mathrm{B}$, the array of coefficients,
• B(1,1) = a × (x0 + x1) + 2 × b$\mathrm{B}\left(1,1\right)=a×\left({x}_{0}+{x}_{1}\right)+2×b$,
• B(1,2) = a × (x1x0) / 2$\mathrm{B}\left(1,2\right)=a×\left({x}_{1}-{x}_{0}\right)/2$,
• and the remainder of the entries to zero.
In some cases a better initial approximation may be needed and can be obtained by using nag_fit_1dcheb_arb (e02ad) or nag_fit_1dcheb_glp (e02af) to obtain a Chebyshev series for an approximate solution. The coefficients of the current iterate must be communicated to coeff and bdyc, e.g., via a global variable. (See Section [Example].) The convergence of the (Newton) iteration cannot be guaranteed in general, though it is usually satisfactory from a good starting approximation.

## Example

This example solves the nonlinear system
 2y1 ′ + (y22 − 1) y1 + y2 = 0, 2y2 ′ ′ − y1 ′ = 0,
$2y1′+(y22-1) y1+y2=0, 2y2′′-y1′=0,$
in the range (1,1)$\left(-1,1\right)$, with y1 = 0${y}_{1}=0$, y2 = 3${y}_{2}=3$, y2 = 0${y}_{2}^{\prime }=0$ at x = 1$x=-1$.
Suppose an approximate solution is z1${z}_{1}$, z2${z}_{2}$ such that y1z1${y}_{1}\sim {z}_{1}$, y2z2${y}_{2}\sim {z}_{2}$: then the first equation gives, on linearizing,
 2y1 ′ + (z22 − 1) y1 + (2z1z2 + 1) y2 = 2z1z22. $2y1′+(z22-1) y1+(2z1z2+1) y2=2z1z22.$
The starting approximation is taken to be z1 = 0${z}_{1}=0$, z2 = 3${z}_{2}=3$. In the program below, the array B$\mathrm{B}$ is used to hold the coefficients of the previous iterate (or of the starting approximation). We iterate until the Chebyshev coefficients converge to five figures. nag_fit_1dcheb_eval2 (e02ak) is used to calculate the solution from its Chebyshev coefficients.
```function nag_ode_bvp_coll_nth_comp_example
global b x0 x1 ndegree % For communication with coeff.

% Initialize variables and arrays.
x0 = -1;
x1 = 1;
ncolloc = 15;
neqn = 2;
ncoeff = 9;

nderiv = [1; 2];
nbc = [1; 2];

% Set initial values for Chebyshev series coefficients.
b = zeros(ncoeff, neqn);
b(1,2) = 6.0;

% The degree of the Chebyshev polynomial (as used in nag_fit_1dcheb_eval2) is one less
% than the number of coefficients (as used in nag_ode_bvp_coll_nth_comp).
ndegree = ncoeff-1;

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

% Initial iteration.
iter = 1;
[c, ifail] = nag_ode_bvp_coll_nth_comp(int64(nderiv), int64(nbc), x0, x1, int64(ncoeff), ...
int64(ncolloc), @coeff, @bdyc, 'n', int64(neqn));
if ifail ~= 0
% Unsuccessful call.  Print message and exit.
error('Warning: nag_ode_bvp_coll_nth_comp returned with ifail = %1d ',ifail);
end

% Output new estimate for coefficients.
fprintf(['\n Iteration ', num2str(iter), ...
'. Chebyshev coefficients are:\n']);
for j = 1:neqn
for i = 1:ncoeff
fprintf('%8.4f',c(i,j));
end
fprintf('\n');
end

% Calculate change in coefficients.
emax = 0.0;
for j = 1:neqn; i = 1:ncoeff;
emax = max(max(emax,abs(c(i,j) - b(i,j))));
b(i,j) = c(i,j);
end

% Iterate until the change is small enough.
while emax >= 1.0e-05
iter = iter + 1;

[c, ifail] = nag_ode_bvp_coll_nth_comp(int64(nderiv), int64(nbc), x0, x1, ...
int64(ncoeff), int64(ncolloc), @coeff, @bdyc, ...
'n', int64(neqn));
if ifail ~= 0
% Unsuccessful call.  Print message and exit.
error('Warning: nag_ode_bvp_coll_nth_comp returned with ifail = %1d ',ifail);
end

% Output coefficients.
fprintf(['\n Iteration ', num2str(iter), ...
'. Chebyshev coefficients are:\n']);
for j = 1:neqn
for i = 1:ncoeff
fprintf('%8.4f',c(i,j));
end
fprintf('\n');
end

emax = 0.0;
for j = 1:neqn; i = 1:ncoeff;
emax = max(max(emax,abs(c(i,j) - b(i,j))));
b(i,j) = c(i,j);
end
end

% Use these coefficients to evaluate the solution.
npt = 9;
fprintf(['\n Solution evaluated at %d equally spaced points.\n\n', ...
'      X'], npt);
for jeqn = 1:neqn
fprintf('       Y(%1d)', jeqn);
end
fprintf('\n');

% Prepare to store results for plotting.
xarray = zeros(npt, 1);
yarray = zeros(npt, neqn);

for ipt = 1:npt
xarray(ipt) = (x0*(npt-ipt) + x1*(ipt-1))/(npt-1);
fprintf('%10.4f', xarray(ipt));
for jeqn = 1:neqn
[yarray(ipt, jeqn), ifail] = nag_fit_1dcheb_eval2(int64(ndegree), x0, x1, ...
c(:,jeqn), int64(1), xarray(ipt));
fprintf('%10.4f', yarray(ipt, jeqn));
end
fprintf('\n');
end
% Plot results.
fig = figure('Number', 'off');
display_plot(xarray, yarray);

function [x, a, rhs] = bdyc(ii, jj, a, ia, ia1, rhs)
% Evaluate coefficient functions and rhs of boundary conditions.

x = -1.0;
a(ii,jj) = 1.0;
if (ii == 2 && jj == 1)
rhs = 3.0;
end
function [a, rhs] = coeff(x, ii, a, ia, ia1, rhs)
% Evaluate coefficient functions and rhs of differential equations.

global b x0 x1 ndegree % For communication with main routine.

if (ii <= 1)
[z1, ifail] = nag_fit_1dcheb_eval2(int64(ndegree), x0, x1, b(:,1), int64(1), x);
[z2, ifail] = nag_fit_1dcheb_eval2(int64(ndegree), x0, x1, b(:,2), int64(1), x);
a(1,1) = z2*z2 - 1.0;
a(1,2) = 2.0;
a(2,1) = 2.0*z1*z2 + 1.0;
rhs = 2.0*z1*z2*z2;
else
a(1,2) = -1.0;
a(2,3) =  2.0;
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 both curves.
[haxes, hline1, hline2] = plotyy(x, y(:,1), x, y(:,2));
% Set the axis limits and the tick specifications to beautify the plot.
set(haxes(1), 'YLim', [-0.5 0.05]);
set(haxes(1), 'YMinorTick', 'on');
set(haxes(1), 'YTick', [-0.5 -0.4 -0.3 -0.2 -0.1 0.0]);
set(haxes(2), 'YLim', [2.5 3.05]);
set(haxes(2), 'YMinorTick', 'on');
set(haxes(2), 'YTick', [2.5 2.6 2.7 2.8 2.9 3.0]);
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
% Add title.
title('Solution of Linearised System by Chebyshev Collocation', ...
titleFmt{:});
% Label the axes.
xlabel('x', labFmt{:});
ylabel(haxes(1), 'y1', labFmt{:});
ylabel(haxes(2), 'y2', labFmt{:});
% Add a legend.
legend('y1','y2','Location','Best')
% Set some features of the three lines.
set(hline1, 'Linewidth', 0.25, 'Marker', '+', 'Line', '-');
set(hline2, 'Linewidth', 0.25, 'Marker', 'x', 'Line', '--');
```
```
nag_ode_bvp_coll_nth_comp example program results

Iteration 1. Chebyshev coefficients are:
-0.5659 -0.1162  0.0906 -0.0468  0.0196 -0.0069  0.0021 -0.0006  0.0001
5.7083 -0.1642 -0.0087  0.0059 -0.0025  0.0009 -0.0003  0.0001 -0.0000

Iteration 2. Chebyshev coefficients are:
-0.6338 -0.1599  0.0831 -0.0445  0.0193 -0.0071  0.0023 -0.0006  0.0001
5.6881 -0.1792 -0.0144  0.0053 -0.0023  0.0008 -0.0003  0.0001 -0.0000

Iteration 3. Chebyshev coefficients are:
-0.6344 -0.1604  0.0828 -0.0446  0.0193 -0.0071  0.0023 -0.0006  0.0001
5.6880 -0.1793 -0.0145  0.0053 -0.0023  0.0008 -0.0003  0.0001 -0.0000

Iteration 4. Chebyshev coefficients are:
-0.6344 -0.1604  0.0828 -0.0446  0.0193 -0.0071  0.0023 -0.0006  0.0001
5.6880 -0.1793 -0.0145  0.0053 -0.0023  0.0008 -0.0003  0.0001 -0.0000

Solution evaluated at 9 equally spaced points.

X       Y(1)       Y(2)
-1.0000    0.0000    3.0000
-0.7500   -0.2372    2.9827
-0.5000   -0.3266    2.9466
-0.2500   -0.3640    2.9032
0.0000   -0.3828    2.8564
0.2500   -0.3951    2.8077
0.5000   -0.4055    2.7577
0.7500   -0.4154    2.7064
1.0000   -0.4255    2.6538

```
```function d02tg_example
global b x0 x1 ndegree % For communication with coeff.

% Initialize variables and arrays.
x0 = -1;
x1 = 1;
ncolloc = 15;
neqn = 2;
ncoeff = 9;

nderiv = [1; 2];
nbc = [1; 2];

% Set initial values for Chebyshev series coefficients.
b = zeros(ncoeff, neqn);
b(1,2) = 6.0;

% The degree of the Chebyshev polynomial (as used in e02ak) is one less
% than the number of coefficients (as used in d02tg).
ndegree = ncoeff-1;

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

% Initial iteration.
iter = 1;
[c, ifail] = d02tg(int64(nderiv), int64(nbc), x0, x1, int64(ncoeff), ...
int64(ncolloc), @coeff, @bdyc, 'n', int64(neqn));
if ifail ~= 0
% Unsuccessful call.  Print message and exit.
error('Warning: d02tg returned with ifail = %1d ',ifail);
end

% Output new estimate for coefficients.
fprintf(['\n Iteration ', num2str(iter), ...
'. Chebyshev coefficients are:\n']);
for j = 1:neqn
for i = 1:ncoeff
fprintf('%8.4f',c(i,j));
end
fprintf('\n');
end

% Calculate change in coefficients.
emax = 0.0;
for j = 1:neqn; i = 1:ncoeff;
emax = max(max(emax,abs(c(i,j) - b(i,j))));
b(i,j) = c(i,j);
end

% Iterate until the change is small enough.
while emax >= 1.0e-05
iter = iter + 1;

[c, ifail] = d02tg(int64(nderiv), int64(nbc), x0, x1, ...
int64(ncoeff), int64(ncolloc), @coeff, @bdyc, ...
'n', int64(neqn));
if ifail ~= 0
% Unsuccessful call.  Print message and exit.
error('Warning: d02tg returned with ifail = %1d ',ifail);
end

% Output coefficients.
fprintf(['\n Iteration ', num2str(iter), ...
'. Chebyshev coefficients are:\n']);
for j = 1:neqn
for i = 1:ncoeff
fprintf('%8.4f',c(i,j));
end
fprintf('\n');
end

emax = 0.0;
for j = 1:neqn; i = 1:ncoeff;
emax = max(max(emax,abs(c(i,j) - b(i,j))));
b(i,j) = c(i,j);
end
end

% Use these coefficients to evaluate the solution.
npt = 9;
fprintf(['\n Solution evaluated at %d equally spaced points.\n\n', ...
'      X'], npt);
for jeqn = 1:neqn
fprintf('       Y(%1d)', jeqn);
end
fprintf('\n');

% Prepare to store results for plotting.
xarray = zeros(npt, 1);
yarray = zeros(npt, neqn);

for ipt = 1:npt
xarray(ipt) = (x0*(npt-ipt) + x1*(ipt-1))/(npt-1);
fprintf('%10.4f', xarray(ipt));
for jeqn = 1:neqn
[yarray(ipt, jeqn), ifail] = e02ak(int64(ndegree), x0, x1, ...
c(:,jeqn), int64(1), xarray(ipt));
fprintf('%10.4f', yarray(ipt, jeqn));
end
fprintf('\n');
end
% Plot results.
fig = figure('Number', 'off');
display_plot(xarray, yarray);

function [x, a, rhs] = bdyc(ii, jj, a, ia, ia1, rhs)
% Evaluate coefficient functions and rhs of boundary conditions.

x = -1.0;
a(ii,jj) = 1.0;
if (ii == 2 && jj == 1)
rhs = 3.0;
end
function [a, rhs] = coeff(x, ii, a, ia, ia1, rhs)
% Evaluate coefficient functions and rhs of differential equations.

global b x0 x1 ndegree % For communication with main routine.

if (ii <= 1)
[z1, ifail] = e02ak(int64(ndegree), x0, x1, b(:,1), int64(1), x);
[z2, ifail] = e02ak(int64(ndegree), x0, x1, b(:,2), int64(1), x);
a(1,1) = z2*z2 - 1.0;
a(1,2) = 2.0;
a(2,1) = 2.0*z1*z2 + 1.0;
rhs = 2.0*z1*z2*z2;
else
a(1,2) = -1.0;
a(2,3) =  2.0;
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 both curves.
[haxes, hline1, hline2] = plotyy(x, y(:,1), x, y(:,2));
% Set the axis limits and the tick specifications to beautify the plot.
set(haxes(1), 'YLim', [-0.5 0.05]);
set(haxes(1), 'YMinorTick', 'on');
set(haxes(1), 'YTick', [-0.5 -0.4 -0.3 -0.2 -0.1 0.0]);
set(haxes(2), 'YLim', [2.5 3.05]);
set(haxes(2), 'YMinorTick', 'on');
set(haxes(2), 'YTick', [2.5 2.6 2.7 2.8 2.9 3.0]);
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
% Add title.
title('Solution of Linearised System by Chebyshev Collocation', ...
titleFmt{:});
% Label the axes.
xlabel('x', labFmt{:});
ylabel(haxes(1), 'y1', labFmt{:});
ylabel(haxes(2), 'y2', labFmt{:});
% Add a legend.
legend('y1','y2','Location','Best')
% Set some features of the three lines.
set(hline1, 'Linewidth', 0.25, 'Marker', '+', 'Line', '-');
set(hline2, 'Linewidth', 0.25, 'Marker', 'x', 'Line', '--');
```
```
d02tg example program results

Iteration 1. Chebyshev coefficients are:
-0.5659 -0.1162  0.0906 -0.0468  0.0196 -0.0069  0.0021 -0.0006  0.0001
5.7083 -0.1642 -0.0087  0.0059 -0.0025  0.0009 -0.0003  0.0001 -0.0000

Iteration 2. Chebyshev coefficients are:
-0.6338 -0.1599  0.0831 -0.0445  0.0193 -0.0071  0.0023 -0.0006  0.0001
5.6881 -0.1792 -0.0144  0.0053 -0.0023  0.0008 -0.0003  0.0001 -0.0000

Iteration 3. Chebyshev coefficients are:
-0.6344 -0.1604  0.0828 -0.0446  0.0193 -0.0071  0.0023 -0.0006  0.0001
5.6880 -0.1793 -0.0145  0.0053 -0.0023  0.0008 -0.0003  0.0001 -0.0000

Iteration 4. Chebyshev coefficients are:
-0.6344 -0.1604  0.0828 -0.0446  0.0193 -0.0071  0.0023 -0.0006  0.0001
5.6880 -0.1793 -0.0145  0.0053 -0.0023  0.0008 -0.0003  0.0001 -0.0000

Solution evaluated at 9 equally spaced points.

X       Y(1)       Y(2)
-1.0000    0.0000    3.0000
-0.7500   -0.2372    2.9827
-0.5000   -0.3266    2.9466
-0.2500   -0.3640    2.9032
0.0000   -0.3828    2.8564
0.2500   -0.3951    2.8077
0.5000   -0.4055    2.7577
0.7500   -0.4154    2.7064
1.0000   -0.4255    2.6538

```

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