NAG FL Interface
e02dcf (dim2_​spline_​grid)

Settings help

FL Name Style:


FL Specification Language:


1 Purpose

e02dcf computes a bicubic spline approximation to a set of data values, given on a rectangular grid in the x-y 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.

2 Specification

Fortran Interface
Subroutine e02dcf ( start, mx, x, my, y, f, s, nxest, nyest, nx, lamda, ny, mu, c, fp, wrk, lwrk, iwrk, liwrk, ifail)
Integer, Intent (In) :: mx, my, nxest, nyest, lwrk, liwrk
Integer, Intent (Inout) :: nx, ny, iwrk(liwrk), ifail
Real (Kind=nag_wp), Intent (In) :: x(mx), y(my), f(mx*my), s
Real (Kind=nag_wp), Intent (Inout) :: 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 <nag.h>
void  e02dcf_ (const char *start, const Integer *mx, const double x[], const Integer *my, const double y[], const double f[], const double *s, const Integer *nxest, const Integer *nyest, Integer *nx, double lamda[], Integer *ny, double mu[], double c[], double *fp, double wrk[], const Integer *lwrk, Integer iwrk[], const Integer *liwrk, Integer *ifail, const Charlen length_start)
The routine may be called by the names e02dcf or nagf_fit_dim2_spline_grid.

3 Description

e02dcf determines a smooth bicubic spline approximation s(x,y) to the set of data points (xq,yr,fq,r), for q=1,2,,mx and r=1,2,,my.
The spline is given in the B-spline representation
s(x,y) = i=1 nx-4 j=1 ny-4 cij Mi(x) Nj(y) , (1)
where Mi(x) and Nj(y) denote normalized cubic B-splines, the former defined on the knots λi to λi+4 and the latter on the knots μj to μj+4. For further details, see Hayes and Halliday (1974) for bicubic splines and de Boor (1972) for normalized B-splines.
The total numbers nx and ny of these knots and their values λ1,,λnx and μ1,,μny are chosen automatically by the routine. The knots λ5,,λnx-4 and μ5,,μny-4 are the interior knots; they divide the approximation domain [x1,xmx]×[y1,ymy] into (nx-7)×(ny-7) subpanels [λi,λi+1]×[μj,μj+1], for i=4,5,,nx-4 and j=4,5,,ny-4. Then, much as in the curve case (see e02bef), the coefficients cij are determined as the solution of the following constrained minimization problem:
minimizeη , (2)
subject to the constraint
θ = q=1 mx r=1 my ε q,r 2 S , (3)
where η is a measure of the (lack of) smoothness of s(x,y). Its value depends on the discontinuity jumps in s(x,y) 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).
εq,r denotes the residual fq,r-s(xq,yr),
and S is a non-negative number specified by you.
By means of the argument S, ‘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 S is too large, the spline will be too smooth and signal will be lost (underfit); if S is too small, the spline will pick up too much noise (overfit). In the extreme cases the routine will return an interpolating spline (θ=0) if S is set to zero, and the least squares bicubic polynomial (η=0) if S is set very large. Experimenting with S-values between these two extremes should result in a good compromise. (See Section 9.3 for advice on choice of S.)
The method employed is outlined in Section 9.5 and fully described in Dierckx (1981) 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 S), 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 (1981) 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

5 Arguments

1: start 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).
start='C'
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, wrk or iwrk.
start='W'
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, wrk and iwrk 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: start='C' or 'W'.
2: mx Integer Input
On entry: mx, the number of grid points along the x axis.
Constraint: mx4.
3: x(mx) Real (Kind=nag_wp) array Input
On entry: x(q) must be set to xq, the x coordinate of the qth grid point along the x axis, for q=1,2,,mx.
Constraint: x1<x2<<xmx.
4: my Integer Input
On entry: my, the number of grid points along the y axis.
Constraint: my4.
5: y(my) Real (Kind=nag_wp) array Input
On entry: y(r) must be set to yr, the y coordinate of the rth grid point along the y axis, for r=1,2,,my.
Constraint: y1<y2<<ymy.
6: f(mx×my) Real (Kind=nag_wp) array Input
On entry: f(my×(q-1)+r) must contain the data value fq,r, for q=1,2,,mx and r=1,2,,my.
7: s Real (Kind=nag_wp) Input
On entry: the smoothing factor, S.
If s=0.0, the routine returns an interpolating spline.
If s is smaller than machine precision, it is assumed equal to zero.
For advice on the choice of s, see Sections 3 and 9.3.
Constraint: s0.0.
8: nxest Integer Input
9: nyest Integer Input
On entry: an upper bound for the number of knots nx and ny required in the x- and y-directions respectively.
In most practical situations, nxest=mx/2 and nyest=my/2 is sufficient. nxest and nyest never need to be larger than mx+4 and my+4 respectively, the numbers of knots needed for interpolation (s=0.0). See also Section 9.4.
Constraints:
  • nxest8;
  • nyest8.
10: nx Integer Input/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, nx, of the computed spline with respect to the x variable.
11: lamda(nxest) Real (Kind=nag_wp) array Input/Output
On entry: if the warm start option is used, the values lamda(1),lamda(2),,lamda(nx) must be left unchanged from the previous call.
On exit: contains the complete set of knots λi associated with the x variable, i.e., the interior knots lamda(5),lamda(6),,lamda(nx-4) as well as the additional knots
lamda(1)=lamda(2)=lamda(3)=lamda(4)=x(1)  
and
lamda(nx- 3)=lamda(nx- 2)=lamda(nx- 1)=lamda(nx)=x(mx)  
needed for the B-spline representation.
12: ny Integer Input/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, ny, of the computed spline with respect to the y variable.
13: mu(nyest) Real (Kind=nag_wp) array Input/Output
On entry: if the warm start option is used, the values mu(1),mu(2),,mu(ny) must be left unchanged from the previous call.
On exit: contains the complete set of knots μi associated with the y variable, i.e., the interior knots mu(5),mu(6),,mu(ny-4) as well as the additional knots
mu(1)=mu(2)=mu(3)=mu(4)=y(1)  
and
mu(ny- 3)=mu(ny- 2)=mu(ny- 1)=mu(ny)=y(my)  
needed for the B-spline representation.
14: c((nxest-4)×(nyest-4)) Real (Kind=nag_wp) array Output
On exit: the coefficients of the spline approximation. c((ny-4) × (i-1) +j ) is the coefficient cij defined in Section 3.
15: fp Real (Kind=nag_wp) Output
On exit: the sum of squared residuals, θ, of the computed spline approximation. If fp=0.0, this is an interpolating spline. fp should equal s within a relative tolerance of 0.001 unless nx=ny=8, 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: wrk(lwrk) Real (Kind=nag_wp) array Communication Array
If the warm start option is used, on entry, the values wrk(1),,wrk(4) must be left unchanged from the previous call.
This array is used as workspace.
17: lwrk Integer Input
On entry: the dimension of the array wrk as declared in the (sub)program from which e02dcf is called.
Constraint: lwrk4×(mx+my)+11×(nxest+nyest)+nxest×my+ max(my,nxest)+54.
18: iwrk(liwrk) Integer array Communication Array
If the warm start option is used, on entry, the values iwrk(1),,iwrk(3) must be left unchanged from the previous call.
This array is used as workspace.
19: liwrk Integer Input
On entry: the dimension of the array iwrk as declared in the (sub)program from which e02dcf is called.
Constraint: liwrk3+mx+my+nxest+nyest.
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
On entry, liwrk=value.
Constraint: liwrkvalue.
On entry, lwrk=value.
Constraint: lwrkvalue.
On entry, mx=value.
Constraint: mxvalue.
On entry, my=value.
Constraint: myvalue.
On entry, nxest=value, mx=value, nyest=value and my=value.
Constraint: if s=0.0, nxestmx+4 and nyestmy+4.
On entry, nxest=value.
Constraint: nxestvalue.
On entry, nyest=value.
Constraint: nyestvalue.
On entry, s=value.
Constraint: s0.0.
On entry, start'W' or 'C': start=value.
ifail=2
On entry, q=value, x(q-1)=value and x(q)=value.
Constraint: x(q-1)<x(q) for all q.
ifail=3
On entry, r=value, y(r-1)=value and y(r)=value.
Constraint: y(r-1)<y(r) for all r.
ifail=4
The number of knots needed in one direction is greater than nxest or nyest: nxest, nyest=value,value. Possibly s is too small: s=value.
ifail=5
The iterative process has failed to converge. Possibly s is too small: s=value.
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

On successful exit, the approximation returned is such that its sum of squared residuals fp is equal to the smoothing factor s, up to a specified relative tolerance of 0.001 – except that if nx=8 and ny=8, fp may be significantly less than s: in this case the computed spline is simply the least squares bicubic polynomial approximation of degree 3, i.e., a spline with no interior knots.

8 Parallelism and Performance

e02dcf 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.

9 Further Comments

9.1 Timing

The time taken for a call of e02dcf depends on the complexity of the shape of the data, the value of the smoothing factor S, and the number of data points. If e02dcf is to be called for different values of S, much time can be saved by setting start='W' after the first call.

9.2 Weighting of Data Points

e02dcf does not allow individual weighting of the data values. If these were determined to widely differing accuracies, it may be better to use e02ddf. The computation time would be very much longer, however.

9.3 Choice of s

If the standard deviation of fq,r is the same for all q and r (the case for which e02dcf is designed – see Section 9.2.) and known to be equal, at least approximately, to σ, say, then following Reinsch (1967) and choosing the argument s in the range σ2(m±2m), where m=mxmy, 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 s will be required from the start, taking account of the remarks in Section 3.
In that case, in view of computation time and memory requirements, it is recommended to start with a very large value for s and so determine the least squares bicubic polynomial; the value returned for fp, call it fp0, gives an upper bound for s. Then progressively decrease the value of s to obtain closer fits – say by a factor of 10 in the beginning, i.e., s=fp0/10, s=fp0/100, 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 s and on the behaviour of the function underlying the data. However, if e02dcf is called with start='W', 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 s and start='W', a fit can finally be accepted as satisfactory, it may be worthwhile to call e02dcf once more with the selected value for s but now using start='C'. Often, e02dcf then returns an approximation with the same quality of fit but with fewer knots, which is, therefore, better if data reduction is also important.

9.4 Choice of nxest and nyest

The number of knots may also depend on the upper bounds nxest and nyest. Indeed, if at a certain stage in e02dcf the number of knots in one direction (say nx) has reached the value of its upper bound (nxest), then from that moment on all subsequent knots are added in the other (y) direction. Therefore, you have the option of limiting the number of knots the routine locates in any direction. For example, by setting nxest=8 (the lowest allowable value for nxest), you can indicate that you want an approximation which is a simple cubic polynomial in the variable x.

9.5 Outline of Method Used

If S=0, the requisite number of knots is known in advance, i.e., nx=mx+4 and ny=my+4; the interior knots are located immediately as λi=xi-2 and μj=yj-2, for i=5,6,,nx-4 and j=5,6,,ny-4. The corresponding least squares spline is then an interpolating spline and, therefore, a solution of the problem.
If S>0, 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 θ>S, 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 S and on the progress made so far in reducing θ. Sooner or later, we find that θS and at that point the knot sets are accepted. The routine 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 θ=S. The routine computes the spline by an iterative scheme which is ended when θ=S 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 e02baf for least squares curve-fitting.
An exception occurs when the routine finds at the start that, even with no interior knots (nx=ny=8), the least squares spline already has its sum of residuals S. 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 S has been chosen too large.
For further details of the algorithm and its use see Dierckx (1982).

9.6 Evaluation of Computed Spline

The values of the computed spline at the points (xr,yr) , for r=1,2,,m, may be obtained in the real array ff (see e02def), of length at least m, by the following call:
ifail = 0
Call e02def(m,px,py,x,y,lamda,mu,c,ff,wrk,iwrk,ifail)
where M=m and the coordinates xr, yr are stored in X(k), Y(k). PX and PY have the same values as nx and ny as output from e02dcf, and LAMDA, MU and C have the same values as lamda, mu and c output from e02dcf. WRK is a real workspace array of length at least PY-4, and IWRK is an integer workspace array of length at least PY-4.
To evaluate the computed spline on a mx×my rectangular grid of points in the x-y plane, which is defined by the x coordinates stored in x(q), for q=1,2,,mx, and the y coordinates stored in y(r), for r=1,2,,my , returning the results in the real array ff (see e02dff) which is of length at least mx×my, the following call may be used:
 ifail = 0
 Call e02dff(mx,my,px,py,x,y,lamda,mu,c,fg,wrk,lwrk,
*            iwrk,liwrk,ifail)
where MX=mx, MY=my. PX and PY have the same values as nx and ny as output from e02dcf, and LAMDA, MU and C have the same values as lamda, mu and c output from e02dcf. WRK is a real workspace array of length at least LWRK = min(nwrk1,nwrk2) , where nwrk1=MX×4+PX and nwrk2=MY×4+PY. IWRK is an integer workspace array of length at least LIWRK=MY+PY-4 if nwrk1nwrk2, or MX+PX-4 otherwise.
The result of the spline evaluated at grid point (q,r) is returned in element (MY×(q-1)+r) of the array FG.

10 Example

This example reads in values of mx, my, xq, for q=1,2,,mx, and yr, for r=1,2,,my, followed by values of the ordinates fq,r defined at the grid points (xq,yr). It then calls e02dcf 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 (e02dcfe.f90)

10.2 Program Data

Program Data (e02dcfe.d)

10.3 Program Results

Program Results (e02dcfe.r)
GnuplotProduced by GNUPLOT 4.6 patchlevel 3 Example Program Calculation and Evaluation of Least Squares Bicubic Spline Fit gnuplot_plot_1 gnuplot_plot_2 0 1 2 3 4 5 y 0 0.5 1 1.5 2 2.5 3 3.5 4 x −6 −4 −2 0 2 4 6