NAG Library Routine Document
E02DAF
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
SUBROUTINE E02DAF ( |
M, PX, PY, X, Y, F, W, LAMDA, MU, POINT, NPOINT, DL, C, NC, WS, NWS, EPS, SIGMA, RANK, IFAIL) |
INTEGER |
M, PX, PY, POINT(NPOINT), NPOINT, NC, NWS, RANK, IFAIL |
REAL (KIND=nag_wp) |
X(M), Y(M), F(M), W(M), LAMDA(PX), MU(PY), DL(NC), C(NC), WS(NWS), EPS, SIGMA |
|
3 Description
E02DAF determines a bicubic spline fit
to the set of data points
with weights
, for
. The two sets of internal knots of the spline,
and
, associated with the variables
and
respectively, are prescribed by you. These knots can be thought of as dividing the data region of the
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.
has the property that
, the sum of squares of its weighted residuals
, for
, where
is as small as possible for a bicubic spline with the given knot sets. The routine produces this minimized value of
and the coefficients
in the B-spline representation of
– see
Section 8.
E02DEF,
E02DFF and
E02DHF are available to compute values and derivatives of the fitted spline from the coefficients
.
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
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
linear equations in the coefficients
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
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 Parameters
- 1: M – INTEGERInput
On entry: , the number of data points.
Constraint:
.
- 2: PX – INTEGERInput
- 3: PY – INTEGERInput
On entry: the total number of knots and associated with the variables and , respectively.
Constraint:
and
.
(They are such that
and
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) arrayInput
- 5: Y(M) – REAL (KIND=nag_wp) arrayInput
- 6: F(M) – REAL (KIND=nag_wp) arrayInput
On entry: the coordinates of the data point
, for
. The order of the data points is immaterial, but see the array
POINT.
- 7: W(M) – REAL (KIND=nag_wp) arrayInput
On entry: the weight
of the
th 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
should be chosen inversely proportional to the (absolute) accuracy of the corresponding
, as expressed, for example, by the standard deviation or probable error of the
. When the
are all of the same accuracy, all the
may be set equal to
.
- 8: LAMDA(PX) – REAL (KIND=nag_wp) arrayInput/Output
On entry:
must contain the
th interior knot
associated with the variable
, for
. The knots must be in nondecreasing order and lie strictly within the range covered by the data values of
. A knot is a value of
at which the spline is allowed to be discontinuous in the third derivative with respect to
, 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
at the value of
at which they coincide. Four coincident knots split the spline surface into two independent parts. For choice of knots see
Section 8.
On exit: the interior knots
to
are unchanged, and the segments
and
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
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) arrayInput/Output
On entry: must contain the th interior knot associated with the variable , for .
On exit: the same remarks apply to
MU as to
LAMDA above, with
Y replacing
X, and
replacing
.
- 10: POINT(NPOINT) – INTEGER arrayInput
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
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
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 – INTEGERInput
On entry: the dimension of the array
POINT as declared in the (sub)program from which E02DAF is called.
Constraint:
.
- 12: DL(NC) – REAL (KIND=nag_wp) arrayOutput
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) arrayOutput
On exit: gives the coefficients of the fit.
is the coefficient
of
Sections 3 and
8, for
and
. These coefficients are used by
E02DEF or
E02DFF to calculate values of the fitted function.
- 14: NC – INTEGERInput
On entry: the value .
- 15: WS(NWS) – REAL (KIND=nag_wp) arrayWorkspace
- 16: NWS – INTEGERInput
On entry: the dimension of the array
WS as declared in the (sub)program from which E02DAF is called.
Constraint:
.
- 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
or
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 – INTEGEROutput
On exit: the rank of the system as determined by the value of the threshold
.
- The least squares solution is unique.
- The minimal least squares solution is computed.
- 20: IFAIL – INTEGERInput/Output
-
On entry:
IFAIL must be set to
,
. 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
is recommended. If the output of error messages is undesirable, then the value
is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is
.
When the value is used it is essential to test the value of IFAIL on exit.
On exit:
unless the routine detects an error or a warning has been flagged (see
Section 6).
6 Error Indicators and Warnings
If on entry
or
, explanatory error messages are output on the current error message unit (as defined by
X04AAF).
Errors or warnings detected by the routine:
At least one set of knots is not in nondecreasing order, or an interior knot is outside the range of the data values.
More than four knots coincide at a single point, possibly because all data points have the same value of (or ) or because an interior knot coincides with an extreme data value.
Array
POINT does not indicate the data points in panel order. Call
E02ZAF to obtain a correct array.
On entry, | , |
or | , |
or | , |
or | , |
or | NWS is too small, |
or | NPOINT is too small. |
All the weights are zero or rank determined as zero.
7 Accuracy
The computation of the B-splines and reduction of the observation matrix to triangular form are both numerically stable.
The time taken is approximately proportional to the number of data points, , and to .
The B-spline representation of the bicubic spline is
summed over
and over
. Here
and
denote normalized cubic B-splines, the former defined on the knots
and the latter on the knots
. 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 or 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
The reason for taking the extreme data values of
and
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
and
with zero weight, where
,
defines the enlarged rectangle. In the case when the data are adequate to make the least squares solution unique (
), 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
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
.
9 Example
This example reads a value for
, and a set of data points, weights and knot positions. If there are more
knots than
knots, it interchanges the
and
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 weighted sum of squares of residuals computed from the linear equations; |
– |
the rank determined by E02DAF; |
– |
data points, fitted values and residuals in panel order; |
– |
the weighted sum of squares of the residuals; and |
– |
the coefficients of the spline fit. |
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
has been chosen in order to illustrate a minimal least squares solution when
; in this example
).
9.1 Program Text
Program Text (e02dafe.f90)
9.2 Program Data
Program Data (e02dafe.d)
9.3 Program Results
Program Results (e02dafe.r)