D02 Chapter Contents
D02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentD02UDF

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

D02UDF differentiates a function discretized on Chebyshev Gauss–Lobatto points. The grid points on which the function values are to be provided are normally returned by a previous call to D02UCF.

## 2  Specification

 SUBROUTINE D02UDF ( N, F, FD, IFAIL)
 INTEGER N, IFAIL REAL (KIND=nag_wp) F(N+1), FD(N+1)

## 3  Description

D02UDF differentiates a function discretized on Chebyshev Gauss–Lobatto points on $\left[-1,1\right]$. The polynomial interpolation on Chebyshev points is equivalent to trigonometric interpolation on equally spaced points. Hence the differentiation on the Chebyshev points can be implemented by the Fast Fourier transform (FFT).
Given the function values $f\left({x}_{i}\right)$ on Chebyshev Gauss–Lobatto points ${x}_{\mathit{i}}=-\mathrm{cos}\left(\left(\mathit{i}-1\right)\pi /n\right)$, for $\mathit{i}=1,2,\dots ,n+1$, $f$ is differentiated with respect to $x$ by means of forward and backward FFTs on the function values $f\left({x}_{i}\right)$. D02UDF returns the computed derivative values ${f}^{\prime }\left({x}_{\mathit{i}}\right)$, for $\mathit{i}=1,2,\dots ,n+1$. The derivatives are computed with respect to the standard Chebyshev Gauss–Lobatto points on $\left[-1,1\right]$; for derivatives of a function on $\left[a,b\right]$ the returned values have to be scaled by a factor $2/\left(b-a\right)$.

## 4  References

Canuto C, Hussaini M Y, Quarteroni A and Zang T A (2006) Spectral Methods: Fundamentals in Single Domains Springer
Greengard L (1991) Spectral integration and two-point boundary value problems SIAM J. Numer. Anal. 28(4) 1071–80
Trefethen L N (2000) Spectral Methods in MATLAB SIAM

## 5  Parameters

1:     N – INTEGERInput
On entry: $n$, where the number of grid points is $n+1$. The fast Fourier transform requires that the prime factorization of $n$ contain no more than $30$ prime factors.
Constraint: ${\mathbf{N}}>0$ and N is even.
2:     F(${\mathbf{N}}+1$) – REAL (KIND=nag_wp) arrayInput
On entry: the function values $f\left({x}_{\mathit{i}}\right)$, for $\mathit{i}=1,2,\dots ,n+1$
3:     FD(${\mathbf{N}}+1$) – REAL (KIND=nag_wp) arrayOutput
On exit: the approximations to the derivatives of the function evaluated at the Chebyshev Gauss–Lobatto points. For functions defined on $\left[a,b\right]$, the returned derivative values (corresponding to the domain $\left[-1,1\right]$) must be multiplied by the factor $2/\left(b-a\right)$ to obtain the correct values on $\left[a,b\right]$.
4:     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}}\le 0$ or N is odd.
${\mathbf{IFAIL}}=2$
An unexpected error has occurred in an internal call. Check all subroutine calls and array dimensions. Seek expert help.
${\mathbf{IFAIL}}=-999$
Internal memory allocation failed.

## 7  Accuracy

The accuracy is close to machine precision for small numbers of grid points, typically less than 100. For larger numbers of grid points, the error in differentiation grows with the number of grid points. See Greengard (1991) for more details.

The number of operations is of the order $n\mathrm{log}n$ and the memory requirements are $\mathit{O}\left(n\right)$; thus the computation remains efficient and practical for very fine discretizations (very large values of $n$).

## 9  Example

The function $2x+\mathrm{exp}\left(-x\right)$, defined on $\left[0,1.5\right]$, is supplied and then differentiated on a grid.

### 9.1  Program Text

Program Text (d02udfe.f90)

### 9.2  Program Data

Program Data (d02udfe.d)

### 9.3  Program Results

Program Results (d02udfe.r)