NAG Library Routine Document

e02caf (dim2_cheb_lines)


    1  Purpose
    7  Accuracy


e02caf forms an approximation to the weighted, least squares Chebyshev series surface fit to data arbitrarily distributed on lines parallel to one independent coordinate axis.


Fortran Interface
Subroutine e02caf ( m, n, k, l, x, y, f, w, mtot, a, na, xmin, xmax, nux, inuxp1, nuy, inuyp1, work, nwork, ifail)
Integer, Intent (In):: m(n), n, k, l, mtot, na, inuxp1, inuyp1, nwork
Integer, Intent (Inout):: ifail
Real (Kind=nag_wp), Intent (In):: x(mtot), y(n), f(mtot), w(mtot), xmin(n), xmax(n), nux(inuxp1), nuy(inuyp1)
Real (Kind=nag_wp), Intent (Out):: a(na), work(nwork)
C Header Interface
#include nagmk26.h
void  e02caf_ (const Integer m[], const Integer *n, const Integer *k, const Integer *l, const double x[], const double y[], const double f[], const double w[], const Integer *mtot, double a[], const Integer *na, const double xmin[], const double xmax[], const double nux[], const Integer *inuxp1, const double nuy[], const Integer *inuyp1, double work[], const Integer *nwork, Integer *ifail)


e02caf determines a bivariate polynomial approximation of degree k in x and l in y to the set of data points xr,s,ys,fr,s, with weights wr,s, for s=1,2,,n and r=1,2,,ms. That is, the data points are on lines y=ys, but the x values may be different on each line. The values of k and l are prescribed by you (for guidance on their choice, see Section 9). The subroutine is based on the method described in Sections 5 and 6 of Clenshaw and Hayes (1965).
The polynomial is represented in double Chebyshev series form with arguments x- and y-. The arguments lie in the range -1 to +1 and are related to the original variables x and y by the transformations
x-=2x-xmax+xmin xmax-xmin   and  y-=2y-ymax+ymin ymax-ymin .  
Here ymax and ymin are set by the subroutine to, respectively, the largest and smallest value of ys, but xmax and xmin are functions of y prescribed by you (see Section 9). For this subroutine, only their values x max s  and x min s  at each y=ys are required. For each s=1,2,,n, x max s  must not be less than the largest xr,s on the line y=ys, and, similarly, x min s  must not be greater than the smallest xr,s.
The double Chebyshev series can be written as
where Tix- is the Chebyshev polynomial of the first kind of degree i with argument x-, and Tjy is similarly defined. However, the standard convention, followed in this subroutine, is that coefficients in the above expression which have either i or j zero are written as 12aij, instead of simply aij, and the coefficient with both i and j equal to zero is written as 14a0,0. The series with coefficients output by the subroutine should be summed using this convention. e02cbf is available to compute values of the fitted function from these coefficients.
The subroutine first obtains Chebyshev series coefficients cs,i, for i=0,1,,k, of the weighted least squares polynomial curve fit of degree k in x- to the data on each line y=ys, for s=1,2,,n, in turn, using an auxiliary subroutine. The same subroutine is then called k+1 times to fit cs,i, for s=1,2,,n, by a polynomial of degree l in y-, for each i=0,1,,k. The resulting coefficients are the required aij.
You can force the fit to contain a given polynomial factor. This allows for the surface fit to be constrained to have specified values and derivatives along the boundaries x=xmin, x=xmax, y=ymin and y=ymax or indeed along any lines x-= constant or y-= constant (see Section 8 of Clenshaw and Hayes (1965)).


Clenshaw C W and Hayes J G (1965) Curve and surface fitting J. Inst. Math. Appl. 1 164–183
Hayes J G (ed.) (1970) Numerical Approximation to Functions and Data Athlone Press, London


1:     mn – Integer arrayInput
On entry: ms must be set to ms, the number of data x values on the line y=ys, for s=1,2,,n.
Constraint: ms>0, for s=1,2,,n.
2:     n – IntegerInput
On entry: the number of lines y= constant on which data points are given.
Constraint: n>0.
3:     k – IntegerInput
On entry: k, the required degree of x in the fit.
Constraint: for s=1,2,,n, inuxp1-1k<mdists+inuxp1-1, where mdists is the number of distinct x values with nonzero weight on the line y=ys. See Section 9.
4:     l – IntegerInput
On entry: l, the required degree of y in the fit.
  • l0;
  • inuyp1-1l<n+inuyp1-1.
5:     xmtot – Real (Kind=nag_wp) arrayInput
On entry: the x values of the data points. The sequence must be
  • all points on y=y1, followed by
  • all points on y=y2, followed by
  • all points on y=yn.
Constraint: for each ys, the x values must be in nondecreasing order.
6:     yn – Real (Kind=nag_wp) arrayInput
On entry: ys must contain the y value of line y=ys, for s=1,2,,n, on which data is given.
Constraint: the ys values must be in strictly increasing order.
7:     fmtot – Real (Kind=nag_wp) arrayInput
On entry: f, the data values of the dependent variable in the same sequence as the x values.
8:     wmtot – Real (Kind=nag_wp) arrayInput
On entry: the weights to be assigned to the data points, in the same sequence as the x values. These weights should be calculated from estimates of the absolute accuracies of the fr, expressed as standard deviations, probable errors or some other measure which is of the same dimensions as fr. Specifically, each wr should be inversely proportional to the accuracy estimate of fr. Often weights all equal to unity will be satisfactory. If a particular weight is zero, the corresponding data point is omitted from the fit.
9:     mtot – IntegerInput
On entry: the dimension of the arrays x, f and w as declared in the (sub)program from which e02caf is called.
Constraint: mtot s=1 n ms .
10:   ana – Real (Kind=nag_wp) arrayOutput
On exit: contains the Chebyshev coefficients of the fit. ai×l+1+j is the coefficient aij of Section 3 defined according to the standard convention. These coefficients are used by e02cbf to calculate values of the fitted function.
11:   na – IntegerInput
On entry: the dimension of the array a as declared in the (sub)program from which e02caf is called.
Constraint: nak+1×l+1, the total number of coefficients in the fit.
12:   xminn – Real (Kind=nag_wp) arrayInput
On entry: xmins must contain xmin s , the lower end of the range of x on the line y=ys, for s=1,2,,n. It must not be greater than the lowest data value of x on the line. Each xmin s  is scaled to -1.0 in the fit. (See also Section 9.)
13:   xmaxn – Real (Kind=nag_wp) arrayInput
On entry: xmaxs must contain x max s , the upper end of the range of x on the line y=ys, for s=1,2,,n. It must not be less than the highest data value of x on the line. Each xmax s  is scaled to +1.0 in the fit. (See also Section 9.)
Constraint: xmaxs>xmins.
14:   nuxinuxp1 – Real (Kind=nag_wp) arrayInput
On entry: nuxi must contain the coefficient of the Chebyshev polynomial of degree i-1 in x-, in the Chebyshev series representation of the polynomial factor in x- which you require the fit to contain, for i=1,2,,inuxp1. These coefficients are defined according to the standard convention of Section 3.
Constraint: nuxinuxp1 must be nonzero, unless inuxp1=1, in which case nux is ignored.
15:   inuxp1 – IntegerInput
On entry: INUX+1, where INUX is the degree of a polynomial factor in x- which you require the fit to contain. (See Section 3, last paragraph.)
If this option is not required, inuxp1 should be set equal to 1.
Constraint: 1inuxp1k+1.
16:   nuyinuyp1 – Real (Kind=nag_wp) arrayInput
On entry: nuyi must contain the coefficient of the Chebyshev polynomial of degree i-1 in y-, in the Chebyshev series representation of the polynomial factor which you require the fit to contain, for i=1,2,,inuyp1. These coefficients are defined according to the standard convention of Section 3.
Constraint: nuyinuyp1 must be nonzero, unless inuyp1=1, in which case nuy is ignored.
17:   inuyp1 – IntegerInput
On entry: INUY+1, where INUY is the degree of a polynomial factor in y- which you require the fit to contain. (See Section 3, last paragraph.) If this option is not required, inuyp1 should be set equal to 1.
18:   worknwork – Real (Kind=nag_wp) arrayWorkspace
19:   nwork – IntegerInput
On entry: the dimension of the array work as declared in the (sub)program from which e02caf is called.
Constraint: nwork 3 × mtot + 2 × n × k + 2 + 5 × 1 + maxk,l .
20:   ifail – IntegerInput/Output
On entry: ifail must be set to 0, -1​ or ​1. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ 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 argument, the recommended value is 0. When the value -1​ or ​1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

Error Indicators and Warnings

If on entry ifail=0 or -1, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
On entry,k or l<0,
orinuxp1 or inuyp1<1,
ormi<k-inuxp1+2 for some i=1,2,,n,
orna is too small,
ornwork is too small,
ormtot is too small.
xmini and xmaxi do not span the data x values on y=yi for some i=1,2,,n, possibly because xminixmaxi.
The data x values on y=yi are not nondecreasing for some i=1,2,,n, or the yi themselves are not strictly increasing.
The number of distinct x values with nonzero weight on y=yi is less than k-inuxp1+2 for some i=1,2,,n.
On entry,nuxinuxp1=0.0 and inuxp11,
ornuyinuyp1=0.0 and inuyp11.
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.


No error analysis for this method has been published. Practical experience with the method, however, is generally extremely satisfactory.

Parallelism and Performance

e02caf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

Further Comments

The time taken is approximately proportional to k×k×mtot+n×l2.
The reason for allowing xmax and xmin (which are used to normalize the range of x) to vary with y is that unsatisfactory fits can result if the highest (or lowest) data values of the normalized x on each line y=ys are not approximately the same. (For an explanation of this phenomenon, see page 176 of Clenshaw and Hayes (1965).) Commonly in practice, the lowest (for example) data values x1,s, while not being approximately constant, do lie close to some smooth curve in the x,y plane. Using values from this curve as the values of xmin, different in general on each line, causes the lowest transformed data values x-1,s to be approximately constant. Sometimes, appropriate curves for xmax and xmin will be clear from the context of the problem (they need not be polynomials). If this is not the case, suitable curves can often be obtained by fitting to the lowest data values x1,s and to the corresponding highest data values of x, low degree polynomials in y, using routine e02adf, and then shifting the two curves outwards by a small amount so that they just contain all the data between them. The complete curves are not in fact supplied to the present subroutine, only their values at each ys; and the values simply need to lie on smooth curves. More values on the complete curves will be required subsequently, when computing values of the fitted surface at arbitrary y values.
Naturally, a satisfactory approximation to the surface underlying the data cannot be expected if the character of the surface is not adequately represented by the data. Also, as always with polynomials, the approximating function may exhibit unwanted oscillations (particularly near the ends of the ranges) if the degrees k and l are taken greater than certain values, generally unknown but depending on the total number of coefficients k+1×l+1 should be significantly smaller than, say not more than half, the total number of data points. Similarly, k+1 should be significantly smaller than most (preferably all) the ms, and l+1 significantly smaller than n. Closer spacing of the data near the ends of the x and y ranges is an advantage. In particular, if y-s = - cos πs-1/ n-1 , for s=1,2,,n and x-r,s = - cos πr-1/ m-1 , for r=1,2,,m, (thus ms=m for all s), then the values k=m-1 and l=n-1 (so that the polynomial passes exactly through all the data points) should not give unwanted oscillations. Other datasets should be similarly satisfactory if they are everywhere at least as closely spaced as the above cosine values with m replaced by k+1 and n by l+1 (more precisely, if for every s the largest interval between consecutive values of arccosx-r,s, for r=1,2,,m, is not greater than π/k, and similarly for the y-s). The polynomial obtained should always be examined graphically before acceptance. Note that, for this purpose it is not sufficient to plot the polynomial only at the data values of x and y: intermediate values should also be plotted, preferably via a graphics facility.
Provided the data are adequate, and the surface underlying the data is of a form that can be represented by a polynomial of the chosen degrees, the subroutine should produce a good approximation to this surface. It is not, however, the true least squares surface fit nor even a polynomial in x and y, the original variables (see Section 6 of Clenshaw and Hayes (1965), ), except in certain special cases. The most important of these is where the data values of x are the same on each line y=ys, (i.e., the data points lie on a rectangular mesh in the x,y plane), the weights of the data points are all equal, and xmax and xmin are both constants (in this case they should be set to the largest and smallest data values of x, respectively).
If the dataset is such that it can be satisfactorily approximated by a polynomial of degrees k and l, say, then if higher values are used for k and l in the subroutine, all the coefficients aij for i>k or j>l will take apparently random values within a range bounded by the size of the data errors, or rather less. (This behaviour of the Chebyshev coefficients, most readily observed if they are set out in a rectangular array, closely parallels that in curve-fitting, examples of which are given in Section 8 of Hayes (1970).) In practice, therefore, to establish suitable values of k and l, you should first be seeking (within the limitations discussed above) values for k and l which are large enough to exhibit the behaviour described. Values for k and l should then be chosen as the smallest which do not exclude any coefficients significantly larger than the random ones. A polynomial of degrees k and l should then be fitted to the data.
If the option to force the fit to contain a given polynomial factor in x is used and if zeros of the chosen factor coincide with data x values on any line, then the effective number of data points on that line is reduced by the number of such coincidences. A similar consideration applies when forcing the y-direction. No account is taken of this by the subroutine when testing that the degrees k and l have not been chosen too large.


This example reads data in the following order, using the notation of the argument list for e02caf above:
nkl yimixminixmaxi, for ​i=1,2,,n xifiwi, for ​i=1,2,,mtot.  
The data points are fitted using e02caf, and then the fitting polynomial is evaluated at the data points using e02cbf.
The output is:

Program Text

Program Text (e02cafe.f90)

Program Data

Program Data (e02cafe.d)

Program Results

Program Results (e02cafe.r)

GnuplotProduced by GNUPLOT 4.6 patchlevel 3 0 1 2 3 4 5 6 7 8 9 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 Fitted Polynomials P(x,y=Y) x Example Program Calculation and Evaluation of Least-squares Bi-variate Polynomial Fit y = 4 y = 2 y = 1 y = 0 gnuplot_plot_1 polynomials in x for constant y gnuplot_plot_2 50*residual gnuplot_plot_3 data points
© The Numerical Algorithms Group Ltd, Oxford, UK. 2017