|
NAG[e02ddc] NAG[nag_2d_spline_fit_scat] - Least-squares bicubic spline fit with automatic knot placement, two variables (scattered data)
|
|
Calling Sequence
e02ddc(start, x, y, f, weights, s, nxest, nyest, fp, rank, warmstartinf, spline_data, 'm'=m, 'fail'=fail)
nag_2d_spline_fit_scat(. . .)
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 and c
|
|
(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..m, datatype=float[8]);
y - Vector(1..m, datatype=float[8]);
f - Vector(1..m, datatype=float[8]);
|
|
|
|
weights - Vector(1..m, datatype=float[8]);
|
|
|
Constraint: the number of data points with non-zero weight must be at least 16 .
|
|
|
s - float;
|
|
|
On entry: the smoothing factor, . For advice on the choice of , see Section [Description] and Section [Choice of S ].
|
|
Constraint: . .
|
|
|
nxest - integer;
nyest - integer;
|
|
|
Constraint: and . .
|
|
|
fp - assignable;
|
|
|
Note: On exit the variable fp will have a value of type float.
|
|
|
rank - assignable;
|
|
|
Note: On exit the variable rank will have a value of type integer.
|
|
On exit: rank 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 .
|
|
|
warmstartinf - assignable;
|
|
|
Note: On exit the variable warmstartinf will have a value of type float.
|
|
On exit: if the warm start option is used, its value must be left unchanged from the previous call.
|
|
|
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.
|
|
|
'm'=m - integer; (optional)
|
|
|
Default value: the first dimension of the arrays x, y, f, weights.
|
|
On entry: , the number of data points.
|
|
The number of data points with non-zero weight (see weights below) must be at least 16.
|
|
|
'fail'=fail - table; (optional)
|
|
|
The NAG error argument, see the documentation for NagError.
|
|
|
|
Description
|
|
|
Purpose
|
|
nag_2d_spline_fit_scat (e02ddc) 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.
|
|
Description
|
|
nag_2d_spline_fit_scat (e02ddc) 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
(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:
minimize
(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 (1981b) for details). denotes the weighted 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 method would return an interpolating spline if were 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 nxest and nyest ] for advice on choice of .) Note however, that this function, unlike e02bec (nag_1d_spline_fit) and e02dcc (nag_2d_spline_fit_grid), does not allow to be set exactly to zero.
The method employed is outlined in Section [Outline of Method Used ] 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 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_SCAT, NE_NUM_COEFF_GT, NE_NO_ADDITIONAL_KNOTS 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_ALL_ELEMENTS_EQUAL"
On entry, all the values in the array x must not be equal.
"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, nxest must not be less than 8: .
"NE_NO_ADDITIONAL_KNOTS"
No more knots added; the additional knot would coincide with an old one. Possibly an inaccurate data point has too large a weight, or s is too small. .
"NE_NON_ZERO_WEIGHTS"
On entry, the number of data points with non-zero weights . Constraint: the number of non-zero weights .
"NE_NUM_COEFF_GT"
No more knots can be added because the number of B-spline coefficients already exceeds m. Either m or s is probably too small: , .
"NE_NUM_KNOTS_2D_GT_SCAT"
The number of knots required is greater than allowed by nxest or nyest, , . Possibly s is too small, especially if nxest, . , .
"NE_REAL_ARG_LE"
On entry, s must not be less than or equal to 0.0: .
"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_scat (e02ddc) 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_scat (e02ddc) 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.
|
|
Choice of
|
|
If the weights have been correctly chosen (see the 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 10 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 nag_2d_spline_fit_scat (e02ddc) 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_scat (e02ddc) once more with the selected value for but now using . Often, nag_2d_spline_fit_scat (e02ddc) 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
|
|
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 function 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 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. The minimal least-squares solution is computed wherever the linear system is found to be rank-deficient.
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 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).
|
|
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 n, 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_scat (e02ddc).
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 kx, and the co-ordinates stored in , for ky, 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_scat (e02ddc). The result of the spline evaluated at grid point is returned in element of the array fg.
|
|
|
|
Examples
|
|
>
|
start := "Nag_Cold":
m := 30:
s := 10:
nxest := 14:
nyest := 14:
spline_data := NAG[Nag_2dSpline]():
x := Vector([11.16, 12.85, 19.85, 19.72, 15.91, 0, 20.87, 3.45, 14.26, 17.43, 22.8, 7.58, 25, 0, 9.66, 5.22, 17.25, 25, 12.13, 22.23, 11.52, 15.2, 7.54, 17.32, 2.14, 0.51, 22.69, 5.47, 21.67, 3.31], datatype=float[8]):
y := Vector([1.24, 3.06, 10.72, 1.39, 7.74, 20, 20, 12.78, 17.87, 3.46, 12.39, 1.98, 11.87, 0, 20, 14.66, 19.57, 3.87, 10.79, 6.21, 8.529999999999999, 0, 10.69, 13.78, 15.03, 8.369999999999999, 19.63, 17.13, 14.36, 0.33], datatype=float[8]):
f := Vector([22.15, 22.11, 7.97, 16.83, 15.3, 34.6, 5.74, 41.24, 10.74, 18.6, 5.47, 29.87, 4.4, 58.2, 4.73, 40.36, 6.43, 8.74, 13.71, 10.25, 15.74, 21.6, 19.31, 12.11, 53.1, 49.43, 3.25, 28.63, 5.52, 44.08], datatype=float[8]):
weights := Vector([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], datatype=float[8]):
NAG:-e02ddc(start, x, y, f, weights, s, nxest, nyest, fp, rank, warmstartinf, spline_data, 'm' = m):
start := "Nag_Warm":
S := 5:
NAG:-e02ddc(start, x, y, f, weights, s, nxest, nyest, fp, rank, warmstartinf, spline_data, 'm' = m):
|
|
|
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 (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
e02 Chapter Introduction.
NAG Toolbox Overview.
NAG Web Site.
|
|