NAG FL Interface
e02caf (dim2_​cheb_​lines)

Settings help

FL Name Style:


FL Specification Language:


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

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 <nag.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)
The routine may be called by the names e02caf or nagf_fit_dim2_cheb_lines.

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 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
i=0kj=0laijTi(x¯)Tj(y¯)  
where Ti(x¯) is the Chebyshev polynomial of the first kind of degree i with argument x¯, and Tj(y) 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 Arguments

1: m(n) Integer array Input
On entry: m(s) must be set to ms, the number of data x values on the line y=ys, for s=1,2,,n.
Constraint: m(s)>0, for s=1,2,,n.
2: n Integer Input
On entry: the number of lines y= constant on which data points are given.
Constraint: n>0.
3: k Integer Input
On entry: k, the required degree of x in the fit.
Constraint: for s=1,2,,n, inuxp1-1k<mdist(s)+inuxp1-1, where mdist(s) is the number of distinct x values with nonzero weight on the line y=ys. See Section 9.
4: l Integer Input
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) array Input
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) array Input
On entry: y(s) 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) array Input
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) array Input
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 Integer Input
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 m(s) .
10: a(na) Real (Kind=nag_wp) array Output
On exit: contains the Chebyshev coefficients of the fit. a(i×(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 Integer Input
On entry: the dimension of the array a as declared in the (sub)program from which e02caf is called.
Constraint: na(k+1)×(l+1), the total number of coefficients in the fit.
12: xmin(n) Real (Kind=nag_wp) array Input
On entry: xmin(s) 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: xmax(n) Real (Kind=nag_wp) array Input
On entry: xmax(s) 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: xmax(s)>xmin(s).
14: nux(inuxp1) Real (Kind=nag_wp) array Input
On entry: nux(i) 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: nux(inuxp1) must be nonzero, unless inuxp1=1, in which case nux is ignored.
15: inuxp1 Integer Input
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) array Input
On entry: nuy(i) 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: nuy(inuyp1) must be nonzero, unless inuyp1=1, in which case nuy is ignored.
17: inuyp1 Integer Input
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) array Workspace
19: nwork Integer Input
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+max(k,l)) .
20: ifail Integer Input/Output
On entry: ifail must be set to 0, −1 or 1 to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of 0 causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of −1 means that an error message is printed while a value of 1 means that it is not.
If halting is not appropriate, the value −1 or 1 is recommended. If message printing is undesirable, then the value 1 is recommended. Otherwise, the value 0 is recommended. 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, I=value, m(I)=value, k=value and inuxp1=value.
Constraint: m(I)k-inuxp1+2.
On entry, inuxp1=value.
Constraint: inuxp11.
On entry, inuxp1=value and k=value.
Constraint: inuxp1k+1.
On entry, inuyp1=value.
Constraint: inuyp11.
On entry, inuyp1=value and l=value.
Constraint: inuyp1l+1.
On entry, k=value.
Constraint: k0.
On entry, l=value.
Constraint: l0.
On entry, mtot is too small. mtot=value. Minimum possible dimension: value.
On entry, n=value, l=value and inuyp1=value.
Constraint: nl-inuyp1+2.
On entry, na is too small. na=value. Minimum possible dimension: value.
On entry, nwork is too small. nwork=value. Minimum possible dimension: value.
ifail=2
On entry, xmin(I) and xmax(I) do not span the data x values on y=y(I): I=value, xmin(I)=value, xmax(I)=value and y(I)=value.
ifail=3
On entry, I=value, y(I)=value and y(I-1)=value.
Constraint: y(I)>y(I-1).
On entry, the data x values are not nondecreasing for y=y(I): I=value and y(I)=value.
ifail=4
On entry, the number of distinct x values with nonzero weight on y=y(I) is less than k-inuxp1+2: I=value, y(I)=value, k=value and inuxp1=value.
ifail=5
On entry, inuxp1=value, nux(inuxp1)=value, inuyp1=value and nuy(inuyp1)=value.
Constraint: if nux(inuxp1)=0.0, inuxp1=1; if nuy(inuyp1)=0.0, inuyp1=1.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

7 Accuracy

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

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

9 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×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.

10 Example

This example reads data in the following order, using the notation of the argument list for e02caf above:
nkl y(i)m(i)xmin(i)xmax(i), for ​i=1,2,,n x(i)f(i)w(i), 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:

10.1 Program Text

Program Text (e02cafe.f90)

10.2 Program Data

Program Data (e02cafe.d)

10.3 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