hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_fit_1dcheb_con (e02ag)


    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example


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.


[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)


nag_fit_1dcheb_con (e02ag) 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
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
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_fit_1dcheb_eval (e02ae) or nag_fit_1dcheb_eval2 (e02ak).
First nag_fit_1dcheb_con (e02ag) 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)).


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


Compulsory Input Parameters

1:     k int64int32nag_int scalar
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.
2:     xmin – double scalar
3:     xmax – double scalar
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.
4:     xm – double array
xr must contain the value xr of the independent variable at the rth data point, for r=1,2,,m.
Constraint: the xr must be in nondecreasing order and satisfy xminxrxmax.
5:     ym – double array
yr must contain yr, the value of the dependent variable at the rth data point, for r=1,2,,m.
6:     wm – double array
wr 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).
7:     xfmf – double array
xfr 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 xminxfrxmax.
8:     yflyf – double array
lyf, the dimension of the array, must satisfy the constraint lyfmf+ i=1 mf ipi.
The values which the approximating polynomials and their derivatives are required to take at the points specified in xf. For each value of xfr, 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 xfr must be contained in yfs, where
where k=0,1,,pr and r=1,2,,mf. The derivatives are with respect to the independent variable x.
9:     ipmf int64int32nag_int array
ipr must contain pr, the order of the highest-order derivative specified at xfr, for r=1,2,,mf. pr=0 implies that the value of the approximation at xfr is specified, but not that of any derivative.
Constraint: ipr0, for r=1,2,,mf.
10:   lwrk int64int32nag_int scalar
The dimension of the array wrk.
Constraint: lwrkmax4×m+3×kplus1,8×n+5×ipmax+mf+10+2×n+2, where ipmax=maxipr, for r=1,2,,mf.

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.)
m, the number of data points to be fitted.
Constraint: m1.
2:     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: mf1.

Output Parameters

1:     aldakplus1 – double array
ai+1j+1 contains the coefficient aij in the approximating polynomial of degree i, for i=n,,k and j=0,1,,i.
2:     skplus1 – double array
si+1 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 General in the E02 Chapter Introduction.
3:     np1 int64int32nag_int scalar
n+1, where n is the total number of constraint conditions imposed: n=mf+p1+p2++pmf.
4:     wrklwrk – double array
Contains weighted residuals of the highest degree of fit determined k. The residual at xr is in element 2n+1+3m+k+1+r, for r=1,2,,m. The rest of the array is used as workspace.
5:     ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and Warnings

Errors or warnings detected by the function:
On entry,m<1,
orlwrk is too small (see Arguments),
(Here n is the total number of constraint conditions.)
ipr<0 for some r=1,2,,mf.
xminxmax, or xfr is not in the interval xmin to xmax for some r=1,2,,mf, or the xfr are not distinct.
xr is not in the interval xmin to xmax for some r=1,2,,m.
xr<xr-1 for some r=2,3,,m.
kplus1>m+n, where m is the number of data points with nonzero weight and distinct abscissae which do not coincide with any xfr. Thus there is no unique solution.
The polynomials μx and/or νx 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.
An unexpected error has been triggered by this routine. Please contact NAG.
Your licence key may have expired or may not have been installed correctly.
Dynamic memory allocation failed.


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.

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_fit_1dcheb_arb (e02ad). To carry out polynomial interpolation only, use nag_interp_1d_cheb (e01ae).


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:
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
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));
          i, x(i), y(i), fiti, fiti-y(i));

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

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

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