D02 Chapter Contents
D02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentD02JBF

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

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

## 2  Specification

 SUBROUTINE D02JBF ( N, CF, BC, X0, X1, K1, KP, C, LDC, W, LW, IW, LIW, IFAIL)
 INTEGER N, K1, KP, LDC, LW, IW(LIW), LIW, IFAIL REAL (KIND=nag_wp) CF, X0, X1, C(LDC,N), W(LW) EXTERNAL CF, BC

## 3  Description

D02JBF calculates the solution of a regular two-point boundary value problem for a regular linear $n$th-order system of first-order ordinary differential equations as a Chebyshev series in the interval $\left({x}_{0},{x}_{1}\right)$. The differential equation
 $y′=Axy+rx$
is defined by CF, and the boundary conditions at the points ${x}_{0}$ and ${x}_{1}$ are defined by BC.
You specify the degree of Chebyshev series required, ${\mathbf{K1}}-1$, and the number of collocation points, KP. The routine sets up a system of linear equations for the Chebyshev coefficients, $n$ 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 $\left(-1,1\right)$.
E02AKF can be used to evaluate the components of the solution at any point on the interval $\left({x}_{0},{x}_{1}\right)$ – see Section 9 for an example. E02AHF followed by E02AKF can be used to evaluate their derivatives.

## 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 order of the system of differential equations.
Constraint: ${\mathbf{N}}\ge 1$.
2:     CF – REAL (KIND=nag_wp) FUNCTION, supplied by the user.External Procedure
CF defines the system of differential equations (see Section 3). It must return the value of a coefficient function ${a}_{i,j}\left(x\right)$, of $A$, at a given point $x$, or of a right-hand side function ${r}_{i}\left(x\right)$ if ${\mathbf{J}}=0$.
The specification of CF is:
 FUNCTION CF ( I, J, X)
 REAL (KIND=nag_wp) CF
 INTEGER I, J REAL (KIND=nag_wp) X
1:     I – INTEGERInput
2:     J – INTEGERInput
On entry: indicate the function to be evaluated, namely ${a}_{i,j}\left(x\right)$ if $1\le {\mathbf{J}}\le n$, or ${r}_{i}\left(x\right)$ if ${\mathbf{J}}=0$.
$1\le {\mathbf{I}}\le n$, $0\le {\mathbf{J}}\le n$.
3:     X – REAL (KIND=nag_wp)Input
On entry: the point at which the function is to be evaluated.
CF must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02JBF is called. Parameters denoted as Input must not be changed by this procedure.
3:     BC – SUBROUTINE, supplied by the user.External Procedure
BC defines the $n$ boundary conditions, which have the form ${y}_{k}\left({x}_{0}\right)=s$ or ${y}_{k}\left({x}_{1}\right)=s$. The boundary conditions may be specified in any order.
The specification of BC is:
 SUBROUTINE BC ( I, J, RHS)
 INTEGER I, J REAL (KIND=nag_wp) RHS
1:     I – INTEGERInput
On entry: the index of the boundary condition to be defined.
2:     J – INTEGEROutput
On exit: must be set to $-k$ if the $i$th boundary condition is ${y}_{k}\left({x}_{0}\right)=s$, or to $+k$ if it is ${y}_{k}\left({x}_{1}\right)=s$.
J must not be set to the same value $k$ for two different values of I.
3:     RHS – REAL (KIND=nag_wp)Output
On exit: the value $s$.
BC must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D02JBF is called. Parameters denoted as Input must not be changed by this procedure.
4:     X0 – REAL (KIND=nag_wp)Input
5:     X1 – REAL (KIND=nag_wp)Input
On entry: the left- and right-hand boundaries, ${x}_{0}$ and ${x}_{1}$, respectively.
Constraint: ${\mathbf{X1}}>{\mathbf{X0}}$.
6:     K1 – INTEGERInput
On entry: 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 ${\mathbf{K1}}-1$).
Constraint: ${\mathbf{K1}}\ge 2$.
7:     KP – INTEGERInput
On entry: the number of collocation points to be used.
Constraint: ${\mathbf{KP}}\ge {\mathbf{K1}}-1$.
8:     C(LDC,N) – REAL (KIND=nag_wp) arrayOutput
On exit: 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 $i$th 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 D02JBF is called.
Constraint: ${\mathbf{LDC}}\ge {\mathbf{K1}}$.
10:   W(LW) – REAL (KIND=nag_wp) arrayWorkspace
11:   LW – INTEGERInput
On entry: the dimension of the array W as declared in the (sub)program from which D02JBF is called.
Constraint: ${\mathbf{LW}}\ge 2×{\mathbf{N}}×\left({\mathbf{KP}}+1\right)×\left({\mathbf{N}}×{\mathbf{K1}}+1\right)+7×{\mathbf{N}}×{\mathbf{K1}}$.
12:   IW(LIW) – INTEGER arrayWorkspace
13:   LIW – INTEGERInput
On entry: the dimension of the array IW as declared in the (sub)program from which D02JBF is called.
Constraint: ${\mathbf{LIW}}\ge {\mathbf{N}}×\left({\mathbf{K1}}+2\right)$.
14:   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{X0}}\ge {\mathbf{X1}}$, or ${\mathbf{K1}}<2$, or ${\mathbf{KP}}<{\mathbf{K1}}-1$, or ${\mathbf{LDC}}<{\mathbf{K1}}$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{LW}}<2×{\mathbf{N}}×\left({\mathbf{KP}}+1\right)×\left({\mathbf{N}}×{\mathbf{K1}}+1\right)+7×{\mathbf{N}}×{\mathbf{K1}}$, or ${\mathbf{LIW}}<{\mathbf{N}}×\left({\mathbf{K1}}+2\right)$ (i.e., insufficient workspace).
${\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$ for two different values of I), 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).

## 7  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 8).

The time taken by D02JBF 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 $\left({x}_{0},{x}_{1}\right)$ are chosen to be the extrema of the appropriate shifted Chebyshev polynomial. If ${\mathbf{KP}}={\mathbf{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 ${\mathbf{KP}}\gg {\mathbf{K1}}-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 $\left(-1,1\right)$.
Linear systems of high-order equations in their original form, singular problems, and, indirectly, nonlinear problems can be solved using D02TGF.

## 9  Example

This example solves the equation
 $y′′ + y = 1$
with boundary conditions
 $y-1 = y1 = 0 .$
The equation is written as the first-order system
 $y1′ y2′ = 0 1 -1 0 y1 y2 + 0 1$
for solution by D02JBF and the boundary conditions are written
 $y1- 1=y11=0.$
We use ${\mathbf{K1}}=4$, $6$ and $8$, and ${\mathbf{KP}}=10$ and $15$, so that the different Chebyshev series may be compared. The solution for ${\mathbf{K1}}=8$ and ${\mathbf{KP}}=15$ is evaluated by E02AKF at nine equally spaced points over the interval $\left(-1,1\right)$.

### 9.1  Program Text

Program Text (d02jbfe.f90)

### 9.2  Program Data

Program Data (d02jbfe.d)

### 9.3  Program Results

Program Results (d02jbfe.r)