|
NAG[e02dcc] NAG[nag_2d_spline_fit_grid] - Least-squares bicubic spline fit with automatic knot placement, two variables (rectangular grid)
|
|
Calling Sequence
e02dcc(start, x, y, f, s, nxest, nyest, fp, spline_data, 'mx'=mx, 'my'=my, 'warmstartinf'=warmstartinf, 'fail'=fail)
nag_2d_spline_fit_grid(. . .)
Parameters
|
start - String;
|
|
|
On entry: start must be set to "Nag_Cold" or "Nag_Warm".
|
|
, (cold start) The function will build up the knot set starting with no interior knots. No values need be assigned to nx and ny and memory will be internally allocated to lamda, mu, c, nag_w and nag_iw.
|
|
(warm start) The function will restart the knot-placing strategy using the knots found in a previous call of the function. In this case, all arguments except s must be unchanged from that previous call. This warm start can save much time in searching for a satisfactory value of .
|
|
Constraint: "Nag_Cold" or "Nag_Warm". .
|
|
|
x - Vector(1..mx, datatype=float[8]);
|
|
|
Constraint: . .
|
|
|
y - Vector(1..my, datatype=float[8]);
|
|
|
Constraint: . .
|
|
|
f - Vector(1.. , datatype=float[8]);
|
|
|
|
s - float;
|
|
|
On entry: the smoothing factor, .
|
|
If , the function returns an interpolating spline.
|
|
If is smaller than machine precision, it is assumed equal to zero.
|
|
Constraint: . .
|
|
|
nxest - integer;
nyest - integer;
|
|
|
Constraint: and . .
|
|
|
fp - assignable;
|
|
|
Note: On exit the variable fp will have a value of type float.
|
|
|
spline_data - table;
|
|
|
A Maple table, which should be generated using NAG[Nag_2dSpline], corresponding to the Nag_2dSpline structure.
|
|
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.
|
|
On entry: a pointer to which if , memory of size nxest is internally allocated. If the warm start option is used, the values must be left unchanged from the previous call.
|
|
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.
|
|
On entry: a pointer to which if , memory of size nyest is internally allocated. If the warm start option is used, the values must be left unchanged from the previous call.
|
|
|
'mx'=mx - integer; (optional)
|
|
|
Default value: the first dimension of the array x.
|
|
On entry: , the number of grid points along the axis.
|
|
Constraint: . .
|
|
|
'my'=my - integer; (optional)
|
|
|
Default value: the first dimension of the array y.
|
|
On entry: , the number of grid points along the axis.
|
|
Constraint: . .
|
|
|
'warmstartinf'=warmstartinf - table; (optional)
|
|
|
A Maple table, which should be generated using NAG[Nag_Comm], corresponding to the Nag_Comm structure.
|
|
|
'fail'=fail - table; (optional)
|
|
|
The NAG error argument, see the documentation for NagError.
|
|
|
|
Description
|
|
|
Purpose
|
|
nag_2d_spline_fit_grid (e02dcc) computes a bicubic spline approximation to a set of data values, given on a rectangular grid in the - plane. 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.
|
|
Description
|
|
nag_2d_spline_fit_grid (e02dcc) determines a smooth bicubic spline approximation to the set of data points , for and .
The spline is given in the B-spline representation
(1)
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 function. The knots and are the interior knots; they divide the approximation domain into subpanels , for and . Then, much as in the curve case (see e02bec (nag_1d_spline_fit)), the coefficients are determined as the solution of the following constrained minimization problem:
(2)
subject to the constraint
(3)
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 (1982) for details). denotes the residual , and is a non-negative number to be specified by the user.
By means of the argument , "the smoothing factor", the user 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 function will return an interpolating spline if is set to zero, and 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 [Choice of S ] for advice on choice of .)
The method employed is outlined in Section [Outline of Method Used ] and fully described in Dierckx (1981a) and Dierckx (1982). 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 of the computed spline can subsequently be computed by calling e02dec (nag_2d_spline_eval) or e02dfc (nag_2d_spline_eval_rect) as described in Section [Evaluation of Computed Spline ].
|
|
Error Indicators and Warnings
|
|
|
If the function fails with an error exit of NE_NUM_KNOTS_2D_GT_RECT or NE_SPLINE_COEFF_CONV, then a spline approximation is returned, but it fails to satisfy the fitting criterion (see (2) and (3)) – perhaps by only a small amount, however.
|
"NE_ALLOC_FAIL"
Dynamic memory allocation failed.
"NE_BAD_PARAM"
On entry, argument start had an illegal value.
"NE_ENUMTYPE_WARM"
at the first call of this function. start must be set to at the first call.
"NE_INT_ARG_LT"
On entry, mx must not be less than 4: .
"NE_NOT_STRICTLY_INCREASING"
The sequence x is not strictly increasing: , . The sequence y is not strictly increasing: , .
"NE_NUM_KNOTS_2D_GT_RECT"
The number of knots required is greater than allowed by nxest or nyest, , . Possibly s is too small, especially if nxest, /2, my/2. , , .
"NE_REAL_ARG_LT"
On entry, s must not be less than 0.0: .
"NE_SF_D_K_CONS"
On entry, , , . Constraint: when .
"NE_SPLINE_COEFF_CONV"
The iterative process has failed to converge. Possibly s is too small: .
|
|
Further Comments
|
|
|
Timing
|
|
The time taken for a call of nag_2d_spline_fit_grid (e02dcc) depends on the complexity of the shape of the data, the value of the smoothing factor , and the number of data points. If nag_2d_spline_fit_grid (e02dcc) is to be called for different values of , much time can be saved by setting after the first call.
|
|
Weighting of Data Points
|
|
nag_2d_spline_fit_grid (e02dcc) does not allow individual weighting of the data values. If these were determined to widely differing accuracies, it may be better to use e02ddc (nag_2d_spline_fit_scat). The computation time would be very much longer, however.
|
|
Choice of
|
|
If the standard deviation of is the same for all and (the case for which this function is designed – see Section [Weighting of Data Points ]) and known to be equal, at least approximately, to , say, then following Reinsch (1967) and choosing the smoothing factor in the range , where , is likely to give a good start in the search for a satisfactory value. If the standard deviations vary, the sum of their squares over all the data points could be used. Otherwise experimenting with different values of will be required from the start, taking account of the remarks in Section [Description].
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 10 in the beginning, i.e., , , and so on, and more carefully as the approximation shows more details.
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 nag_2d_spline_fit_grid (e02dcc) 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 nag_2d_spline_fit_grid (e02dcc) once more with the selected value for but now using . Often, nag_2d_spline_fit_grid (e02dcc) then returns an approximation with the same quality of fit but with fewer knots, which is therefore better if data reduction is also important.
|
|
Outline of Method Used
|
|
If , the requisite number of knots is known in advance, i.e., and ; the interior knots are located immediately as and , for and . The corresponding least-squares spline is then an interpolating spline and therefore a solution of the problem.
If , 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 , new knots are added to one knot set or the other so as to reduce at the next stage. The new knots are located in intervals where the fit is particularly poor, their number depending on the value of and on the progress made so far in reducing . Sooner or later, we find that and at that point the knot sets are accepted. The function then goes on to compute the (unique) spline which has these knot sets and which satisfies the full fitting criterion specified by (2) and (3). The theoretical solution has . The function computes the spline by an iterative scheme which is ended when within a relative tolerance of 0.001. 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 e02bac (nag_1d_spline_fit_knots) for least-squares curve fitting.
An exception occurs when the function finds at the start that, even with no interior knots , the least-squares spline already has its sum 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 (1982).
|
|
Evaluation of Computed Spline
|
|
The values of the computed spline at the points , for may be obtained in the array ff, of length at least , by the following code:
e02dec(tx, ty, ff, spline,'m'=n)
where spline_data is a structure of type which is an output argument of nag_2d_spline_fit_grid (e02dcc).
To evaluate the computed spline on a kx by ky rectangular grid of points in the - plane, which is defined by the co-ordinates stored in , for , and the co-ordinates stored in , for , returning the results in the array fg which is of length at least , the following call may be used:
e02dfc(tx, ty, fg, spline,'mx'=kx,'my'=ky)
where spline_data is a structure of type which is an output argument of nag_2d_spline_fit_grid (e02dcc). The result of the spline evaluated at grid point is returned in element of the array fg.
|
|
|
|
Examples
|
|
>
|
start := "Nag_Cold":
mx := 11:
my := 9:
s := 0.1:
nxest := 15:
nyest := 13:
warmstartinf := NAG[Nag_Comm]():
spline_data := NAG[Nag_2dSpline]():
x := Vector([0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5], datatype=float[8]):
y := Vector([0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4], datatype=float[8]):
f := Vector([1, 0.88758, 0.5403, 0.07073699999999999, -0.41515, -0.80114, -0.97999, -0.93446, -0.65664, 1.5, 1.3564, 0.82045, 0.10611, -0.62422, -1.2317, -1.485, -1.3047, -0.98547, 2.06, 1.7552, 1.0806, 0.15147, -0.83229, -1.6023, -1.97, -1.8729, -1.4073, 2.57, 2.124, 1.3508, 0.17684, -1.0404, -2.0029, -2.475, -2.3511, -1.6741, 3, 2.6427, 1.6309, 0.21221, -1.2484, -2.2034, -2.97, -2.8094, -1.9809, 3.5, 3.1715, 1.8611, 0.24458, -1.4565, -2.864, -3.265, -3.2776, -2.2878, 4.04, 3.5103, 2.0612, 0.28595, -1.6946, -3.2046, -3.96, -3.7958, -2.6146, 4.5, 3.9391, 2.4314, 0.31632, -1.8627, -3.6351, -4.455, -4.2141, -2.9314, 5.04, 4.3879, 2.7515, 0.35369, -2.0707, -4.0057, -4.97, -4.6823, -3.2382, 5.505, 4.8367, 2.9717, 0.38505, -2.2888, -4.4033, -5.445, -5.1405, -3.595, 6, 5.2755, 3.2418, 0.42442, -2.4769, -4.8169, -5.93, -5.6387, -3.9319], datatype=float[8]):
NAG:-e02dcc(start, x, y, f, s, nxest, nyest, fp, spline_data, 'mx' = mx, 'my' = my, 'warmstartinf' = warmstartinf):
|
|
|
See Also
|
|
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 (1982) A fast algorithm for smoothing data on a rectangular grid while using spline functions SIAM J. Numer. Anal. 19 1286–1304
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
Reinsch C H (1967) Smoothing by spline functions Numer. Math. 10 177–183
e02 Chapter Introduction.
NAG Toolbox Overview.
NAG Web Site.
|
|