Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_ode_bvp_ps_lin_cgl_deriv (d02ud)

## Purpose

nag_ode_bvp_ps_lin_cgl_deriv (d02ud) 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 nag_ode_bvp_ps_lin_cgl_grid (d02uc).

## Syntax

[fd, ifail] = d02ud(n, f)
[fd, ifail] = nag_ode_bvp_ps_lin_cgl_deriv(n, f)

## Description

nag_ode_bvp_ps_lin_cgl_deriv (d02ud) 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)$. nag_ode_bvp_ps_lin_cgl_deriv (d02ud) 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)$.

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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
$n$, where the number of grid points is $n+1$.
Constraint: ${\mathbf{n}}>0$ and n is even.
2:     $\mathrm{f}\left({\mathbf{n}}+1\right)$ – double array
The function values $f\left({x}_{\mathit{i}}\right)$, for $\mathit{i}=1,2,\dots ,n+1$

None.

### Output Parameters

1:     $\mathrm{fd}\left({\mathbf{n}}+1\right)$ – double array
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]$.
2:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
Constraint: ${\mathbf{n}}>0$.
Constraint: n is even.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## 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}\left(n\right)$ 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$).

## Example

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

fprintf('d02ud example results\n\n');

n = int64(16);
a = 0;
b = 1.5;

% Set up solution grid
[x, ifail] = d02uc(n, a, b);

% Evaluate function on Chebyshev grid
f = 2*x+exp(-x);

% Calculate derivative of function
[fd, ifail] = d02ud(n, f);

scale = 2/(b-a);
fd = scale*fd;

% Print function and its derivatives
fprintf('\nOriginal function F and numerical derivative Fx\n');
fprintf('      x          F          Fx\n');
fprintf('%10.4f %10.4f %10.4f\n', [x f fd]');

```
```d02ud example results

Original function F and numerical derivative Fx
x          F          Fx
0.0000     1.0000     1.0000
0.0144     1.0145     1.0143
0.0571     1.0587     1.0555
0.1264     1.1341     1.1187
0.2197     1.2421     1.1972
0.3333     1.3832     1.2835
0.4630     1.5554     1.3706
0.6037     1.7542     1.4532
0.7500     1.9724     1.5276
0.8963     2.2007     1.5919
1.0370     2.4285     1.6455
1.1667     2.6448     1.6886
1.2803     2.8386     1.7221
1.3736     3.0004     1.7468
1.4429     3.1221     1.7638
1.4856     3.1975     1.7736
1.5000     3.2231     1.7769
```