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_fit_1dcheb_con (e02ag)

## Purpose

nag_fit_1dcheb_con (e02ag) computes constrained weighted least squares polynomial approximations in Chebyshev series form to an arbitrary set of data points. The values of the approximations and any number of their derivatives can be specified at selected points.

## Syntax

[a, s, np1, wrk, ifail] = e02ag(k, xmin, xmax, x, y, w, xf, yf, ip, lwrk, 'm', m, 'mf', mf)
[a, s, np1, wrk, ifail] = nag_fit_1dcheb_con(k, xmin, xmax, x, y, w, xf, yf, ip, lwrk, 'm', m, 'mf', mf)

## Description

nag_fit_1dcheb_con (e02ag) determines least squares polynomial approximations of degrees up to $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 value of $k$, the maximum degree required, is to be prescribed by you. At each of the values ${xf}_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,mf$, of the independent variable $x$, the approximations and their derivatives up to order ${p}_{\mathit{r}}$ are constrained to have one of the values ${yf}_{\mathit{s}}$, for $\mathit{s}=1,2,\dots ,\mathit{n}$, specified by you, where $\mathit{n}=mf+\sum _{r=0}^{mf}{p}_{r}$.
The approximation of degree $i$ has the property that, subject to the imposed constraints, it minimizes ${\sigma }_{i}$, the sum of the squares of the weighted residuals ${\epsilon }_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,m$, where
 $εr=wryr-fixr$
and ${f}_{i}\left({x}_{r}\right)$ is the value of the polynomial approximation 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$
where ${x}_{\mathrm{min}}$ and ${x}_{\mathrm{max}}$, specified by you, are respectively the lower and upper end points of the interval of $x$ over which the polynomials are to be defined.
The polynomial approximation of degree $i$ can be written as
 $12ai,0+ai,1T1x-+⋯+aijTjx-+⋯+aiiTix-$
where ${T}_{j}\left(\stackrel{-}{x}\right)$ is the Chebyshev polynomial of the first kind of degree $j$ with argument $\stackrel{-}{x}$. For $i=\mathit{n},\mathit{n}+1,\dots ,k$, the function produces the values of the coefficients ${a}_{i\mathit{j}}$, for $\mathit{j}=0,1,\dots ,i$, together with the value of the root mean square residual,
 $Si = σ i m ′ +n -i -1 ,$
where ${m}^{\prime }$ is the number of data points with nonzero weight.
Values of the approximations may subsequently be computed using nag_fit_1dcheb_eval (e02ae) or nag_fit_1dcheb_eval2 (e02ak).
First nag_fit_1dcheb_con (e02ag) determines a polynomial $\mu \left(\stackrel{-}{x}\right)$, of degree $\mathit{n}-1$, which satisfies the given constraints, and a polynomial $\nu \left(\stackrel{-}{x}\right)$, of degree $\mathit{n}$, which has value (or derivative) zero wherever a constrained value (or derivative) is specified. It then fits ${y}_{\mathit{r}}-\mu \left({x}_{\mathit{r}}\right)$, for $\mathit{r}=1,2,\dots ,m$, with polynomials of the required degree in $\stackrel{-}{x}$ each with factor $\nu \left(\stackrel{-}{x}\right)$. Finally the coefficients of $\mu \left(\stackrel{-}{x}\right)$ are added to the coefficients of these fits to give the coefficients of the constrained polynomial approximations to the data points $\left({x}_{\mathit{r}},{y}_{\mathit{r}}\right)$, for $\mathit{r}=1,2,\dots ,m$. The method employed is given in Hayes (1970): it is an extension of Forsythe's orthogonal polynomials method (see Forsythe (1957)) as modified by Clenshaw (see Clenshaw (1960)).

## References

Clenshaw C W (1960) Curve fitting with a digital computer Comput. J. 2 170–173
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
Hayes J G (ed.) (1970) Numerical Approximation to Functions and Data Athlone Press, London

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{k}$int64int32nag_int scalar
$k$, the maximum degree required.
Constraint: $\mathit{n}\le {\mathbf{k}}\le {m}^{\prime \prime }+\mathit{n}-1$ where $\mathit{n}$ is the total number of constraints and ${m}^{\prime \prime }$ is the number of data points with nonzero weights and distinct abscissae which do not coincide with any of the ${{\mathbf{xf}}}_{r}$.
2:     $\mathrm{xmin}$ – double scalar
3:     $\mathrm{xmax}$ – double scalar
The lower and upper end points, respectively, of the interval $\left[{x}_{\mathrm{min}},{x}_{\mathrm{max}}\right]$. Unless there are specific reasons to the contrary, it is recommended that xmin and xmax be set respectively to the lowest and highest value among the ${x}_{r}$ and ${xf}_{r}$. This avoids the danger of extrapolation provided there is a constraint point or data point with nonzero weight at each end point.
Constraint: ${\mathbf{xmax}}>{\mathbf{xmin}}$.
4:     $\mathrm{x}\left({\mathbf{m}}\right)$ – double array
${\mathbf{x}}\left(\mathit{r}\right)$ must contain the value ${x}_{\mathit{r}}$ of the independent variable at the $\mathit{r}$th data point, for $\mathit{r}=1,2,\dots ,m$.
Constraint: the ${\mathbf{x}}\left(r\right)$ must be in nondecreasing order and satisfy ${\mathbf{xmin}}\le {\mathbf{x}}\left(r\right)\le {\mathbf{xmax}}$.
5:     $\mathrm{y}\left({\mathbf{m}}\right)$ – double array
${\mathbf{y}}\left(\mathit{r}\right)$ must contain ${y}_{\mathit{r}}$, the value of the dependent variable at the $\mathit{r}$th data point, for $\mathit{r}=1,2,\dots ,m$.
6:     $\mathrm{w}\left({\mathbf{m}}\right)$ – double array
${\mathbf{w}}\left(\mathit{r}\right)$ must contain the weight ${w}_{\mathit{r}}$ to be applied to the data point ${x}_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,m$. For advice on the choice of weights see the E02 Chapter Introduction. Negative weights are treated as positive. A zero weight causes the corresponding data point to be ignored. Zero weight should be given to any data point whose $x$ and $y$ values both coincide with those of a constraint (otherwise the denominators involved in the root mean square residuals ${S}_{i}$ will be slightly in error).
7:     $\mathrm{xf}\left({\mathbf{mf}}\right)$ – double array
${\mathbf{xf}}\left(\mathit{r}\right)$ must contain ${xf}_{\mathit{r}}$, the value of the independent variable at which a constraint is specified, for $\mathit{r}=1,2,\dots ,{\mathbf{mf}}$.
Constraint: these values need not be ordered but must be distinct and satisfy ${\mathbf{xmin}}\le {\mathbf{xf}}\left(r\right)\le {\mathbf{xmax}}$.
8:     $\mathrm{yf}\left(\mathit{lyf}\right)$ – double array
lyf, the dimension of the array, must satisfy the constraint $\mathit{lyf}\ge {\mathbf{mf}}+\sum _{\mathit{i}=1}^{{\mathbf{mf}}}{\mathbf{ip}}\left(\mathit{i}\right)$.
The values which the approximating polynomials and their derivatives are required to take at the points specified in xf. For each value of ${\mathbf{xf}}\left(\mathit{r}\right)$, yf contains in successive elements the required value of the approximation, its first derivative, second derivative, $\dots ,{p}_{\mathit{r}}$th derivative, for $\mathit{r}=1,2,\dots ,mf$. Thus the value, ${yf}_{s}$, which the $k$th derivative of each approximation ($k=0$ referring to the approximation itself) is required to take at the point ${\mathbf{xf}}\left(r\right)$ must be contained in ${\mathbf{yf}}\left(s\right)$, where
 $s=r+k+p1+p2+⋯+pr-1,$
where $k=0,1,\dots ,{p}_{r}$ and $r=1,2,\dots ,mf$. The derivatives are with respect to the independent variable $x$.
9:     $\mathrm{ip}\left({\mathbf{mf}}\right)$int64int32nag_int array
${\mathbf{ip}}\left(\mathit{r}\right)$ must contain ${p}_{\mathit{r}}$, the order of the highest-order derivative specified at ${\mathbf{xf}}\left(\mathit{r}\right)$, for $\mathit{r}=1,2,\dots ,mf$. ${p}_{r}=0$ implies that the value of the approximation at ${\mathbf{xf}}\left(r\right)$ is specified, but not that of any derivative.
Constraint: ${\mathbf{ip}}\left(\mathit{r}\right)\ge 0$, for $\mathit{r}=1,2,\dots ,{\mathbf{mf}}$.
10:   $\mathrm{lwrk}$int64int32nag_int scalar
The dimension of the array wrk.
Constraint: ${\mathbf{lwrk}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(4×{\mathbf{m}}+3×\mathit{kplus1},8×\mathit{n}+5×\mathit{ipmax}+{\mathbf{mf}}+10\right)+2×\mathit{n}+2$, where $\mathit{ipmax}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{ip}}\left(\mathit{r}\right)\right)$, for $\mathit{r}=1,2,\dots ,mf$.

### Optional Input Parameters

1:     $\mathrm{m}$int64int32nag_int scalar
Default: the dimension of the arrays x, y, w. (An error is raised if these dimensions are not equal.)
$m$, the number of data points to be fitted.
Constraint: ${\mathbf{m}}\ge 1$.
2:     $\mathrm{mf}$int64int32nag_int scalar
Default: the dimension of the arrays xf, ip. (An error is raised if these dimensions are not equal.)
$mf$, the number of values of the independent variable at which a constraint is specified.
Constraint: ${\mathbf{mf}}\ge 1$.

### Output Parameters

1:     $\mathrm{a}\left(\mathit{lda},\mathit{kplus1}\right)$ – double array
$\mathit{kplus1}={\mathbf{k}}+1$.
${\mathbf{a}}\left(\mathit{i}+1,\mathit{j}+1\right)$ contains the coefficient ${a}_{\mathit{i}\mathit{j}}$ in the approximating polynomial of degree $\mathit{i}$, for $\mathit{i}=\mathit{n},\dots ,k$ and $\mathit{j}=0,1,\dots ,\mathit{i}$.
2:     $\mathrm{s}\left(\mathit{kplus1}\right)$ – double array
$\mathit{kplus1}={\mathbf{k}}+1$.
${\mathbf{s}}\left(\mathit{i}+1\right)$ contains ${S}_{\mathit{i}}$, for $\mathit{i}=\mathit{n},\dots ,k$, the root mean square residual corresponding to the approximating polynomial of degree $i$. In the case where the number of data points with nonzero weight is equal to $k+1-\mathit{n}$, ${S}_{i}$ is indeterminate: the function sets it to zero. For the interpretation of the values of ${S}_{i}$ and their use in selecting an appropriate degree, see General in the E02 Chapter Introduction.
3:     $\mathrm{np1}$int64int32nag_int scalar
$\mathit{n}+1$, where $\mathit{n}$ is the total number of constraint conditions imposed: $\mathit{n}=mf+{p}_{1}+{p}_{2}+\cdots +{p}_{mf}$.
4:     $\mathrm{wrk}\left({\mathbf{lwrk}}\right)$ – double array
Contains weighted residuals of the highest degree of fit determined $\left(k\right)$. The residual at ${x}_{\mathit{r}}$ is in element $2\left(\mathit{n}+1\right)+3\left(m+\mathit{k}+1\right)+\mathit{r}$, for $\mathit{r}=1,2,\dots ,m$. The rest of the array is used as workspace.
5:     $\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$
 On entry, ${\mathbf{m}}<1$, or $\mathit{kplus1}<\mathit{n}+1$, or $\mathit{lda}<\mathit{kplus1}$, or ${\mathbf{mf}}<1$, or $\mathit{lyf}<\mathit{n}$, or lwrk is too small (see Arguments), or $\mathit{liwrk}<2×{\mathbf{mf}}+2$.
(Here $\mathit{n}$ is the total number of constraint conditions.)
${\mathbf{ifail}}=2$
${\mathbf{ip}}\left(r\right)<0$ for some $r=1,2,\dots ,{\mathbf{mf}}$.
${\mathbf{ifail}}=3$
${\mathbf{xmin}}\ge {\mathbf{xmax}}$, or ${\mathbf{xf}}\left(r\right)$ is not in the interval xmin to xmax for some $r=1,2,\dots ,{\mathbf{mf}}$, or the ${\mathbf{xf}}\left(r\right)$ are not distinct.
${\mathbf{ifail}}=4$
${\mathbf{x}}\left(r\right)$ is not in the interval xmin to xmax for some $r=1,2,\dots ,{\mathbf{m}}$.
${\mathbf{ifail}}=5$
${\mathbf{x}}\left(r\right)<{\mathbf{x}}\left(r-1\right)$ for some $r=2,3,\dots ,{\mathbf{m}}$.
${\mathbf{ifail}}=6$
$\mathit{kplus1}>{m}^{\prime \prime }+\mathit{n}$, where ${m}^{\prime \prime }$ is the number of data points with nonzero weight and distinct abscissae which do not coincide with any ${\mathbf{xf}}\left(r\right)$. Thus there is no unique solution.
${\mathbf{ifail}}=7$
The polynomials $\mu \left(x\right)$ and/or $\nu \left(x\right)$ cannot be determined. The problem supplied is too ill-conditioned. This may occur when the constraint points are very close together, or large in number, or when an attempt is made to constrain high-order derivatives.
${\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

No complete error analysis exists for either the interpolating algorithm or the approximating algorithm. However, considerable experience with the approximating algorithm shows that it is generally extremely satisfactory. Also the moderate number of constraints, of low-order, which are typical of data fitting applications, are unlikely to cause difficulty with the interpolating function.

The time taken to form the interpolating polynomial is approximately proportional to ${\mathit{n}}^{3}$, and that to form the approximating polynomials is very approximately proportional to $m\left(k+1\right)\left(k+1-\mathit{n}\right)$.
To carry out a least squares polynomial fit without constraints, use nag_fit_1dcheb_arb (e02ad). To carry out polynomial interpolation only, use nag_interp_1d_cheb (e01ae).

## Example

This example reads data in the following order, using the notation of the argument list above:
• mf
• ${\mathbf{ip}}\left(\mathit{i}\right)$, ${\mathbf{xf}}\left(\mathit{i}\right)$, Y-value and derivative values (if any) at ${\mathbf{xf}}\left(\mathit{i}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{mf}}$
• m
• ${\mathbf{x}}\left(\mathit{i}\right)$, ${\mathbf{y}}\left(\mathit{i}\right)$, ${\mathbf{w}}\left(\mathit{i}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{m}}$
• $k$, xmin, xmax
The output is:
• the root mean square residual for each degree from $\mathit{n}$ to $k$;
• the Chebyshev coefficients for the fit of degree $k$;
• the data points, and the fitted values and residuals for the fit of degree $k$.
The program is written in a generalized form which will read any number of datasets.
The dataset supplied specifies $5$ data points in the interval $\left[0.0,4.0\right]$ with unit weights, to which are to be fitted polynomials, $p$, of degrees up to $4$, subject to the $3$ constraints:
• $p\left(0.0\right)=1.0\text{, }{p}^{\prime }\left(0.0\right)=-2.0\text{, }p\left(4.0\right)=9.0\text{.}$
```function e02ag_example

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

% Data to be fitted
n = 5;
x = [0.5;   1;     2;  2.5; 3];
y = [0.03; -0.75; -1; -0.1; 1.75];
w = [1;     1;     1;  1;   1];

% constrained values at two points x=0, 4.
% y(0) = 1, dy/dx(0) = -2; y(4) = 9.
ip = int64([1;0]);
xf = [0;      4];
yf = [1; -2;  9];
% degree and range of polynomial.
k = int64(4);
xmin = 0;
xmax = 4;

% Compute least squares fit for poynomial of degree at most k
lwrk = int64(200);
[a, s, np1, wrk, ifail] = e02ag(...
k, xmin, xmax, x, y, w, xf, yf, ip, lwrk);

fprintf('Degree      residual\n');
lyf = 3;
for i=lyf:k
fprintf('%5d%15.2e\n',i,s(i+1));
end
p = a(k+1,:);

fprintf('\n     i      x(i)       y(i)       Fit     Residual\n');
for i = 1:n
[fiti, ifail] = e02ak(k, xmin, xmax, p, int64(1), x(i));
fprintf('%6d%11.4f%11.4f%11.4f%11.2e\n',...
i, x(i), y(i), fiti, fiti-y(i));
end

```
```e02ag example results

Degree      residual
3       2.55e-03
4       2.94e-03

i      x(i)       y(i)       Fit     Residual
1     0.5000     0.0300     0.0310   1.02e-03
2     1.0000    -0.7500    -0.7508  -7.81e-04
3     2.0000    -1.0000    -1.0020  -2.00e-03
4     2.5000    -0.1000    -0.0961   3.95e-03
5     3.0000     1.7500     1.7478  -2.17e-03
```