# NAG Library Routine Document

## 1Purpose

e02aff computes the coefficients of a polynomial, in its Chebyshev series form, which interpolates (passes exactly through) data at a special set of points. Least squares polynomial approximations can also be obtained.

## 2Specification

Fortran Interface
 Subroutine e02aff ( f, a,
 Integer, Intent (In) :: nplus1 Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: f(nplus1) Real (Kind=nag_wp), Intent (Out) :: a(nplus1)
#include <nagmk26.h>
 void e02aff_ (const Integer *nplus1, const double f[], double a[], Integer *ifail)

## 3Description

e02aff computes the coefficients ${a}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,n+1$, in the Chebyshev series
 $12a1T0x-+a2T1x-+a3T2x-+⋯+an+1Tnx-,$
which interpolates the data ${f}_{r}$ at the points
 $x-r=cosr-1π/n , r=1,2,…,n+1.$
Here ${T}_{j}\left(\stackrel{-}{x}\right)$ denotes the Chebyshev polynomial of the first kind of degree $j$ with argument $\stackrel{-}{x}$. The use of these points minimizes the risk of unwanted fluctuations in the polynomial and is recommended when the data abscissae can be chosen by you, e.g., when the data is given as a graph. For further advantages of this choice of points, see Clenshaw (1962).
In terms of your original variables, $x$ say, the values of $x$ at which the data ${f}_{r}$ are to be provided are
 $xr=12xmax-xmincosπr-1/n+12xmax+xmin, r=1,2,…,n+1$
where ${x}_{\mathrm{max}}$ and ${x}_{\mathrm{min}}$ are respectively the upper and lower ends of the range of $x$ over which you wish to interpolate.
Truncation of the resulting series after the term involving ${a}_{i+1}$, say, yields a least squares approximation to the data. This approximation, $p\left(\stackrel{-}{x}\right)$, say, is the polynomial of degree $i$ which minimizes
 $12ε12+ε22+ε32+⋯+εn2+12εn+12,$
where the residual ${\epsilon }_{\mathit{r}}=p\left({\stackrel{-}{x}}_{\mathit{r}}\right)-{f}_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,n+1$.
The method employed is based on the application of the three-term recurrence relation due to Clenshaw (1955) for the evaluation of the defining expression for the Chebyshev coefficients (see, for example, Clenshaw (1962)). The modifications to this recurrence relation suggested by Reinsch and Gentleman (see Gentleman (1969)) are used to give greater numerical stability.
For further details of the algorithm and its use see Cox (1974) and Cox and Hayes (1973).
Subsequent evaluation of the computed polynomial, perhaps truncated after an appropriate number of terms, should be carried out using e02aef.

## 4References

Clenshaw C W (1955) A note on the summation of Chebyshev series Math. Tables Aids Comput. 9 118–120
Clenshaw C W (1962) Chebyshev Series for Mathematical Functions Mathematical tables HMSO
Cox M G (1974) A data-fitting package for the non-specialist user Software for Numerical Mathematics (ed D J Evans) Academic Press
Cox M G and Hayes J G (1973) Curve fitting: a guide and suite of algorithms for the non-specialist user NPL Report NAC26 National Physical Laboratory
Gentleman W M (1969) An error analysis of Goertzel's (Watt's) method for computing Fourier coefficients Comput. J. 12 160–165

## 5Arguments

1:     $\mathbf{nplus1}$ – IntegerInput
On entry: the number $n+1$ of data points (one greater than the degree $n$ of the interpolating polynomial).
Constraint: ${\mathbf{nplus1}}\ge 2$.
2:     $\mathbf{f}\left({\mathbf{nplus1}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: for $r=1,2,\dots ,n+1$, ${\mathbf{f}}\left(r\right)$ must contain ${f}_{r}$ the value of the dependent variable (ordinate) corresponding to the value
 $x-r=cosπr-1/n$
of the independent variable (abscissa) $\stackrel{-}{x}$, or equivalently to the value
 $xr=12xmax-xmincosπr-1/n+12xmax+xmin$
of your original variable $x$. Here ${x}_{\mathrm{max}}$ and ${x}_{\mathrm{min}}$ are respectively the upper and lower ends of the range over which you wish to interpolate.
3:     $\mathbf{a}\left({\mathbf{nplus1}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: ${\mathbf{a}}\left(\mathit{j}\right)$ is the coefficient ${a}_{\mathit{j}}$ in the interpolating polynomial, for $\mathit{j}=1,2,\dots ,n+1$.
4:     $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, . If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value  is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value  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{nplus1}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nplus1}}\ge 2$.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

The rounding errors committed are such that the computed coefficients are exact for a slightly perturbed set of ordinates ${f}_{r}+\delta {f}_{r}$. The ratio of the sum of the absolute values of the $\delta {f}_{r}$ to the sum of the absolute values of the ${f}_{r}$ is less than a small multiple of $\left(n+1\right)\epsilon$, where $\epsilon$ is the machine precision.

## 8Parallelism and Performance

e02aff is not threaded in any implementation.

The time taken is approximately proportional to ${\left(n+1\right)}^{2}+30$.
For choice of degree when using the routine for least squares approximation, see Section 3.2 in the E02 Chapter Introduction.

## 10Example

Determine the Chebyshev coefficients of the polynomial which interpolates the data ${\stackrel{-}{x}}_{\mathit{r}},{f}_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,11$, where ${\stackrel{-}{x}}_{r}=\mathrm{cos}\left(\pi ×\left(r-1\right)/10\right)$ and ${f}_{r}={e}^{{\stackrel{-}{x}}_{r}}$. Evaluate, for comparison with the values of ${f}_{\mathit{r}}$, the resulting Chebyshev series at ${\stackrel{-}{x}}_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,11$.
The example program supplied is written in a general form that will enable polynomial interpolations of arbitrary data at the cosine points $\mathrm{cos}\left(\pi ×\left(\mathit{r}-1\right)/n\right)$, for $\mathit{r}=1,2,\dots ,n+1$, to be obtained for any $n$ ($\text{}={\mathbf{nplus1}}-1$). Note that e02aef is used to evaluate the interpolating polynomial. The program is self-starting in that any number of datasets can be supplied.

### 10.1Program Text

Program Text (e02affe.f90)

### 10.2Program Data

Program Data (e02affe.d)

### 10.3Program Results

Program Results (e02affe.r)