NAG Library Routine Document
E02AGF
1 Purpose
E02AGF 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.
2 Specification
SUBROUTINE E02AGF ( 
M, KPLUS1, LDA, XMIN, XMAX, X, Y, W, MF, XF, YF, LYF, IP, A, S, NP1, WRK, LWRK, IWRK, LIWRK, IFAIL) 
INTEGER 
M, KPLUS1, LDA, MF, LYF, IP(MF), NP1, LWRK, IWRK(LIWRK), LIWRK, IFAIL 
REAL (KIND=nag_wp) 
XMIN, XMAX, X(M), Y(M), W(M), XF(MF), YF(LYF), A(LDA,KPLUS1), S(KPLUS1), WRK(LWRK) 

3 Description
E02AGF 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+{\displaystyle \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
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
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
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 routine 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,
where
${m}^{\prime}$ is the number of data points with nonzero weight.
Values of the approximations may subsequently be computed using
E02AEF or
E02AKF.
First E02AGF 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)).
4 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
5 Parameters
 1: M – INTEGERInput
On entry: $m$, the number of data points to be fitted.
Constraint:
${\mathbf{M}}\ge 1$.
 2: KPLUS1 – INTEGERInput
On entry: $k+1$, where $k$ is the maximum degree required.
Constraint:
$\mathit{n}+1\le {\mathbf{KPLUS1}}\le {m}^{\prime \prime}+\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}}\left(r\right)$.
 3: LDA – INTEGERInput
On entry: the first dimension of the array
A as declared in the (sub)program from which E02AGF is called.
Constraint:
${\mathbf{LDA}}\ge {\mathbf{KPLUS1}}$.
 4: XMIN – REAL (KIND=nag_wp)Input
 5: XMAX – REAL (KIND=nag_wp)Input
On entry: 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}}$.
 6: X(M) – REAL (KIND=nag_wp) arrayInput
On entry: ${\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}}$.
 7: Y(M) – REAL (KIND=nag_wp) arrayInput
On entry: ${\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$.
 8: W(M) – REAL (KIND=nag_wp) arrayInput
On entry:
${\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).
 9: MF – INTEGERInput
On entry: $mf$, the number of values of the independent variable at which a constraint is specified.
Constraint:
${\mathbf{MF}}\ge 1$.
 10: XF(MF) – REAL (KIND=nag_wp) arrayInput
On entry: ${\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}}$.
 11: YF(LYF) – REAL (KIND=nag_wp) arrayInput
On entry: 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
where
$k=0,1,\dots ,{p}_{r}$ and
$r=1,2,\dots ,mf$. The derivatives are with respect to the independent variable
$x$.
 12: LYF – INTEGERInput
On entry: the dimension of the array
YF as declared in the (sub)program from which E02AGF is called.
Constraint:
${\mathbf{LYF}}\ge {\mathbf{MF}}+{\displaystyle \sum _{\mathit{i}=1}^{{\mathbf{MF}}}}{\mathbf{IP}}\left(\mathit{i}\right)$.
 13: IP(MF) – INTEGER arrayInput
On entry: ${\mathbf{IP}}\left(\mathit{r}\right)$ must contain ${p}_{\mathit{r}}$, the order of the highestorder 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}}$.
 14: A(LDA,KPLUS1) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\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}$.
 15: S(KPLUS1) – REAL (KIND=nag_wp) arrayOutput
On exit:
${\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 routine sets it to zero. For the interpretation of the values of
${S}_{i}$ and their use in selecting an appropriate degree, see
Section 3.1 in the E02 Chapter Introduction.
 16: NP1 – INTEGEROutput
On exit: $\mathit{n}+1$, where $\mathit{n}$ is the total number of constraint conditions imposed: $\mathit{n}=mf+{p}_{1}+{p}_{2}+\cdots +{p}_{mf}$.
 17: WRK(LWRK) – REAL (KIND=nag_wp) arrayOutput
On exit: 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.
 18: LWRK – INTEGERInput
On entry: the dimension of the array
WRK as declared in the (sub)program from which E02AGF is called.
Constraint:
${\mathbf{LWRK}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(4\times {\mathbf{M}}+3\times {\mathbf{KPLUS1}},8\times \mathit{n}+5\times \mathit{ipmax}+{\mathbf{MF}}+10\right)+2\times \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$.
 19: IWRK(LIWRK) – INTEGER arrayWorkspace
 20: LIWRK – INTEGERInput
On entry: the dimension of the array
IWRK as declared in the (sub)program from which E02AGF is called.
Constraint:
${\mathbf{LIWRK}}\ge 2\times {\mathbf{MF}}+2$.
 21: 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$
On entry,  ${\mathbf{M}}<1$, 
or  ${\mathbf{KPLUS1}}<\mathit{n}+1$, 
or  ${\mathbf{LDA}}<{\mathbf{KPLUS1}}$, 
or  ${\mathbf{MF}}<1$, 
or  ${\mathbf{LYF}}<\mathit{n}$, 
or  LWRK is too small (see Section 5), 
or  ${\mathbf{LIWRK}}<2\times {\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(r1\right)$ for some $r=2,3,\dots ,{\mathbf{M}}$.
 ${\mathbf{IFAIL}}=6$
${\mathbf{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 illconditioned. This may occur when the constraint points are very close together, or large in number, or when an attempt is made to constrain highorder derivatives.
7 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 loworder, which are typical of data fitting applications, are unlikely to cause difficulty with the interpolating routine.
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
E02ADF. To carry out polynomial interpolation only, use
E01AEF.
9 Example
This example reads data in the following order, using the notation of the parameter list above:
 MF
 ${\mathbf{IP}}\left(\mathit{i}\right)$,
${\mathbf{XF}}\left(\mathit{i}\right)$,
Yvalue 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{, \hspace{1em}}{p}^{\prime}\left(0.0\right)=2.0\text{, \hspace{1em}}p\left(4.0\right)=9.0\text{.}$
9.1 Program Text
Program Text (e02agfe.f90)
9.2 Program Data
Program Data (e02agfe.d)
9.3 Program Results
Program Results (e02agfe.r)