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

## Purpose

nag_fit_1dcheb_arb (e02ad) computes weighted least squares polynomial approximations to an arbitrary set of data points.

## Syntax

[a, s, ifail] = e02ad(kplus1, x, y, w, 'm', m)
[a, s, ifail] = nag_fit_1dcheb_arb(kplus1, x, y, w, 'm', m)

## Description

nag_fit_1dcheb_arb (e02ad) determines least squares polynomial approximations of degrees 0,1,,k$0,1,\dots ,k$ to the set of data points (xr,yr)$\left({x}_{\mathit{r}},{y}_{\mathit{r}}\right)$ with weights wr${w}_{\mathit{r}}$, for r = 1,2,,m$\mathit{r}=1,2,\dots ,m$.
The approximation of degree i$i$ has the property that it minimizes σi${\sigma }_{i}$ the sum of squares of the weighted residuals εr${\epsilon }_{r}$, where
 εr = wr(yr − fr) $εr=wr(yr-fr)$
and fr${f}_{r}$ is the value of the polynomial of degree i$i$ at the r$r$th data point.
Each polynomial is represented in Chebyshev series form with normalized argument x$\stackrel{-}{x}$. This argument lies in the range 1$-1$ to + 1$+1$ and is related to the original variable x$x$ by the linear transformation
 x = ((2x − xmax − xmin))/((xmax − xmin)). $x-= (2x-xmax-xmin) (xmax-xmin) .$
Here xmax${x}_{\mathrm{max}}$ and xmin${x}_{\mathrm{min}}$ are respectively the largest and smallest values of xr${x}_{r}$. The polynomial approximation of degree i$i$ is represented as
 (1/2)ai + 1,1T0(x) + ai + 1,2T1(x) + ai + 1,3T2(x) + ⋯ + ai + 1,i + 1Ti(x), $12ai+1,1T0(x-)+ai+1,2T1(x-)+ai+1,3T2(x-)+⋯+ai+1,i+1Ti(x-),$
where Tj(x)${T}_{\mathit{j}}\left(\stackrel{-}{x}\right)$, for j = 0,1,,i$\mathit{j}=0,1,\dots ,i$, are the Chebyshev polynomials of the first kind of degree j$j$ with argument (x)$\left(\stackrel{-}{x}\right)$.
For i = 0,1,,k$i=0,1,\dots ,k$, the function produces the values of ai + 1,j + 1${a}_{i+1,\mathit{j}+1}$, for j = 0,1,,i$\mathit{j}=0,1,\dots ,i$, together with the value of the root-mean-square residual si = sqrt(σi / (mi1))${s}_{i}=\sqrt{{\sigma }_{i}/\left(m-i-1\right)}$. In the case m = i + 1$m=i+1$ the function sets the value of si${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 nag_fit_1dcheb_eval (e02ae).

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

## Parameters

### Compulsory Input Parameters

1:     kplus1 – int64int32nag_int scalar
k + 1$k+1$, where k$k$ is the maximum degree required.
Constraint: 0 < kplus1mdist$0<{\mathbf{kplus1}}\le \mathit{mdist}$, where mdist$\mathit{mdist}$ is the number of distinct x$x$ values in the data.
2:     x(m) – double array
m, the dimension of the array, must satisfy the constraint mmdist2${\mathbf{m}}\ge \mathit{mdist}\ge 2$, where mdist$\mathit{mdist}$ is the number of distinct x$x$ values in the data.
The values xr${x}_{\mathit{r}}$ of the independent variable, for r = 1,2,,m$\mathit{r}=1,2,\dots ,m$.
Constraint: the values must be supplied in nondecreasing order with x(m) > x(1)${\mathbf{x}}\left({\mathbf{m}}\right)>{\mathbf{x}}\left(1\right)$.
3:     y(m) – double array
m, the dimension of the array, must satisfy the constraint mmdist2${\mathbf{m}}\ge \mathit{mdist}\ge 2$, where mdist$\mathit{mdist}$ is the number of distinct x$x$ values in the data.
The values yr${y}_{\mathit{r}}$ of the dependent variable, for r = 1,2,,m$\mathit{r}=1,2,\dots ,m$.
4:     w(m) – double array
m, the dimension of the array, must satisfy the constraint mmdist2${\mathbf{m}}\ge \mathit{mdist}\ge 2$, where mdist$\mathit{mdist}$ is the number of distinct x$x$ values in the data.
The set of weights, wr${w}_{\mathit{r}}$, for r = 1,2,,m$\mathit{r}=1,2,\dots ,m$. For advice on the choice of weights, see Section [Weighting of data points] in the E02 Chapter Introduction.
Constraint: w(r) > 0.0${\mathbf{w}}\left(\mathit{r}\right)>0.0$, for r = 1,2,,m$\mathit{r}=1,2,\dots ,m$.

### Optional Input Parameters

1:     m – int64int32nag_int scalar
Default: The dimension of the arrays x, y, w. (An error is raised if these dimensions are not equal.)
The number m$m$ of data points.
Constraint: mmdist2${\mathbf{m}}\ge \mathit{mdist}\ge 2$, where mdist$\mathit{mdist}$ is the number of distinct x$x$ values in the data.

lda work1 work2

### Output Parameters

1:     a(lda,kplus1) – double array
ldakplus1$\mathit{lda}\ge {\mathbf{kplus1}}$.
The coefficients of Tj(x)${T}_{\mathit{j}}\left(\stackrel{-}{x}\right)$ in the approximating polynomial of degree i$\mathit{i}$. a(i + 1,j + 1)${\mathbf{a}}\left(\mathit{i}+1,\mathit{j}+1\right)$ contains the coefficient ai + 1,j + 1${a}_{\mathit{i}+1,\mathit{j}+1}$, for i = 0,1,,k$\mathit{i}=0,1,\dots ,k$ and j = 0,1,,i$\mathit{j}=0,1,\dots ,i$.
2:     s(kplus1) – double array
s(i + 1)${\mathbf{s}}\left(\mathit{i}+1\right)$ contains the root-mean-square residual si${s}_{\mathit{i}}$, for i = 0,1,,k$\mathit{i}=0,1,\dots ,k$, as described in Section [Description]. For the interpretation of the values of the si${s}_{\mathit{i}}$ and their use in selecting an appropriate degree, see Section [General] in the E02 Chapter Introduction.
3:     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:
ifail = 1${\mathbf{ifail}}=1$
The weights are not all strictly positive.
ifail = 2${\mathbf{ifail}}=2$
The values of x(r)${\mathbf{x}}\left(\mathit{r}\right)$, for r = 1,2,,m$\mathit{r}=1,2,\dots ,{\mathbf{m}}$, are not in nondecreasing order.
ifail = 3${\mathbf{ifail}}=3$
All x(r)${\mathbf{x}}\left(r\right)$ have the same value: thus the normalization of x is not possible.
ifail = 4${\mathbf{ifail}}=4$
 On entry, kplus1 < 1${\mathbf{kplus1}}<1$ (so the maximum degree required is negative) or kplus1 > mdist${\mathbf{kplus1}}>\mathit{mdist}$, where mdist$\mathit{mdist}$ is the number of distinct x$x$ values in the data (so there cannot be a unique solution for degree k = kplus1 − 1$k={\mathbf{kplus1}}-1$).
ifail = 5${\mathbf{ifail}}=5$
lda < kplus1$\mathit{lda}<{\mathbf{kplus1}}$.

## 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(k + 1)(k + 11)$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$k$ exceeds a critical value which depends on the number of data points m$m$ and their relative positions. As a rough guide, for equally-spaced data, this critical value is about 2 × sqrt(m)$2×\sqrt{m}$. For further details see page 60 of Hayes (1970).

## Example

```function nag_fit_1dcheb_arb_example
kplus1 = int64(4);
x = [1;
2.1;
3.1;
3.9;
4.9;
5.8;
6.5;
7.1;
7.8;
8.4;
9];
y = [10.4;
7.9;
4.7;
2.5;
1.2;
2.2;
5.1;
9.2;
16.1;
24.5;
35.3];
w = [1;
1;
1;
1;
1;
0.8;
0.8;
0.7;
0.5;
0.3;
0.2];
[a, s, ifail] = nag_fit_1dcheb_arb(kplus1, x, y, w)
```
```

a =

12.1740         0         0         0
12.2954    0.2740         0         0
20.7345    6.2016    8.1876         0
24.1429    9.4065   10.8400    3.0589

s =

4.0659
4.2840
1.6865
0.0682

ifail =

0

```
```function e02ad_example
kplus1 = int64(4);
x = [1;
2.1;
3.1;
3.9;
4.9;
5.8;
6.5;
7.1;
7.8;
8.4;
9];
y = [10.4;
7.9;
4.7;
2.5;
1.2;
2.2;
5.1;
9.2;
16.1;
24.5;
35.3];
w = [1;
1;
1;
1;
1;
0.8;
0.8;
0.7;
0.5;
0.3;
0.2];
[a, s, ifail] = e02ad(kplus1, x, y, w)
```
```

a =

12.1740         0         0         0
12.2954    0.2740         0         0
20.7345    6.2016    8.1876         0
24.1429    9.4065   10.8400    3.0589

s =

4.0659
4.2840
1.6865
0.0682

ifail =

0

```