PDF version (NAG web site
, 64-bit version, 64-bit version)
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
differential equations for
variables
, over the range
. Let the
th equation be
where
.
coeff evaluates the coefficients
and the right-hand side
for each
,
, at any point
. 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
th boundary condition out of those associated with the
th differential equation takes the form
where
lies between
and
. 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, , and the number of collocation points, , in the range. The function sets up a system of linear equations for the Chebyshev coefficients, with 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 . 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 functions , with the range normalized to .
nag_fit_1dcheb_eval2 (e02ak) can be used to evaluate the components of the solution at any point on the range
(see
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:
– int64int32nag_int array
-
must be set to the highest order derivative occurring in the th equation, for .
Constraint:
, for .
- 2:
– int64int32nag_int array
-
must be set to the number of boundary conditions associated with the th equation, for .
Constraint:
, for .
- 3:
– double scalar
-
The left-hand boundary, .
- 4:
– double scalar
-
The right-hand boundary, .
Constraint:
.
- 5:
– 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 ).
Constraint:
.
- 6:
– int64int32nag_int scalar
-
The number of collocation points to be used, .
Constraint:
.
- 7:
– function handle or string containing name of m-file
-
coeff defines the system of differential equations (see
Description). It must evaluate the coefficient functions
and the right-hand side function
of the
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:
– double scalar
-
, the point at which the functions must be evaluated.
- 2:
– int64int32nag_int scalar
-
The equation for which the coefficients and right-hand side are to be evaluated.
- 3:
– double array
-
All elements of
a are set to zero.
- 4:
– int64int32nag_int scalar
- 5:
– int64int32nag_int scalar
-
The first dimension of the array
a and the second dimension of the array
a.
- 6:
– double scalar
-
Is set to zero.
Output Parameters
- 1:
– double array
-
must contain the value , for , .
- 2:
– double scalar
-
It must contain the value .
- 8:
– function handle or string containing name of m-file
-
bdyc defines the boundary conditions (see
Description). It must evaluate the coefficient functions
and right-hand side function
in the
th boundary condition associated with the
th equation, at the point
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:
– int64int32nag_int scalar
-
The differential equation with which the condition is associated.
- 2:
– int64int32nag_int scalar
-
The boundary condition for which the coefficients and right-hand side are to be evaluated.
- 3:
– double array
-
All elements of
a are set to zero.
- 4:
– int64int32nag_int scalar
- 5:
– int64int32nag_int scalar
-
The first dimension of the array
a and the second dimension of the array
a.
- 6:
– double scalar
-
Is set to zero.
Output Parameters
- 1:
– double scalar
-
, the value at which the boundary condition is applied.
- 2:
– double array
-
The value , for , .
- 3:
– double scalar
-
The value .
Optional Input Parameters
- 1:
– int64int32nag_int scalar
-
Default:
the dimension of the arrays
m,
l. (An error is raised if these dimensions are not equal.)
, the number of differential equations in the system.
Constraint:
.
Output Parameters
- 1:
– double array
-
The
th column of
c contains the computed Chebyshev coefficients of the
th component of the solution,
; that is, the computed solution is:
where
is the Chebyshev polynomial of the first kind and
denotes that the first coefficient,
, is halved.
- 2:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Errors or warnings detected by the function:
-
-
On entry, | , |
or | for some , |
or | for some , |
or | , |
or | for some , |
or | , |
or | . |
-
-
On entry, | lw is too small (see Arguments), |
or | . |
-
-
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.
-
-
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.
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
Accuracy
Estimates of the accuracy of the solution may be obtained by using the checks described in
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 is equal to the number of coefficients minus the average number of boundary conditions
, 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 . 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 .
Generally the number of boundary conditions required is equal to the sum of the orders of the 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
Example, the
variables are to be determined at the current iteration whilst the
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
in the range
, we set
, the array of coefficients,
- ,
- ,
- 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
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
in the range
, with
,
,
at
.
Suppose an approximate solution is
,
such that
,
: then the first equation gives, on linearizing,
The starting approximation is taken to be
,
. In the program below, the array
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.
Open in the MATLAB editor:
d02tg_example
function d02tg_example
fprintf('d02tg example results\n\n');
global b x0 x1 ndegree
n = 2;
nderiv = int64([1 2]);
nbc = nderiv;
x0 = -1;
x1 = 1;
ncoeff = int64(9);
ncolloc = int64(15);
ndegree = ncoeff-1;
iter = 0;
emax = 1.0;
b = zeros(ncoeff, n);
b(1,2) = 6.0;
while emax >= 1.0e-05
iter = iter + 1;
[c, ifail] = d02tg(...
nderiv, nbc, x0, x1, ncoeff, ncolloc, ...
@coeff, @bdyc);
fprintf('\n Iteration %2d. Chebyshev coefficients are:\n',iter);
for j = 1:n
fprintf('%8.4f',c(1:ncoeff,j));
fprintf('\n');
end
emax = max(abs(c - b));
b = c;
end
npt = 9;
fprintf('\n Solution evaluated at %d equally spaced points.\n\n', npt);
fprintf('%7s%16s\n','x','y');
dx = (x1-x0)/(double(npt-1));
xarray = [x0:dx:x1];
yarray = zeros(npt, n);
for ipt = 1:npt
xx = xarray(ipt);
fprintf('%10.4f', xx);
for jeqn = 1:n
[yarray(ipt, jeqn), ifail] = ...
e02ak(...
ndegree, x0, x1, c(:,jeqn), int64(1), xx);
fprintf('%10.4f', yarray(ipt, jeqn));
end
fprintf('\n');
end
fig1 = figure;
display_plot(xarray, yarray);
function [x, a, rhs] = bdyc(ii, jj, a, ia, ia1, rhs)
x = -1;
a(ii,jj) = 1;
if (ii == 2 && jj == 1)
rhs = 3;
end
function [a, rhs] = coeff(x, ii, a, ia, ia1, rhs)
global b x0 x1 ndegree
if (ii <= 1)
[z1, ifail] = e02ak(ndegree, x0, x1, b(:,1), int64(1), x);
[z2, ifail] = e02ak(ndegree, x0, x1, b(:,2), int64(1), x);
a(1,1) = z2*z2 - 1;
a(1,2) = 2;
a(2,1) = 2*z1*z2 + 1;
rhs = 2*z1*z2*z2;
else
a(1,2) = -1;
a(2,3) = 2;
end
function display_plot(x, y)
[haxes, hline1, hline2] = plotyy(x, y(:,1), x, y(:,2));
set(haxes(1), 'YLim', [-0.5 0.1]);
set(haxes(1), 'YMinorTick', 'on');
set(haxes(1), 'YTick', [-0.5:0.1:0]);
set(haxes(2), 'YLim', [2.6 3.2]);
set(haxes(2), 'YMinorTick', 'on');
set(haxes(2), 'YTick', [2.6:0.1:3.2]);
for iaxis = 1:2
set(haxes(iaxis), 'XLim', [-1 1]);
end
title('Solution by Chebyshev Collocation');
xlabel('x');
ylabel(haxes(1), 'y_1');
ylabel(haxes(2), 'y_2');
legend('y_1','y_2','Location','Best')
set(hline1, 'Linewidth', 0.25, 'Marker', '+', 'LineStyle', '-');
set(hline2, 'Linewidth', 0.25, 'Marker', 'x', 'LineStyle', '--');
d02tg example 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.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)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015