D02 Chapter Contents
D02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentD02TGF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

D02TGF solves a system of linear ordinary differential equations by least squares fitting of a series of Chebyshev polynomials using collocation.

## 2  Specification

 SUBROUTINE D02TGF ( N, M, L, X0, X1, K1, KP, C, LDC, COEFF, BDYC, W, LW, IW, LIW, IFAIL)
 INTEGER N, M(N), L(N), K1, KP, LDC, LW, IW(LIW), LIW, IFAIL REAL (KIND=nag_wp) X0, X1, C(LDC,N), W(LW) EXTERNAL COEFF, BDYC

## 3  Description

D02TGF calculates an approximate solution of a linear or linearized system of ordinary differential equations as a Chebyshev series. Suppose there are $n$ differential equations for $n$ variables ${y}_{1},{y}_{2},\dots ,{y}_{n}$, over the range $\left({x}_{0},{x}_{1}\right)$. Let the $i$th equation be
 $∑ j=1 mi+1 ∑ k=1 n fkjix y kj-1 x= rix$
where ${y}_{k}^{\left(j\right)}\left(x\right)=\frac{{d}^{j}{y}_{k}\left(x\right)}{d{x}^{j}}$. COEFF evaluates the coefficients ${f}_{kj}^{i}\left(x\right)$ and the right-hand side ${r}^{i}\left(x\right)$ for each $i$, $1\le i\le n$, at any point $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$th boundary condition out of those associated with the $i$th differential equation takes the form
 $∑ j=1 li+1 ∑ k=1 n f kj ij xij ykj-1 xij=rij xij ,$
where ${x}^{ij}$ lies between ${x}_{0}$ and ${x}_{1}$. It is assumed in this routine 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, ${k}_{1}-1$, and the number of collocation points, ${k}_{p}$, in the range. The routine sets up a system of linear equations for the Chebyshev coefficients, with $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 ${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$ functions ${y}_{1},{y}_{2},\dots ,{y}_{n}$, with the range normalized to $\left[-1,1\right]$.
E02AKF can be used to evaluate the components of the solution at any point on the range $\left[{x}_{0},{x}_{1}\right]$ (see Section 9 for an example). E02AHF and E02AJF may be used to obtain Chebyshev series representations of derivatives and integrals (respectively) of the components of the solution.

## 4  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

## 5  Parameters

1:     N – INTEGERInput
On entry: $n$, the number of differential equations in the system.
Constraint: ${\mathbf{N}}\ge 1$.
2:     M(N) – INTEGER arrayInput
On entry: ${\mathbf{M}}\left(\mathit{i}\right)$ must be set to the highest order derivative occurring in the $\mathit{i}$th equation, for $\mathit{i}=1,2,\dots ,n$.
Constraint: ${\mathbf{M}}\left(\mathit{i}\right)\ge 1$, for $\mathit{i}=1,2,\dots ,n$.
3:     L(N) – INTEGER arrayInput
On entry: ${\mathbf{L}}\left(\mathit{i}\right)$ must be set to the number of boundary conditions associated with the $\mathit{i}$th equation, for $\mathit{i}=1,2,\dots ,n$.
Constraint: ${\mathbf{L}}\left(\mathit{i}\right)\ge 0$, for $\mathit{i}=1,2,\dots ,n$.
4:     X0 – REAL (KIND=nag_wp)Input
On entry: the left-hand boundary, ${x}_{0}$.
5:     X1 – REAL (KIND=nag_wp)Input
On entry: the right-hand boundary, ${x}_{1}$.
Constraint: ${\mathbf{X1}}>{\mathbf{X0}}$.
6:     K1 – INTEGERInput
On entry: the number of coefficients, ${k}_{1}$, to be returned in the Chebyshev series representation of the solution (hence, the degree of the polynomial approximation is ${\mathbf{K1}}-1$).
Constraint: ${\mathbf{K1}}\ge 1+\underset{1\le i\le {\mathbf{N}}}{\mathrm{max}}\phantom{\rule{0.25em}{0ex}}{\mathbf{M}}\left(i\right)$.
7:     KP – INTEGERInput
On entry: the number of collocation points to be used, ${k}_{p}$.
Constraint: ${\mathbf{N}}×{\mathbf{KP}}\ge {\mathbf{N}}×{\mathbf{K1}}+\sum _{i=1}^{{\mathbf{N}}}{\mathbf{L}}\left(i\right)$.
8:     C(LDC,N) – REAL (KIND=nag_wp) arrayOutput
On exit: the $k$th column of C contains the computed Chebyshev coefficients of the $k$th component of the solution, ${y}_{k}$; that is, the computed solution is:
 $yk=∑′i=1k1CikTi-1x, 1≤k≤n,$
where ${T}_{i}\left(x\right)$ is the Chebyshev polynomial of the first kind and ${\sum }^{\prime }$ denotes that the first coefficient, ${\mathbf{C}}\left(1,k\right)$, is halved.
9:     LDC – INTEGERInput
On entry: the first dimension of the array C as declared in the (sub)program from which D02TGF is called.
Constraint: ${\mathbf{LDC}}\ge {\mathbf{K1}}$.
10:   COEFF – SUBROUTINE, supplied by the user.External Procedure
COEFF defines the system of differential equations (see Section 3). It must evaluate the coefficient functions ${f}_{kj}^{i}\left(x\right)$ and the right-hand side function ${r}^{i}\left(x\right)$ of the $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 D02TGF before calling COEFF.
The specification of COEFF is:
 SUBROUTINE COEFF ( X, I, A, IA, IA1, RHS)
 INTEGER I, IA, IA1 REAL (KIND=nag_wp) X, A(IA,IA1), RHS
Important: the dimension declaration for A must contain the variable IA, not an integer constant.
1:     X – REAL (KIND=nag_wp)Input
On entry: $x$, the point at which the functions must be evaluated.
2:     I – INTEGERInput
On entry: the equation for which the coefficients and right-hand side are to be evaluated.
3:     A(IA,IA1) – REAL (KIND=nag_wp) arrayInput/Output
On entry: all elements of A are set to zero.
On exit: ${\mathbf{A}}\left(k,j\right)$ must contain the value ${f}_{kj}^{i}\left(x\right)$, for $1\le k\le n$, $1\le j\le {m}_{i}+1$.
4:     IA – INTEGERInput
5:     IA1 – INTEGERInput
On entry: the first dimension of the array A and the second dimension of the array A as declared in the (sub)program from which D02TGF is called.
6:     RHS – REAL (KIND=nag_wp)Input/Output
On entry: is set to zero.
On exit: it must contain the value ${r}^{i}\left(x\right)$.
COEFF must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02TGF is called. Parameters denoted as Input must not be changed by this procedure.
11:   BDYC – SUBROUTINE, supplied by the user.External Procedure
BDYC defines the boundary conditions (see Section 3). It must evaluate the coefficient functions ${f}_{kj}^{ij}$ and right-hand side function ${r}^{ij}$ in the $j$th boundary condition associated with the $i$th equation, at the point ${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 D02TGF before calling BDYC.
The specification of BDYC is:
 SUBROUTINE BDYC ( X, I, J, A, IA, IA1, RHS)
 INTEGER I, J, IA, IA1 REAL (KIND=nag_wp) X, A(IA,IA1), RHS
Important: the dimension declaration for A must contain the variable IA, not an integer constant.
1:     X – REAL (KIND=nag_wp)Output
On exit: ${x}^{ij}$, the value at which the boundary condition is applied.
2:     I – INTEGERInput
On entry: the differential equation with which the condition is associated.
3:     J – INTEGERInput
On entry: the boundary condition for which the coefficients and right-hand side are to be evaluated.
4:     A(IA,IA1) – REAL (KIND=nag_wp) arrayInput/Output
On entry: all elements of A are set to zero.
On exit: the value ${f}_{kj}^{ij}\left({x}^{ij}\right)$, for $1\le k\le n$, $1\le j\le {m}_{i}+1$.
5:     IA – INTEGERInput
6:     IA1 – INTEGERInput
On entry: the first dimension of the array A and the second dimension of the array A as declared in the (sub)program from which D02TGF is called.
7:     RHS – REAL (KIND=nag_wp)Input/Output
On entry: is set to zero.
On exit: the value ${r}^{ij}\left({x}^{ij}\right)$.
BDYC must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02TGF is called. Parameters denoted as Input must not be changed by this procedure.
12:   W(LW) – REAL (KIND=nag_wp) arrayWorkspace
13:   LW – INTEGERInput
On entry: the dimension of the array W as declared in the (sub)program from which D02TGF is called.
Constraint: ${\mathbf{LW}}\ge 2×\left({\mathbf{N}}×{\mathbf{KP}}+\mathit{NL}\right)×\left({\mathbf{N}}×{\mathbf{K1}}+1\right)+7×{\mathbf{N}}×{\mathbf{K1}}$, where $\mathit{NL}=\sum _{i=1}^{n}{\mathbf{L}}\left(i\right)$ .
14:   IW(LIW) – INTEGER arrayWorkspace
15:   LIW – INTEGERInput
On entry: the dimension of the array IW as declared in the (sub)program from which D02TGF is called.
Constraint: ${\mathbf{LIW}}\ge {\mathbf{N}}×{\mathbf{K1}}+1$.
16:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{N}}<1$, or ${\mathbf{M}}\left(i\right)<1$ for some $i$, or ${\mathbf{L}}\left(i\right)<0$ for some $i$, or ${\mathbf{X0}}\ge {\mathbf{X1}}$, or ${\mathbf{K1}}<1+{\mathbf{M}}\left(i\right)$ for some $i$, or ${\mathbf{N}}×{\mathbf{KP}}<{\mathbf{N}}×{\mathbf{K1}}+\sum _{i=1}^{n}{\mathbf{L}}\left(i\right)$, or ${\mathbf{LDC}}<{\mathbf{K1}}$.
${\mathbf{IFAIL}}=2$
 On entry, LW is too small (see Section 5), or ${\mathbf{LIW}}<{\mathbf{N}}×{\mathbf{K1}}$.
${\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.
${\mathbf{IFAIL}}=4$
The least squares routine F04AMF has failed to correct the first approximate solution (see F04AMF). Increasing KP may remove this difficulty.

## 7  Accuracy

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

The time taken by D02TGF 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 ${k}_{p}$ is equal to the number of coefficients ${k}_{1}$ minus the average number of boundary conditions $\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 ${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 $\left[-1,1\right]$.
Generally the number of boundary conditions required is equal to the sum of the orders of the $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 routine. The calculation is repeated iteratively. On each iteration the linearized equation is used. In the example in Section 9, the $y$ variables are to be determined at the current iteration whilst the $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 ${y}_{1}=ax+b$ in the range $\left({x}_{0},{x}_{1}\right)$, we set $\mathrm{B}$, the array of coefficients,
• $\mathrm{B}\left(1,1\right)=a×\left({x}_{0}+{x}_{1}\right)+2×b$,
• $\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 E02ADF or E02AFF to obtain a Chebyshev series for an approximate solution. The coefficients of the current iterate must be communicated to COEFF and BDYC, e.g., in COMMON. (See Section 9.) The convergence of the (Newton) iteration cannot be guaranteed in general, though it is usually satisfactory from a good starting approximation.

## 9  Example

This example solves the nonlinear system
 $2y1′+y22-1 y1+y2=0, 2y2′′-y1′=0,$
in the range $\left(-1,1\right)$, with ${y}_{1}=0$, ${y}_{2}=3$, ${y}_{2}^{\prime }=0$ at $x=-1$.
Suppose an approximate solution is ${z}_{1}$, ${z}_{2}$ such that ${y}_{1}\sim {z}_{1}$, ${y}_{2}\sim {z}_{2}$: then the first equation gives, on linearizing,
 $2y1′+z22-1 y1+2z1z2+1 y2=2z1z22.$
The starting approximation is taken to be ${z}_{1}=0$, ${z}_{2}=3$. In the program below, the array $\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. E02AKF is used to calculate the solution from its Chebyshev coefficients.

### 9.1  Program Text

Program Text (d02tgfe.f90)

### 9.2  Program Data

Program Data (d02tgfe.d)

### 9.3  Program Results

Program Results (d02tgfe.r)