nag_1d_cheb_fit_constr (e02agc) (PDF version)
e02 Chapter Contents
e02 Chapter Introduction
NAG Library Manual

NAG Library Function Document

nag_1d_cheb_fit_constr (e02agc)

 Contents

    1  Purpose
    7  Accuracy

1  Purpose

nag_1d_cheb_fit_constr (e02agc) 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

#include <nag.h>
#include <nage02.h>
void  nag_1d_cheb_fit_constr (Nag_OrderType order, Integer m, Integer k, double xmin, double xmax, const double x[], const double y[], const double w[], Integer mf, const double xf[], const double yf[], const Integer p[], double a[], double s[], Integer *n, double resid[], NagError *fail)

3  Description

nag_1d_cheb_fit_constr (e02agc) determines least squares polynomial approximations of degrees up to k to the set of data points xr,yr with weights wr, for r=1,2,,m. The value of k, the maximum degree required, is to be prescribed by you. At each of the values xfr, for r=1,2,,mf, of the independent variable x, the approximations and their derivatives up to order pr are constrained to have one of the values yfs, for s=1,2,,n, specified by you, where n=mf+r=0mfpr.
The approximation of degree i has the property that, subject to the imposed constraints, it minimizes σi, the sum of the squares of the weighted residuals εr, for r=1,2,,m, where
εr=wryr-fixr  
and fixr is the value of the polynomial approximation of degree i at the rth data point.
Each polynomial is represented in Chebyshev series form with normalized argument 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 xmin and xmax, 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 Tjx- is the Chebyshev polynomial of the first kind of degree j with argument x-. For i=n,n+1,,k, the function produces the values of the coefficients aij, for j=0,1,,i, together with the value of the root mean square residual,
Si = σ i m +n -i -1 ,  
where m is the number of data points with nonzero weight.
Values of the approximations may subsequently be computed using nag_1d_cheb_eval (e02aec) or nag_1d_cheb_eval2 (e02akc).
First nag_1d_cheb_fit_constr (e02agc) determines a polynomial μx-, of degree n-1, which satisfies the given constraints, and a polynomial νx-, of degree n, which has value (or derivative) zero wherever a constrained value (or derivative) is specified. It then fits yr-μxr, for r=1,2,,m, with polynomials of the required degree in x- each with factor νx-. Finally the coefficients of μx- are added to the coefficients of these fits to give the coefficients of the constrained polynomial approximations to the data points xr,yr, for r=1,2,,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  Arguments

1:     order Nag_OrderTypeInput
On entry: the order argument specifies the two-dimensional storage scheme being used, i.e., row-major ordering or column-major ordering. C language defined storage is specified by order=Nag_RowMajor. See Section 3.2.1.3 in the Essential Introduction for a more detailed explanation of the use of this argument.
Constraint: order=Nag_RowMajor or Nag_ColMajor.
2:     m IntegerInput
On entry: m, the number of data points to be fitted.
Constraint: m1.
3:     k IntegerInput
On entry: k, the maximum degree required.
Constraint: nkm+n-1 where n is the total number of constraints and m is the number of data points with nonzero weights and distinct abscissae which do not coincide with any of the xfr.
4:     xmin doubleInput
5:     xmax doubleInput
On entry: the lower and upper end points, respectively, of the interval xmin,xmax. 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 xr and xfr. This avoids the danger of extrapolation provided there is a constraint point or data point with nonzero weight at each end point.
Constraint: xmax>xmin.
6:     x[m] const doubleInput
On entry: x[r-1] must contain the value xr of the independent variable at the rth data point, for r=1,2,,m.
Constraint: the x[r-1] must be in nondecreasing order and satisfy xminx[r-1]xmax.
7:     y[m] const doubleInput
On entry: y[r-1] must contain yr, the value of the dependent variable at the rth data point, for r=1,2,,m.
8:     w[m] const doubleInput
On entry: w[r-1] must contain the weight wr to be applied to the data point xr, for r=1,2,,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 Si 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: mf1.
10:   xf[mf] const doubleInput
On entry: xf[r-1] must contain xfr, the value of the independent variable at which a constraint is specified, for r=1,2,,mf.
Constraint: these values need not be ordered but must be distinct and satisfy xminxf[r-1]xmax.
11:   yf[dim] const doubleInput
Note: the dimension, dim, of the array yf must be at least mf+ i=0 mf-1 p[i].
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 xf[r-1], yf contains in successive elements the required value of the approximation, its first derivative, second derivative, ,prth derivative, for r=1,2,,mf. Thus the value, yfs, which the kth derivative of each approximation (k=0 referring to the approximation itself) is required to take at the point xf[r-1] must be contained in yf[s-1], where
s=r+k+p1+p2++pr-1,  
where k=0,1,,pr and r=1,2,,mf. The derivatives are with respect to the independent variable x.
12:   p[mf] const IntegerInput
On entry: p[r-1] must contain pr, the order of the highest-order derivative specified at xf[r-1], for r=1,2,,mf. pr=0 implies that the value of the approximation at xf[r-1] is specified, but not that of any derivative.
Constraint: p[r-1]0, for r=1,2,,mf.
13:   a[dim] doubleOutput
Note: the dimension, dim, of the array a must be at least k+1×k+1.
Where Ai,j appears in this document, it refers to the array element
  • a[j-1×k+1+i-1] when order=Nag_ColMajor;
  • a[i-1×k+1+j-1] when order=Nag_RowMajor.
On exit: Ai+1,j+1 contains the coefficient aij in the approximating polynomial of degree i, for i=n,,k and j=0,1,,i.
14:   s[k+1] doubleOutput
On exit: s[i] contains Si, for i=n,,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-n, Si is indeterminate: the function sets it to zero. For the interpretation of the values of Si and their use in selecting an appropriate degree, see Section 3.1 in the e02 Chapter Introduction.
15:   n Integer *Output
On exit: contains the total number of constraint conditions imposed: n=mf+p1+p2++pmf.
16:   resid[m] doubleOutput
On exit: contains weighted residuals of the highest degree of fit determined k. The residual at xr is in element resid[r-1], for r=1,2,,m.
17:   fail NagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

6  Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.2.1.2 in the Essential Introduction for further information.
NE_BAD_PARAM
On entry, argument value had an illegal value.
NE_CONSTRAINT
On entry, k=value and =value.
Constraint: nkm+n-1 where n is the total number of constraints and m is the number of data points with nonzero weights and distinct abscissae which do not coincide with any of the xfr.
NE_ILL_CONDITIONED
The polynomials mux and/or nux cannot be found. The problem is too ill-conditioned.
NE_INT
On entry, m=value.
Constraint: m1.
On entry, mf=value.
Constraint: mf1.
NE_INT_3
On entry, k+1>m+n, where m is the number of data points with nonzero weight and distinct abscissae different from all xf, and n is the total number of constraints: k+1=value, m=value and n=value.
NE_INT_ARRAY
On entry, p[value]=value.
Constraint: p[r-1]0, for r=1,2,,mf.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
An unexpected error has been triggered by this function. Please contact NAG.
See Section 3.6.6 in the Essential Introduction for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 3.6.5 in the Essential Introduction for further information.
NE_NOT_MONOTONIC
On entry, i=value, x[i-1]=value and x[i-2]=value.
Constraint: x[i-1]x[i-2].
NE_REAL_2
On entry, xmin=value and xmax=value.
Constraint: xmin<xmax.
NE_REAL_ARRAY
On entry, I=value, xf[I-1]=value, J=value and xf[J-1]=value.
Constraint: xf[I-1]xf[J-1].
On entry, xf[I-1] lies outside interval xmin,xmax: I=value, xf[I-1]=value, xmin=value and xmax=value.
On entry, x[I-1] lies outside interval xmin,xmax: I=value, x[I-1]=value, xmin=value and xmax=value.
On entry, x[I-1] lies outside interval xmin,xmax for some I.

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 low-order, which are typical of data fitting applications, are unlikely to cause difficulty with the interpolating function.

8  Parallelism and Performance

Not applicable.

9  Further Comments

The time taken to form the interpolating polynomial is approximately proportional to n3, and that to form the approximating polynomials is very approximately proportional to mk+1k+1-n.
To carry out a least squares polynomial fit without constraints, use nag_1d_cheb_fit (e02adc). To carry out polynomial interpolation only, use nag_1d_cheb_interp (e01aec).

10  Example

This example reads data in the following order, using the notation of the argument list above:
The output is:
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 0.0,4.0 with unit weights, to which are to be fitted polynomials, p, of degrees up to 4, subject to the 3 constraints:

10.1  Program Text

Program Text (e02agce.c)

10.2  Program Data

Program Data (e02agce.d)

10.3  Program Results

Program Results (e02agce.r)

GnuplotProduced by GNUPLOT 4.6 patchlevel 3 −2 −1.5 −1 −0.5 0 0.5 1 1.5 2 0.5 1 1.5 2 2.5 3 −0.004 −0.002 0 0.002 0.004 Polynomial Fit P(x) Residual P(xi)yi x Example Program Constrained Least-squares Polynomial Approximation residual polynomial fit gnuplot_plot_1 data points gnuplot_plot_2 gnuplot_plot_3

nag_1d_cheb_fit_constr (e02agc) (PDF version)
e02 Chapter Contents
e02 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2015