E02CAF (PDF version)
E02 Chapter Contents
E02 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

E02CAF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

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.

2  Specification

SUBROUTINE E02CAF ( M, N, K, L, X, Y, F, W, MTOT, A, NA, XMIN, XMAX, NUX, INUXP1, NUY, INUYP1, WORK, NWORK, IFAIL)
INTEGER  M(N), N, K, L, MTOT, NA, INUXP1, INUYP1, NWORK, IFAIL
REAL (KIND=nag_wp)  X(MTOT), Y(N), F(MTOT), W(MTOT), A(NA), XMIN(N), XMAX(N), NUX(INUXP1), NUY(INUYP1), WORK(NWORK)

3  Description

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 8). 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 8). 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
i=0kj=0laijTix-Tjy-
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)).

4  References

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

5  Parameters

1:     M(N) – 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 8.
4:     L – INTEGERInput
On entry: l, the required degree of y in the fit.
Constraints:
  • L0;
  • INUYP1-1L<N+INUYP1-1.
5:     X(MTOT) – 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:     Y(N) – 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:     F(MTOT) – REAL (KIND=nag_wp) arrayInput
On entry: f, the data values of the dependent variable in the same sequence as the x values.
8:     W(MTOT) – 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:   A(NA) – 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:   XMIN(N) – 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 8.)
13:   XMAX(N) – 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 8.)
Constraint: XMAXs>XMINs.
14:   NUX(INUXP1) – 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:   NUY(INUYP1) – 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:   WORK(NWORK) – 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 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​ 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 -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).

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:
IFAIL=1
On entry,K or L<0,
orINUXP1 or INUYP1<1,
orINUXP1>K+1,
orINUYP1>L+1,
orMi<K-INUXP1+2 for some i=1,2,,N,
orN<L-INUYP1+2,
orNA is too small,
orNWORK is too small,
orMTOT is too small.
IFAIL=2
XMINi and XMAXi do not span the data X values on Y=Yi for some i=1,2,,N, possibly because XMINiXMAXi.
IFAIL=3
The data X values on Y=Yi are not nondecreasing for some i=1,2,,N, or the Yi themselves are not strictly increasing.
IFAIL=4
The number of distinct X values with nonzero weight on Y=Yi is less than K-INUXP1+2 for some i=1,2,,N.
IFAIL=5
On entry,NUXINUXP1=0.0 and INUXP11,
orNUYINUYP1=0.0 and INUYP11.

7  Accuracy

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

8  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.

9  Example

This example reads data in the following order, using the notation of the parameter 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:

9.1  Program Text

Program Text (e02cafe.f90)

9.2  Program Data

Program Data (e02cafe.d)

9.3  Program Results

Program Results (e02cafe.r)

Produced by GNUPLOT 4.4 patchlevel 0 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 polynomials in x for constant y 50*residual data points

E02CAF (PDF version)
E02 Chapter Contents
E02 Chapter Introduction
NAG Library Manual

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