NAG FL Interface
e02daf (dim2_​spline_​panel)

Settings help

FL Name Style:


FL Specification Language:


1 Purpose

e02daf forms a minimal, weighted least squares bicubic spline surface fit with prescribed knots to a given set of data points.

2 Specification

Fortran Interface
Subroutine e02daf ( m, px, py, x, y, f, w, lamda, mu, point, npoint, dl, c, nc, ws, nws, eps, sigma, rank, ifail)
Integer, Intent (In) :: m, px, py, point(npoint), npoint, nc, nws
Integer, Intent (Inout) :: ifail
Integer, Intent (Out) :: rank
Real (Kind=nag_wp), Intent (In) :: x(m), y(m), f(m), w(m), eps
Real (Kind=nag_wp), Intent (Inout) :: lamda(px), mu(py)
Real (Kind=nag_wp), Intent (Out) :: dl(nc), c(nc), ws(nws), sigma
C Header Interface
#include <nag.h>
void  e02daf_ (const Integer *m, const Integer *px, const Integer *py, const double x[], const double y[], const double f[], const double w[], double lamda[], double mu[], const Integer point[], const Integer *npoint, double dl[], double c[], const Integer *nc, double ws[], const Integer *nws, const double *eps, double *sigma, Integer *rank, Integer *ifail)
The routine may be called by the names e02daf or nagf_fit_dim2_spline_panel.

3 Description

e02daf determines a bicubic spline fit s(x,y) to the set of data points (xr,yr,fr) with weights wr, for r=1,2,,m. The two sets of internal knots of the spline, {λ} and {μ}, associated with the variables x and y respectively, are prescribed by you. These knots can be thought of as dividing the data region of the (x,y) plane into panels (see Figure 1 in Section 5). A bicubic spline consists of a separate bicubic polynomial in each panel, the polynomials joining together with continuity up to the second derivative across the panel boundaries.
s(x,y) has the property that Σ, the sum of squares of its weighted residuals ρr, for r=1,2,,m, where
ρr = wr (s( x r , y r )- f r ) (1)
is as small as possible for a bicubic spline with the given knot sets. The routine produces this minimized value of Σ and the coefficients cij in the B-spline representation of s(x,y) – see Section 9. e02def, e02dff and e02dhf are available to compute values and derivatives of the fitted spline from the coefficients cij.
The least squares criterion is not always sufficient to determine the bicubic spline uniquely: there may be a whole family of splines which have the same minimum sum of squares. In these cases, the routine selects from this family the spline for which the sum of squares of the coefficients cij is smallest: in other words, the minimal least squares solution. This choice, although arbitrary, reduces the risk of unwanted fluctuations in the spline fit. The method employed involves forming a system of m linear equations in the coefficients cij and then computing its least squares solution, which will be the minimal least squares solution when appropriate. The basis of the method is described in Hayes and Halliday (1974). The matrix of the equation is formed using a recurrence relation for B-splines which is numerically stable (see Cox (1972) and de Boor (1972) – the former contains the more elementary derivation but, unlike de Boor (1972), does not cover the case of coincident knots). The least squares solution is also obtained in a stable manner by using orthogonal transformations, viz. a variant of Givens rotation (see Gentleman (1973)). This requires only one row of the matrix to be stored at a time. Advantage is taken of the stepped-band structure which the matrix possesses when the data points are suitably ordered, there being at most sixteen nonzero elements in any row because of the definition of B-splines. First the matrix is reduced to upper triangular form and then the diagonal elements of this triangle are examined in turn. When an element is encountered whose square, divided by the mean squared weight, is less than a threshold ε, it is replaced by zero and the rest of the elements in its row are reduced to zero by rotations with the remaining rows. The rank of the system is taken to be the number of nonzero diagonal elements in the final triangle, and the nonzero rows of this triangle are used to compute the minimal least squares solution. If all the diagonal elements are nonzero, the rank is equal to the number of coefficients cij and the solution obtained is the ordinary least squares solution, which is unique in this case.

4 References

Cox M G (1972) The numerical evaluation of B-splines J. Inst. Math. Appl. 10 134–149
de Boor C (1972) On calculating with B-splines J. Approx. Theory 6 50–62
Gentleman W M (1973) Least squares computations by Givens transformations without square roots J. Inst. Math. Applic. 12 329–336
Hayes J G and Halliday J (1974) The least squares fitting of cubic spline surfaces to general data sets J. Inst. Math. Appl. 14 89–103

5 Arguments

1: m Integer Input
On entry: m, the number of data points.
Constraint: m>1.
2: px Integer Input
3: py Integer Input
On entry: the total number of knots λ and μ associated with the variables x and y, respectively.
Constraint: px8 and py8.
(They are such that px-8 and py-8 are the corresponding numbers of interior knots.) The running time and storage required by the routine are both minimized if the axes are labelled so that py is the smaller of px and py.
4: x(m) Real (Kind=nag_wp) array Input
5: y(m) Real (Kind=nag_wp) array Input
6: f(m) Real (Kind=nag_wp) array Input
On entry: the coordinates of the data point (xr,yr,fr), for r=1,2,,m. The order of the data points is immaterial, but see the array point.
7: w(m) Real (Kind=nag_wp) array Input
On entry: the weight wr of the rth data point. It is important to note the definition of weight implied by the equation (1) in Section 3, since it is also common usage to define weight as the square of this weight. In this routine, each wr should be chosen inversely proportional to the (absolute) accuracy of the corresponding fr, as expressed, for example, by the standard deviation or probable error of the fr. When the fr are all of the same accuracy, all the wr may be set equal to 1.0.
8: lamda(px) Real (Kind=nag_wp) array Input/Output
On entry: lamda(i+4) must contain the ith interior knot λi+4 associated with the variable x, for i=1,2,,px-8. The knots must be in nondecreasing order and lie strictly within the range covered by the data values of x. A knot is a value of x at which the spline is allowed to be discontinuous in the third derivative with respect to x, though continuous up to the second derivative. This degree of continuity can be reduced, if you require, by the use of coincident knots, provided that no more than four knots are chosen to coincide at any point. Two, or three, coincident knots allow loss of continuity in, respectively, the second and first derivative with respect to x at the value of x at which they coincide. Four coincident knots split the spline surface into two independent parts. For choice of knots see Section 9.
On exit: the interior knots lamda(5) to lamda(px-4) are unchanged, and the segments lamda(1:4) and lamda(px-3:px) contain additional (exterior) knots introduced by the routine in order to define the full set of B-splines required. The four knots in the first segment are all set equal to the lowest data value of x and the other four additional knots are all set equal to the highest value: there is experimental evidence that coincident end-knots are best for numerical accuracy. The complete array must be left undisturbed if e02def or e02dff is to be used subsequently.
9: mu(py) Real (Kind=nag_wp) array Input/Output
On entry: mu(i+4) must contain the ith interior knot μi+4 associated with the variable y, for i=1,2,,py-8.
On exit: the same remarks apply to mu as to lamda above, with y replacing x, and y replacing x.
10: point(npoint) Integer array Input
On entry: indexing information usually provided by e02zaf which enables the data points to be accessed in the order which produces the advantageous matrix structure mentioned in Section 3. This order is such that, if the (x,y) plane is thought of as being divided into rectangular panels by the two sets of knots, all data in a panel occur before data in succeeding panels, where the panels are numbered from bottom to top and then left to right with the usual arrangement of axes, as indicated in Figure 1.
Figure 1
Figure 1
A data point lying exactly on one or more panel sides is considered to be in the highest numbered panel adjacent to the point. e02zaf should be called to obtain the array point, unless it is provided by other means.
11: npoint Integer Input
On entry: the dimension of the array point as declared in the (sub)program from which e02daf is called.
Constraint: npointm+(px-7)×(py-7).
12: dl(nc) Real (Kind=nag_wp) array Output
On exit: gives the squares of the diagonal elements of the reduced triangular matrix, divided by the mean squared weight. It includes those elements, less than ε, which are treated as zero (see Section 3).
13: c(nc) Real (Kind=nag_wp) array Output
On exit: gives the coefficients of the fit. c( (py-4)× (i-1) +j ) is the coefficient cij of Sections 3 and 9, for i=1,2,,px-4 and j=1,2,,py-4. These coefficients are used by e02def or e02dff to calculate values of the fitted function.
14: nc Integer Input
On entry: the value (px-4)×(py-4).
15: ws(nws) Real (Kind=nag_wp) array Workspace
16: nws Integer Input
On entry: the dimension of the array ws as declared in the (sub)program from which e02daf is called.
Constraint: nws(2×nc+1)×(3×py-6)-2.
17: eps Real (Kind=nag_wp) Input
On entry: a threshold ε for determining the effective rank of the system of linear equations. The rank is determined as the number of elements of the array dl which are nonzero. An element of dl is regarded as zero if it is less than ε. Machine precision is a suitable value for ε in most practical applications which have only 2 or 3 decimals accurate in data. If some coefficients of the fit prove to be very large compared with the data ordinates, this suggests that ε should be increased so as to decrease the rank. The array dl will give a guide to appropriate values of ε to achieve this, as well as to the choice of ε in other cases where some experimentation may be needed to determine a value which leads to a satisfactory fit.
18: sigma Real (Kind=nag_wp) Output
On exit: Σ, the weighted sum of squares of residuals. This is not computed from the individual residuals but from the right-hand sides of the orthogonally-transformed linear equations. For further details see page 97 of Hayes and Halliday (1974). The two methods of computation are theoretically equivalent, but the results may differ because of rounding error.
19: rank Integer Output
On exit: the rank of the system as determined by the value of the threshold ε.
rank=nc
The least squares solution is unique.
ranknc
The minimal least squares solution is computed.
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
At least one set of knots is not in nondecreasing order.
ifail=2
More than four knots coincide at a single point.
ifail=3
Array point does not indicate the data points in panel order.
ifail=4
On entry, m=value.
Constraint: m>1.
On entry, nc=value, px=value and py=value.
Constraint: nc=(px-4)×(py-4).
On entry, npoint=value, m=value, px=value and py=value.
Constraint: npointm+(px-7)×(py-7).
On entry, nws is too small. nws=value. Minimum possible dimension: value.
On entry, px=value.
Constraint: px8.
On entry, py=value.
Constraint: py8.
ifail=5
All the weights are zero, or rank determined as zero.
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

The computation of the B-splines and reduction of the observation matrix to triangular form are both numerically stable.

8 Parallelism and Performance

Background information to multithreading can be found in the Multithreading documentation.
e02daf is not threaded in any implementation.

9 Further Comments

The time taken is approximately proportional to the number of data points, m, and to (3×(py-4)+4)2.
The B-spline representation of the bicubic spline is
s(x,y) = i,j cij Mi(x) Nj(y)  
summed over i=1,2,,px-4 and over j=1,2,,py-4. Here Mi(x) and Nj(y) denote normalized cubic B-splines, the former defined on the knots λi,λi+1,,λi+4 and the latter on the knots μj,μj+1,,μj+4. For further details, see Hayes and Halliday (1974) for bicubic splines and de Boor (1972) for normalized B-splines.
The choice of the interior knots, which help to determine the spline's shape, must largely be a matter of trial and error. It is usually best to start with a small number of knots and, examining the fit at each stage, add a few knots at a time in places where the fit is particularly poor. In intervals of x or y where the surface represented by the data changes rapidly, in function value or derivatives, more knots will be needed than elsewhere. In some cases guidance can be obtained by analogy with the case of coincident knots: for example, just as three coincident knots can produce a discontinuity in slope, three close knots can produce rapid change in slope. Of course, such rapid changes in behaviour must be adequately represented by the data points, as indeed must the behaviour of the surface generally, if a satisfactory fit is to be achieved. When there is no rapid change in behaviour, equally-spaced knots will often suffice.
In all cases the fit should be examined graphically before it is accepted as satisfactory.
The fit obtained is not defined outside the rectangle
λ4 x λpx-3 , μ4 y μpy-3 .  
The reason for taking the extreme data values of x and y for these four knots is that, as is usual in data fitting, the fit cannot be expected to give satisfactory values outside the data region. If, nevertheless, you require values over a larger rectangle, this can be achieved by augmenting the data with two artificial data points (a,c,0) and (b,d,0) with zero weight, where axb, cyd defines the enlarged rectangle. In the case when the data are adequate to make the least squares solution unique (rank=nc), this enlargement will not affect the fit over the original rectangle, except for possibly enlarged rounding errors, and will simply continue the bicubic polynomials in the panels bordering the rectangle out to the new boundaries: in other cases the fit will be affected. Even using the original rectangle there may be regions within it, particularly at its corners, which lie outside the data region and where, therefore, the fit will be unreliable. For example, if there is no data point in panel 1 of Figure 1 in Section 5, the least squares criterion leaves the spline indeterminate in this panel: the minimal spline determined by the subroutine in this case passes through the value zero at the point (λ4,μ4).

10 Example

This example reads a value for ε, and a set of data points, weights and knot positions. If there are more y knots than x knots, it interchanges the x and y axes. It calls e02zaf to sort the data points into panel order, e02daf to fit a bicubic spline to them, and e02def to evaluate the spline at the data points.
Finally it prints:
The program is written to handle any number of datasets.
Note:  the data supplied in this example is not typical of a realistic problem: the number of data points would normally be much larger (in which case the array dimensions and the value of nws in the program would have to be increased); and the value of ε would normally be much smaller on most machines (see Section 5; the relatively large value of 10−6 has been chosen in order to illustrate a minimal least squares solution when rank<nc; in this example nc=24).

10.1 Program Text

Program Text (e02dafe.f90)

10.2 Program Data

Program Data (e02dafe.d)

10.3 Program Results

Program Results (e02dafe.r)
GnuplotProduced by GNUPLOT 5.4 patchlevel 6 Polynomial Surface gnuplot_plot_1 gnuplot_plot_2 −1 −0.5 0 0.5 1 y −1 −0.5 0 0.5 1 x −200 −150 −100 −50 0 50 Example Program Evaluation of Least-squares Bi-cubic Spline Fit