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 [1,1]$\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(xi)$f\left({x}_{i}\right)$ on Chebyshev Gauss–Lobatto points xi = cos((i1)π / n) ${x}_{\mathit{i}}=-\mathrm{cos}\left(\left(\mathit{i}-1\right)\pi /n\right)$, for i = 1,2,,n + 1$\mathit{i}=1,2,\dots ,n+1$, f$f$ is differentiated with respect to x$x$ by means of forward and backward FFTs on the function values f(xi)$f\left({x}_{i}\right)$. nag_ode_bvp_ps_lin_cgl_deriv (d02ud) returns the computed derivative values f(xi)${f}^{\prime }\left({x}_{\mathit{i}}\right)$, for i = 1,2,,n + 1$\mathit{i}=1,2,\dots ,n+1$. The derivatives are computed with respect to the standard Chebyshev Gauss–Lobatto points on [1,1]$\left[-1,1\right]$; for derivatives of a function on [a,b]$\left[a,b\right]$ the returned values have to be scaled by a factor 2 / (ba)$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:     n – int64int32nag_int scalar
n$n$, where the number of grid points is n + 1$n+1$. The fast Fourier transform requires that the prime factorization of n$n$ contain no more than 30$30$ prime factors.
Constraint: n > 0${\mathbf{n}}>0$ and n is even.
2:     f(n + 1${\mathbf{n}}+1$) – double array
The function values f(xi)$f\left({x}_{\mathit{i}}\right)$, for i = 1,2,,n + 1$\mathit{i}=1,2,\dots ,n+1$

None.

None.

### Output Parameters

1:     fd(n + 1${\mathbf{n}}+1$) – double array
The approximations to the derivatives of the function evaluated at the Chebyshev Gauss–Lobatto points. For functions defined on [a,b]$\left[a,b\right]$, the returned derivative values (corresponding to the domain [1,1]$\left[-1,1\right]$) must be multiplied by the factor 2 / (ba)$2/\left(b-a\right)$ to obtain the correct values on [a,b]$\left[a,b\right]$.
2:     ifail – int64int32nag_int scalar
${\mathrm{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}}=\mathbf{}$
Constraint: n > 0${\mathbf{n}}>0$.
Constraint: n is even.
ifail = 999${\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 log(n) $n\mathrm{log}\left(n\right)$ and the memory requirements are O(n)$\mathit{O}\left(n\right)$; thus the computation remains efficient and practical for very fine discretizations (very large values of n$n$).

## Example

```function nag_ode_bvp_ps_lin_cgl_deriv_example
n = int64(16);
a = 0;
b = 1.5;

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

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

% Calculate derivative of function
[fd, ifail] = nag_ode_bvp_ps_lin_cgl_deriv(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');
for i=1:17
fprintf('%10.4f %10.4f %10.4f\n', x(i), f(i), fd(i));
end
```
```

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

```
```function d02ud_example
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');
for i=1:17
fprintf('%10.4f %10.4f %10.4f\n', x(i), f(i), fd(i));
end
```
```

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

```