# NAG FL Interfaced02jaf (bvp_​coll_​nth)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine d02jaf ( n, cf, bc, x0, x1, k1, kp, c, w, lw, iw,
 Integer, Intent (In) :: n, k1, kp, lw Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: iw(k1) Real (Kind=nag_wp), External :: cf Real (Kind=nag_wp), Intent (In) :: x0, x1 Real (Kind=nag_wp), Intent (Out) :: c(k1), w(lw) External :: bc
#include <nag.h>
 void d02jaf_ (const Integer *n, double (NAG_CALL *cf)(const Integer *j, const double *x),void (NAG_CALL *bc)(const Integer *i, Integer *j, double *rhs),const double *x0, const double *x1, const Integer *k1, const Integer *kp, double c[], double w[], const Integer *lw, Integer iw[], Integer *ifail)
The routine may be called by the names d02jaf or nagf_ode_bvp_coll_nth.

## 3Description

d02jaf calculates the solution of a regular two-point boundary value problem for a single $n$th-order linear ordinary differential equation as a Chebyshev series in the interval $\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)$
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, 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 $\left(-1,1\right)$.
e02akf can be used to evaluate the solution at any point on the interval $\left({x}_{0},{x}_{1}\right)$. e02ahf followed by e02akf can be used to evaluate its derivatives.
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

## 5Arguments

1: $\mathbf{n}$Integer Input
On entry: $n$, the order of the differential equation.
Constraint: ${\mathbf{n}}\ge 1$.
2: $\mathbf{cf}$real (Kind=nag_wp) Function, supplied by the user. External Procedure
cf defines the differential equation (see Section 3). It must return the value of a function ${f}_{j}\left(x\right)$ at a given point $x$, where, for $1\le j\le n+1$, ${f}_{j}\left(x\right)$ is the coefficient of ${y}^{\left(j-1\right)}\left(x\right)$ in the equation, and ${f}_{0}\left(x\right)$ is the right-hand side.
The specification of cf is:
Fortran Interface
 Function cf ( j, x)
 Real (Kind=nag_wp) :: cf Integer, Intent (In) :: j Real (Kind=nag_wp), Intent (In) :: x
 double cf (const Integer *j, const double *x)
1: $\mathbf{j}$Integer Input
On entry: the index of the function ${f}_{j}$ to be evaluated.
2: $\mathbf{x}$Real (Kind=nag_wp) Input
On entry: the point at which ${f}_{j}$ is to be evaluated.
cf must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which d02jaf is called. Arguments denoted as Input must not be changed by this procedure.
Note: cf should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by d02jaf. If your code inadvertently does return any NaNs or infinities, d02jaf is likely to produce unexpected results.
3: $\mathbf{bc}$Subroutine, supplied by the user. External Procedure
bc defines the boundary conditions, each of which has the form ${y}^{\left(k-1\right)}\left({x}_{1}\right)={s}_{k}$ or ${y}^{\left(k-1\right)}\left({x}_{0}\right)={s}_{k}$. The boundary conditions may be specified in any order.
The specification of bc is:
Fortran Interface
 Subroutine bc ( i, j, rhs)
 Integer, Intent (In) :: i Integer, Intent (Out) :: j Real (Kind=nag_wp), Intent (Out) :: rhs
 void bc (const Integer *i, Integer *j, double *rhs)
1: $\mathbf{i}$Integer Input
On entry: the index of the boundary condition to be defined.
2: $\mathbf{j}$Integer Output
On exit: must be set to $-k$ if the boundary condition is ${y}^{\left(k-1\right)}\left({x}_{0}\right)={s}_{k}$, and to $+k$ if it is ${y}^{\left(k-1\right)}\left({x}_{1}\right)={s}_{k}$.
j must not be set to the same value $k$ for two different values of i.
3: $\mathbf{rhs}$Real (Kind=nag_wp) Output
On exit: must be set to the value ${s}_{k}$.
bc must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which d02jaf is called. Arguments denoted as Input must not be changed by this procedure.
Note: bc should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by d02jaf. If your code inadvertently does return any NaNs or infinities, d02jaf is likely to produce unexpected results.
4: $\mathbf{x0}$Real (Kind=nag_wp) Input
5: $\mathbf{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: $\mathbf{k1}$Integer Input
On entry: the number of coefficients 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 {\mathbf{n}}+1$.
7: $\mathbf{kp}$Integer Input
On entry: the number of collocation points to be used.
Constraint: ${\mathbf{kp}}\ge {\mathbf{k1}}-{\mathbf{n}}$.
8: $\mathbf{c}\left({\mathbf{k1}}\right)$Real (Kind=nag_wp) array Output
On exit: the computed Chebyshev coefficients; that is, the computed solution is:
 $∑′i=1k1c(i)Ti-1(x)$
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\right)$, is halved.
9: $\mathbf{w}\left({\mathbf{lw}}\right)$Real (Kind=nag_wp) array Workspace
10: $\mathbf{lw}$Integer Input
On entry: the dimension of the array w as declared in the (sub)program from which d02jaf is called.
Constraint: ${\mathbf{lw}}\ge 2×\left({\mathbf{kp}}+{\mathbf{n}}\right)×\left({\mathbf{k1}}+1\right)+7×{\mathbf{k1}}$.
11: $\mathbf{iw}\left({\mathbf{k1}}\right)$Integer array Workspace
12: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ 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).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-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{k1}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{k1}}\ge {\mathbf{n}}+1$.
On entry, ${\mathbf{kp}}=⟨\mathit{\text{value}}⟩$, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{k1}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{kp}}+{\mathbf{n}}\ge {\mathbf{k1}}$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 1$.
On entry, ${\mathbf{x1}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{x0}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{x1}}>{\mathbf{x0}}$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{lw}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{lw}}\ge 2×\left({\mathbf{kp}}+{\mathbf{n}}\right)×\left({\mathbf{k1}}+1\right)+7×{\mathbf{k1}}$; that is, $⟨\mathit{\text{value}}⟩$.
${\mathbf{ifail}}=3$
Either the boundary conditions are not linearly independent, or the coefficient matrix is rank deficient. Increasing the number of collocation points may overcome this latter problem.
${\mathbf{ifail}}=4$
Iterative refinement in the least squares solution has failed to converge. The coefficient matrix is too ill-conditioned.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

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 9).

## 8Parallelism and Performance

d02jaf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

The time taken by d02jaf 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 $\left({x}_{0},{x}_{1}\right)$ are chosen to be the extrema of the appropriate shifted Chebyshev polynomial. If ${\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 ${\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 $\left(-1,1\right)$.
Systems of regular linear differential equations can be solved using d02jbf. It is necessary before using d02jbf 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 d02tgf.

## 10Example

This example solves the equation
 $y′′ + y = 1$
with boundary conditions
 $y(-1) = y(1) = 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)$.

### 10.1Program Text

Program Text (d02jafe.f90)

### 10.2Program Data

Program Data (d02jafe.d)

### 10.3Program Results

Program Results (d02jafe.r)