NAG Library Routine Document
e02ddf
(dim2_spline_sctr)
1
Purpose
e02ddf computes a bicubic spline approximation to a set of scattered data. The knots of the spline are located automatically, but a single argument must be specified to control the trade-off between closeness of fit and smoothness of fit.
2
Specification
Fortran Interface
Subroutine e02ddf ( |
start,
m,
x,
y,
f,
w,
s,
nxest,
nyest,
nx,
lamda,
ny,
mu,
c,
fp,
rank,
wrk,
lwrk,
iwrk,
liwrk,
ifail) |
Integer, Intent (In) | :: |
m,
nxest,
nyest,
lwrk,
liwrk | Integer, Intent (Inout) | :: |
nx,
ny,
ifail | Integer, Intent (Out) | :: |
rank,
iwrk(liwrk) | Real (Kind=nag_wp), Intent (In) | :: |
f(m),
w(m),
s | Real (Kind=nag_wp), Intent (Inout) | :: |
x(m),
y(m),
lamda(nxest),
mu(nyest),
wrk(lwrk) | Real (Kind=nag_wp), Intent (Out) | :: |
c((nxest-4)*(nyest-4)),
fp | Character (1), Intent (In) | :: |
start |
|
C Header Interface
#include nagmk26.h
void |
e02ddf_ (
const char *start,
const Integer *m,
double x[],
double y[],
const double f[],
const double w[],
const double *s,
const Integer *nxest,
const Integer *nyest,
Integer *nx,
double lamda[],
Integer *ny,
double mu[],
double c[],
double *fp,
Integer *rank,
double wrk[],
const Integer *lwrk,
Integer iwrk[],
const Integer *liwrk,
Integer *ifail,
const Charlen length_start) |
|
3
Description
e02ddf determines a smooth bicubic spline approximation to the set of data points with weights , for .
The approximation domain is considered to be the rectangle , where and denote the lowest and highest data values of .
The spline is given in the B-spline representation
where
and
denote normalized cubic B-splines, the former defined on the knots
to
and the latter on the knots
to
. For further details, see
Hayes and Halliday (1974) for bicubic splines and
de Boor (1972) for normalized B-splines.
The total numbers
and
of these knots and their values
and
are chosen automatically by the routine. The knots
and
are the interior knots; they divide the approximation domain
into
subpanels
, for
and
. Then, much as in the curve case (see
e02bef), the coefficients
are determined as the solution of the following constrained minimization problem:
minimize
subject to the constraint
where: |
|
is a measure of the (lack of) smoothness of . Its value depends on the discontinuity jumps in across the boundaries of the subpanels. It is zero only when there are no discontinuities and is positive otherwise, increasing with the size of the jumps (see Dierckx (1981b) for details). |
|
|
denotes the weighted residual
, |
and |
|
is a non-negative number to be specified by you. |
By means of the argument
, ‘the smoothing factor’, you will then control the balance between smoothness and closeness of fit, as measured by the sum of squares of residuals in
(3). If
is too large, the spline will be too smooth and signal will be lost (underfit); if
is too small, the spline will pick up too much noise (overfit). In the extreme cases the method would return an interpolating spline
if
were set to zero, and returns the least squares bicubic polynomial
if
is set very large. Experimenting with
-values between these two extremes should result in a good compromise. (See
Section 9.2 for advice on choice of
.) Note however, that this routine, unlike
e02bef and
e02dcf, does not allow
to be set exactly to zero: to compute an interpolant to scattered data,
e01saf or
e01sgf should be used.
The method employed is outlined in
Section 9.5 and fully described in
Dierckx (1981a) and
Dierckx (1981b). It involves an adaptive strategy for locating the knots of the bicubic spline (depending on the function underlying the data and on the value of
), and an iterative method for solving the constrained minimization problem once the knots have been determined.
Values and derivatives of the computed spline can subsequently be computed by calling
e02def,
e02dff or
e02dhf as described in
Section 9.6.
4
References
de Boor C (1972) On calculating with B-splines J. Approx. Theory 6 50–62
Dierckx P (1981a) An improved algorithm for curve fitting with spline functions Report TW54 Department of Computer Science, Katholieke Univerciteit Leuven
Dierckx P (1981b) An algorithm for surface fitting with spline functions IMA J. Numer. Anal. 1 267–283
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
Peters G and Wilkinson J H (1970) The least squares problem and pseudo-inverses Comput. J. 13 309–316
Reinsch C H (1967) Smoothing by spline functions Numer. Math. 10 177–183
5
Arguments
- 1: – Character(1)Input
-
On entry: determines whether calculations are to be performed afresh (Cold Start) or whether knots found in previous calls are to be used as an initial estimate of knot placement (Warm Start).
- The routine will build up the knot set starting with no interior knots. No values need be assigned to the arguments nx, ny, lamda, mu or wrk.
- The routine will restart the knot-placing strategy using the knots found in a previous call of the routine. In this case, the arguments nx, ny, lamda, mu and wrk must be unchanged from that previous call. This warm start can save much time in determining a satisfactory set of knots for the given value of s. This is particularly useful when different smoothing factors are used for the same dataset.
Constraint:
or .
- 2: – IntegerInput
-
On entry:
, the number of data points.
The number of data points with nonzero weight (see
w) must be at least
.
- 3: – Real (Kind=nag_wp) arrayInput
- 4: – Real (Kind=nag_wp) arrayInput
- 5: – Real (Kind=nag_wp) arrayInput
-
On entry: , , must be set to the coordinates of , the th data point, for . The order of the data points is immaterial.
- 6: – Real (Kind=nag_wp) arrayInput
-
On entry:
must be set to
, the
th value in the set of weights, for
. Zero weights are permitted and the corresponding points are ignored, except when determining
,
,
and
(see
Section 9.4). For advice on the choice of weights, see
Section 2.1.2 in the E02 Chapter Introduction.
Constraint:
the number of data points with nonzero weight must be at least .
- 7: – Real (Kind=nag_wp)Input
-
On entry: the smoothing factor,
s.
For advice on the choice of
s, see
Sections 3 and
9.2.
Constraint:
.
- 8: – IntegerInput
- 9: – IntegerInput
-
On entry: an upper bound for the number of knots
and
required in the
- and
-directions respectively.
In most practical situations,
is sufficient. See also
Section 9.3.
Constraint:
and .
- 10: – IntegerInput/Output
-
On entry: if the warm start option is used, the value of
nx must be left unchanged from the previous call.
On exit: the total number of knots, , of the computed spline with respect to the variable.
- 11: – Real (Kind=nag_wp) arrayInput/Output
-
On entry: if the warm start option is used, the values must be left unchanged from the previous call.
On exit: contains the complete set of knots
associated with the
variable, i.e., the interior knots
as well as the additional knots
and
needed for the B-spline representation (where
and
are as described in
Section 3).
- 12: – IntegerInput/Output
-
On entry: if the warm start option is used, the value of
ny must be left unchanged from the previous call.
On exit: the total number of knots, , of the computed spline with respect to the variable.
- 13: – Real (Kind=nag_wp) arrayInput/Output
-
On entry: if the warm start option is used, the values must be left unchanged from the previous call.
On exit: contains the complete set of knots
associated with the
variable, i.e., the interior knots
as well as the additional knots
and
needed for the B-spline representation (where
and
are as described in
Section 3).
- 14: – Real (Kind=nag_wp) arrayOutput
-
On exit: the coefficients of the spline approximation.
is the coefficient
defined in
Section 3.
- 15: – Real (Kind=nag_wp)Output
-
On exit: the weighted sum of squared residuals,
, of the computed spline approximation.
fp should equal
s within a relative tolerance of
unless
, when the spline has no interior knots and so is simply a bicubic polynomial. For knots to be inserted,
s must be set to a value below the value of
fp produced in this case.
- 16: – IntegerOutput
-
On exit: gives the rank of the system of equations used to compute the final spline (as determined by a suitable machine-dependent threshold). When
, the solution is unique; otherwise the system is rank-deficient and the minimum-norm solution is computed. The latter case may be caused by too small a value of
s.
- 17: – Real (Kind=nag_wp) arrayCommunication Array
-
If the warm start option is used, on entry, the value of must be left unchanged from the previous call.
This array is used as workspace.
- 18: – IntegerInput
-
On entry: the dimension of the array
wrk as declared in the (sub)program from which
e02ddf is called.
Constraint:
,
where
,
and
.
For some problems, the routine may need to compute the minimal least squares solution of a rank-deficient system of linear equations (see
Section 3). The amount of workspace required to solve such problems will be larger than specified by the value given above, which must be increased by an amount,
lwrk2 say. An upper bound for
lwrk2 is given by
, where
,
and
are as above. However, if there are enough data points, scattered uniformly over the approximation domain, and if the smoothing factor
s is not too small, there is a good chance that this extra workspace is not needed. A lot of memory might therefore be saved by assuming that no additional workspace is required (
).
- 19: – Integer arrayWorkspace
- 20: – IntegerInput
-
On entry: the dimension of the array
iwrk as declared in the (sub)program from which
e02ddf is called.
Constraint:
.
- 21: – IntegerInput/Output
-
On entry:
ifail must be set to
,
. 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
is recommended. If the output of error messages is undesirable, then the value
is recommended. Otherwise, if you are not familiar with this argument, 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:
-
On entry, | or , |
or | the number of data points with nonzero weight , |
or | , |
or | , |
or | , |
or | , where , and , |
or | . |
-
On entry, either all the , for , are equal, or all the , for , are equal.
-
The number of knots required is greater than allowed by
nxest and
nyest. Try increasing
nxest and/or
nyest and, if necessary, supplying larger arrays for the arguments
lamda,
mu,
c,
wrk and
iwrk. However, if
nxest and
nyest are already large, say
nxest,
, this error exit may indicate that
s is too small.
-
No more knots can be added because the number of B-spline coefficients
already exceeds the number of data points
m. This error exit may occur if either of
s or
m is too small.
-
No more knots can be added because the additional knot would (quasi) coincide with an old one. This error exit may occur if too large a weight has been given to an inaccurate data point, or if
s is too small.
-
The iterative process used to compute the coefficients of the approximating spline has failed to converge. This error exit may occur if
s has been set very small. If the error persists with increased
s, contact
NAG.
-
lwrk is too small; the routine needs to compute the minimal least squares solution of a rank-deficient system of linear equations, but there is not enough workspace. There is no approximation returned but, having saved the information contained in
nx,
lamda,
ny,
mu and
wrk, and having adjusted the value of
lwrk and the dimension of array
wrk accordingly, you can continue at the point the program was left by calling
e02ddf with
. Note that the requested value for
lwrk is only large enough for the current phase of the algorithm. If the routine is restarted with
lwrk set to the minimum value requested, a larger request may be made at a later stage of the computation. See
Section 5 for the upper bound on
lwrk. On soft failure, the minimum requested value for
lwrk is returned in
and the safe value for
lwrk is returned in
.
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.
If
,
,
or
, a spline approximation is returned, but it fails to satisfy the fitting criterion (see
(2) and
(3) in
Section 3 – perhaps only by a small amount, however.
7
Accuracy
On successful exit, the approximation returned is such that its weighted sum of squared residuals
fp is equal to the smoothing factor
, up to a specified relative tolerance of
– except that if
and
,
fp may be significantly less than
: in this case the computed spline is simply the least squares bicubic polynomial approximation of degree
, i.e., a spline with no interior knots.
8
Parallelism and Performance
e02ddf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
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.
The time taken for a call of e02ddf depends on the complexity of the shape of the data, the value of the smoothing factor , and the number of data points. If e02ddf is to be called for different values of , much time can be saved by setting after the first call.
It should be noted that choosing very small considerably increases computation time.
If the weights have been correctly chosen (see
Section 2.1.2 in the E02 Chapter Introduction), the standard deviation of
would be the same for all
, equal to
, say. In this case, choosing the smoothing factor
in the range
, as suggested by
Reinsch (1967), is likely to give a good start in the search for a satisfactory value. Otherwise, experimenting with different values of
will be required from the start.
In that case, in view of computation time and memory requirements, it is recommended to start with a very large value for
and so determine the least squares bicubic polynomial; the value returned for
fp, call it
, gives an upper bound for
. Then progressively decrease the value of
to obtain closer fits – say by a factor of
in the beginning, i.e.,
,
, and so on, and more carefully as the approximation shows more details.
To choose
very small is strongly discouraged. This considerably increases computation time and memory requirements. It may also cause rank-deficiency (as indicated by the argument
rank) and endanger numerical stability.
The number of knots of the spline returned, and their location, generally depend on the value of and on the behaviour of the function underlying the data. However, if e02ddf is called with , the knots returned may also depend on the smoothing factors of the previous calls. Therefore if, after a number of trials with different values of and , a fit can finally be accepted as satisfactory, it may be worthwhile to call e02ddf once more with the selected value for but now using . Often, e02ddf then returns an approximation with the same quality of fit but with fewer knots, which is therefore better if data reduction is also important.
The number of knots may also depend on the upper bounds
nxest and
nyest. Indeed, if at a certain stage in
e02ddf the number of knots in one direction (say
) has reached the value of its upper bound (
nxest), then from that moment on all subsequent knots are added in the other
direction. This may indicate that the value of
nxest is too small. On the other hand, it gives you the option of limiting the number of knots the routine locates in any direction. For example, by setting
(the lowest allowable value for
nxest), you can indicate that you want an approximation which is a simple cubic polynomial in the variable
.
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 denotes the enlarged rectangle.
First suitable knot sets are built up in stages (starting with no interior knots in the case of a cold start but with the knot set found in a previous call if a warm start is chosen). At each stage, a bicubic spline is fitted to the data by least squares and
, the sum of squares of residuals, is computed. If
, a new knot is added to one knot set or the other so as to reduce
at the next stage. The new knot is located in an interval where the fit is particularly poor. Sooner or later, we find that
and at that point the knot sets are accepted. The routine then goes on to compute a spline which has these knot sets and which satisfies the full fitting criterion specified by
(2) and
(3). The theoretical solution has
. The routine computes the spline by an iterative scheme which is ended when
within a relative tolerance of
. The main part of each iteration consists of a linear least squares computation of special form, done in a similarly stable and efficient manner as in
e02daf. As there also, the minimal least squares solution is computed wherever the linear system is found to be rank-deficient.
An exception occurs when the routine finds at the start that, even with no interior knots (), the least squares spline already has its sum of squares of residuals . In this case, since this spline (which is simply a bicubic polynomial) also has an optimal value for the smoothness measure , namely zero, it is returned at once as the (trivial) solution. It will usually mean that has been chosen too large.
For further details of the algorithm and its use see
Dierckx (1981b).
The values of the computed spline at the points
, for
, may be obtained in the real array
ff (see
e02def), of length at least
, by the following call:
ifail = 0
Call e02def(n,nx,ny,x,y,lamda,mu,c,ff,wrk,iwrk,ifail)
where
and the coordinates
,
are stored in
,
.
PX and
PY have the same values as
nx and
ny as output from
e02ddf, and
LAMDA,
MU and
C have the same values as
lamda,
mu and
c output from
e02ddf.
WRK is a real workspace array of length at least
, and
IWRK is an integer workspace array of length at least
.
To evaluate the computed spline on a
by
rectangular grid of points in the
-
plane, which is defined by the
coordinates stored in
, for
, and the
coordinates stored in
, for
, returning the results in the real array
ff (see
e02dff) which is of length at least
, the following call may be used:
ifail = 0
Call e02dff(kx,ky,nx,ny,tx,ty,lamda,mu,c,fg,wrk,lwrk,
* iwrk,liwrk,ifail)
where
,
.
NX,
NY,
LAMDA,
MU and
C have the same values as
nx,
ny,
lamda,
mu and
c output from
e02ddf.
WRK is a real workspace array of length at least
, where
and
.
IWRK is an integer workspace array of length at least
if
, or
otherwise.
The result of the spline evaluated at grid point is returned in element of the array FG.
10
Example
This example reads in a value of
m, followed by a set of
m data points
and their weights
. It then calls
e02ddf to compute a bicubic spline approximation for one specified value of
s, and prints the values of the computed knots and B-spline coefficients. Finally it evaluates the spline at a small sample of points on a rectangular grid.
10.1
Program Text
Program Text (e02ddfe.f90)
10.2
Program Data
Program Data (e02ddfe.d)
10.3
Program Results
Program Results (e02ddfe.r)