E02 Chapter Contents
E02 Chapter Introduction
NAG Library Manual

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

E02ADF computes weighted least squares polynomial approximations to an arbitrary set of data points.

## 2  Specification

 SUBROUTINE E02ADF ( M, KPLUS1, LDA, X, Y, W, WORK1, WORK2, A, S, IFAIL)
 INTEGER M, KPLUS1, LDA, IFAIL REAL (KIND=nag_wp) X(M), Y(M), W(M), WORK1(3*M), WORK2(2*KPLUS1), A(LDA,KPLUS1), S(KPLUS1)

## 3  Description

E02ADF determines least squares polynomial approximations of degrees $0,1,\dots ,k$ to the set of data points $\left({x}_{\mathit{r}},{y}_{\mathit{r}}\right)$ with weights ${w}_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,m$.
The approximation of degree $i$ has the property that it minimizes ${\sigma }_{i}$ the sum of squares of the weighted residuals ${\epsilon }_{r}$, where
 $εr=wryr-fr$
and ${f}_{r}$ is the value of the polynomial of degree $i$ at the $r$th data point.
Each polynomial is represented in Chebyshev series form with normalized argument $\stackrel{-}{x}$. This argument lies in the range $-1$ to $+1$ and is related to the original variable $x$ by the linear transformation
 $x-= 2x-xmax-xmin xmax-xmin .$
Here ${x}_{\mathrm{max}}$ and ${x}_{\mathrm{min}}$ are respectively the largest and smallest values of ${x}_{r}$. The polynomial approximation of degree $i$ is represented as
 $12ai+1,1T0x-+ai+1,2T1x-+ai+1,3T2x-+⋯+ai+1,i+1Tix-,$
where ${T}_{\mathit{j}}\left(\stackrel{-}{x}\right)$, for $\mathit{j}=0,1,\dots ,i$, are the Chebyshev polynomials of the first kind of degree $j$ with argument $\left(\stackrel{-}{x}\right)$.
For $i=0,1,\dots ,k$, the routine produces the values of ${a}_{i+1,\mathit{j}+1}$, for $\mathit{j}=0,1,\dots ,i$, together with the value of the root-mean-square residual ${s}_{i}=\sqrt{{\sigma }_{i}/\left(m-i-1\right)}$. In the case $m=i+1$ the routine sets the value of ${s}_{i}$ to zero.
The method employed is due to Forsythe (1957) and is based on the generation of a set of polynomials orthogonal with respect to summation over the normalized dataset. The extensions due to Clenshaw (1960) to represent these polynomials as well as the approximating polynomials in their Chebyshev series forms are incorporated. The modifications suggested by Reinsch and Gentleman (see Gentleman (1969)) to the method originally employed by Clenshaw for evaluating the orthogonal polynomials from their Chebyshev series representations 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 Chebyshev series representations of the polynomial approximations should be carried out using E02AEF.

## 4  References

Clenshaw C W (1960) Curve fitting with a digital computer Comput. J. 2 170–173
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
Forsythe G E (1957) Generation and use of orthogonal polynomials for data fitting with a digital computer J. Soc. Indust. Appl. Math. 5 74–88
Gentleman W M (1969) An error analysis of Goertzel's (Watt's) method for computing Fourier coefficients Comput. J. 12 160–165
Hayes J G (ed.) (1970) Numerical Approximation to Functions and Data Athlone Press, London

## 5  Parameters

1:     M – INTEGERInput
On entry: the number $m$ of data points.
Constraint: ${\mathbf{M}}\ge \mathit{mdist}\ge 2$, where $\mathit{mdist}$ is the number of distinct $x$ values in the data.
2:     KPLUS1 – INTEGERInput
On entry: $k+1$, where $k$ is the maximum degree required.
Constraint: $0<{\mathbf{KPLUS1}}\le \mathit{mdist}$, where $\mathit{mdist}$ is the number of distinct $x$ values in the data.
3:     LDA – INTEGERInput
On entry: the first dimension of the array A as declared in the (sub)program from which E02ADF is called.
Constraint: ${\mathbf{LDA}}\ge {\mathbf{KPLUS1}}$.
4:     X(M) – REAL (KIND=nag_wp) arrayInput
On entry: the values ${x}_{\mathit{r}}$ of the independent variable, for $\mathit{r}=1,2,\dots ,m$.
Constraint: the values must be supplied in nondecreasing order with ${\mathbf{X}}\left({\mathbf{M}}\right)>{\mathbf{X}}\left(1\right)$.
5:     Y(M) – REAL (KIND=nag_wp) arrayInput
On entry: the values ${y}_{\mathit{r}}$ of the dependent variable, for $\mathit{r}=1,2,\dots ,m$.
6:     W(M) – REAL (KIND=nag_wp) arrayInput
On entry: the set of weights, ${w}_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,m$. For advice on the choice of weights, see Section 2.1.2 in the E02 Chapter Introduction.
Constraint: ${\mathbf{W}}\left(\mathit{r}\right)>0.0$, for $\mathit{r}=1,2,\dots ,m$.
7:     WORK1($3×{\mathbf{M}}$) – REAL (KIND=nag_wp) arrayWorkspace
8:     WORK2($2×{\mathbf{KPLUS1}}$) – REAL (KIND=nag_wp) arrayWorkspace
9:     A(LDA,KPLUS1) – REAL (KIND=nag_wp) arrayOutput
On exit: the coefficients of ${T}_{\mathit{j}}\left(\stackrel{-}{x}\right)$ in the approximating polynomial of degree $\mathit{i}$. ${\mathbf{A}}\left(\mathit{i}+1,\mathit{j}+1\right)$ contains the coefficient ${a}_{\mathit{i}+1,\mathit{j}+1}$, for $\mathit{i}=0,1,\dots ,k$ and $\mathit{j}=0,1,\dots ,i$.
10:   S(KPLUS1) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{S}}\left(\mathit{i}+1\right)$ contains the root-mean-square residual ${s}_{\mathit{i}}$, for $\mathit{i}=0,1,\dots ,k$, as described in Section 3. For the interpretation of the values of the ${s}_{\mathit{i}}$ and their use in selecting an appropriate degree, see Section 3.1 in the E02 Chapter Introduction.
11:   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$
The weights are not all strictly positive.
${\mathbf{IFAIL}}=2$
The values of ${\mathbf{X}}\left(\mathit{r}\right)$, for $\mathit{r}=1,2,\dots ,{\mathbf{M}}$, are not in nondecreasing order.
${\mathbf{IFAIL}}=3$
All ${\mathbf{X}}\left(r\right)$ have the same value: thus the normalization of X is not possible.
${\mathbf{IFAIL}}=4$
 On entry, ${\mathbf{KPLUS1}}<1$ (so the maximum degree required is negative) or ${\mathbf{KPLUS1}}>\mathit{mdist}$, where $\mathit{mdist}$ is the number of distinct $x$ values in the data (so there cannot be a unique solution for degree $k={\mathbf{KPLUS1}}-1$).
${\mathbf{IFAIL}}=5$
${\mathbf{LDA}}<{\mathbf{KPLUS1}}$.

## 7  Accuracy

No error analysis for the method has been published. Practical experience with the method, however, is generally extremely satisfactory.

The time taken is approximately proportional to $m\left(k+1\right)\left(k+11\right)$.
The approximating polynomials may exhibit undesirable oscillations (particularly near the ends of the range) if the maximum degree $k$ exceeds a critical value which depends on the number of data points $m$ and their relative positions. As a rough guide, for equally-spaced data, this critical value is about $2×\sqrt{m}$. For further details see page 60 of Hayes (1970).

## 9  Example

Determine weighted least squares polynomial approximations of degrees $0$, $1$, $2$ and $3$ to a set of $11$ prescribed data points. For the approximation of degree $3$, tabulate the data and the corresponding values of the approximating polynomial, together with the residual errors, and also the values of the approximating polynomial at points half-way between each pair of adjacent data points.
The example program supplied is written in a general form that will enable polynomial approximations of degrees $0,1,\dots ,k$ to be obtained to $m$ data points, with arbitrary positive weights, and the approximation of degree $k$ to be tabulated. E02AEF is used to evaluate the approximating polynomial. The program is self-starting in that any number of datasets can be supplied.