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.
The routine may be called by the names e02ddf or nagf_fit_dim2_spline_sctr.
3Description
e02ddf determines a smooth bicubic spline approximation $s(x,y)$ to the set of data points $({x}_{\mathit{r}},{y}_{\mathit{r}},{f}_{\mathit{r}})$ with weights ${\mathit{w}}_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,m$.
The approximation domain is considered to be the rectangle $[{x}_{\mathrm{min}},{x}_{\mathrm{max}}]\times [{y}_{\mathrm{min}},{y}_{\mathrm{max}}]$, where ${x}_{\mathrm{min}}\left({y}_{\mathrm{min}}\right)$ and ${x}_{\mathrm{max}}\left({y}_{\mathrm{max}}\right)$ denote the lowest and highest data values of $x\left(y\right)$.
The spline is given in the B-spline representation
where ${M}_{i}\left(x\right)$ and ${N}_{j}\left(y\right)$ denote normalized cubic B-splines, the former defined on the knots ${\lambda}_{i}$ to ${\lambda}_{i+4}$ and the latter on the knots ${\mu}_{j}$ to ${\mu}_{j+4}$. For further details, see Hayes and Halliday (1974) for bicubic splines and de Boor (1972) for normalized B-splines.
The total numbers ${n}_{x}$ and ${n}_{y}$ of these knots and their values ${\lambda}_{1},\dots ,{\lambda}_{{n}_{x}}$ and ${\mu}_{1},\dots ,{\mu}_{{n}_{y}}$ are chosen automatically by the routine. The knots ${\lambda}_{5},\dots ,{\lambda}_{{n}_{x}-4}$ and ${\mu}_{5},\dots ,{\mu}_{{n}_{y}-4}$ are the interior knots; they divide the approximation domain $[{x}_{\mathrm{min}},{x}_{\mathrm{max}}]\times [{y}_{\mathrm{min}},{y}_{\mathrm{max}}]$ into $({n}_{x}-7)\times ({n}_{y}-7)$ subpanels $[{\lambda}_{\mathit{i}},{\lambda}_{\mathit{i}+1}]\times [{\mu}_{\mathit{j}},{\mu}_{\mathit{j}+1}]$, for $\mathit{i}=4,5,\dots ,{n}_{x}-4$ and $\mathit{j}=4,5,\dots ,{n}_{y}-4$. Then, much as in the curve case (see e02bef), the coefficients ${c}_{ij}$ are determined as the solution of the following constrained minimization problem:
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 (1981b) for details).
${\epsilon}_{r}$
denotes the weighted residual
${\mathit{w}}_{r}({f}_{r}-s({x}_{r},{y}_{r}))$,
and
${\mathbf{s}}$
is a non-negative number to be specified by you.
By means of the argument ${\mathbf{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 ${\mathbf{s}}$ is too large, the spline will be too smooth and signal will be lost (underfit); if ${\mathbf{s}}$ is too small, the spline will pick up too much noise (overfit). In the extreme cases the method would return an interpolating spline $(\theta =0)$ if ${\mathbf{s}}$ were set to zero, and returns the least squares bicubic polynomial $(\eta =0)$ if ${\mathbf{s}}$ is set very large. Experimenting with ${\mathbf{s}}$-values between these two extremes should result in a good compromise. (See Section 9.2 for advice on choice of ${\mathbf{s}}$.) Note however, that this routine, unlike e02befande02dcf, does not allow ${\mathbf{s}}$ to be set exactly to zero: to compute an interpolant to scattered data, e01safore01sgf 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 ${\mathbf{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,e02dffore02dhf as described in Section 9.6.
4References
de Boor C (1972) On calculating with B-splines J. Approx. Theory6 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
5Arguments
1: $\mathbf{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).
${\mathbf{start}}=\text{'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 or wrk.
${\mathbf{start}}=\text{'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 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:
${\mathbf{start}}=\text{'C'}$ or $\text{'W'}$.
2: $\mathbf{m}$ – IntegerInput
On entry: $m$, the number of data points.
The number of data points with nonzero weight (see w) must be at least $16$.
3: $\mathbf{x}\left({\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayInput
4: $\mathbf{y}\left({\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayInput
5: $\mathbf{f}\left({\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: ${\mathbf{x}}\left(\mathit{r}\right)$, ${\mathbf{y}}\left(\mathit{r}\right)$, ${\mathbf{f}}\left(\mathit{r}\right)$ must be set to the coordinates of $({x}_{\mathit{r}},{y}_{\mathit{r}},{f}_{\mathit{r}})$, the $\mathit{r}$th data point, for $\mathit{r}=1,2,\dots ,m$. The order of the data points is immaterial.
6: $\mathbf{w}\left({\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: ${\mathbf{w}}\left(\mathit{r}\right)$ must be set to ${\mathit{w}}_{\mathit{r}}$, the $\mathit{r}$th value in the set of weights, for $\mathit{r}=1,2,\dots ,m$. Zero weights are permitted and the corresponding points are ignored, except when determining ${x}_{\mathrm{min}}$, ${x}_{\mathrm{max}}$, ${y}_{\mathrm{min}}$ and ${y}_{\mathrm{max}}$ (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 $16$.
On entry: an upper bound for the number of knots ${n}_{x}$ and ${n}_{y}$ required in the $x$- and $y$-directions respectively.
In most practical situations, ${\mathbf{nxest}}={\mathbf{nyest}}=4+\sqrt{m/2}$ is sufficient. See also Section 9.3.
Constraint:
${\mathbf{nxest}}\ge 8$ and ${\mathbf{nyest}}\ge 8$.
10: $\mathbf{nx}$ – 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, ${n}_{x}$, of the computed spline with respect to the $x$ variable.
11: $\mathbf{lamda}\left({\mathbf{nxest}}\right)$ – Real (Kind=nag_wp) arrayInput/Output
On entry: if the warm start option is used, the values ${\mathbf{lamda}}\left(1\right),{\mathbf{lamda}}\left(2\right),\dots ,{\mathbf{lamda}}\left({\mathbf{nx}}\right)$ must be left unchanged from the previous call.
On exit: contains the complete set of knots ${\lambda}_{i}$ associated with the $x$ variable, i.e., the interior knots ${\mathbf{lamda}}\left(5\right),{\mathbf{lamda}}\left(6\right),\dots ,{\mathbf{lamda}}\left({\mathbf{nx}}-4\right)$ as well as the additional knots
needed for the B-spline representation (where ${x}_{\mathrm{min}}$ and ${x}_{\mathrm{max}}$ are as described in Section 3).
12: $\mathbf{ny}$ – 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, ${n}_{y}$, of the computed spline with respect to the $y$ variable.
13: $\mathbf{mu}\left({\mathbf{nyest}}\right)$ – Real (Kind=nag_wp) arrayInput/Output
On entry: if the warm start option is used, the values ${\mathbf{mu}}\left(1\right),{\mathbf{mu}}\left(2\right),\dots ,{\mathbf{mu}}\left({\mathbf{ny}}\right)$ must be left unchanged from the previous call.
On exit: contains the complete set of knots ${\mu}_{i}$ associated with the $y$ variable, i.e., the interior knots ${\mathbf{mu}}\left(5\right),{\mathbf{mu}}\left(6\right),\dots ,{\mathbf{mu}}\left({\mathbf{ny}}-4\right)$ as well as the additional knots
needed for the B-spline representation (where ${y}_{\mathrm{min}}$ and ${y}_{\mathrm{max}}$ are as described in Section 3).
14: $\mathbf{c}\left(({\mathbf{nxest}}-4)\times ({\mathbf{nyest}}-4)\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: the coefficients of the spline approximation. ${\mathbf{c}}\left(({n}_{y}-4)\times (i-1)+j\right)$ is the coefficient ${c}_{ij}$ defined in Section 3.
15: $\mathbf{fp}$ – Real (Kind=nag_wp)Output
On exit: the weighted sum of squared residuals, $\theta $, of the computed spline approximation. fp should equal s within a relative tolerance of $0.001$ unless ${\mathbf{nx}}={\mathbf{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: $\mathbf{rank}$ – 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 ${\mathbf{rank}}=({\mathbf{nx}}-4)\times ({\mathbf{ny}}-4)$, 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: $\mathbf{wrk}\left({\mathbf{lwrk}}\right)$ – Real (Kind=nag_wp) arrayCommunication Array
If the warm start option is used, on entry, the value of ${\mathbf{wrk}}\left(1\right)$ must be left unchanged from the previous call.
This array is used as workspace.
18: $\mathbf{lwrk}$ – IntegerInput
On entry: the dimension of the array wrk as declared in the (sub)program from which e02ddf is called.
Constraint:
${\mathbf{lwrk}}\ge (7\times \mathit{u}\times \mathit{v}+25\times \mathit{w})\times (\mathit{w}+1)+2\times (\mathit{u}+\mathit{v}+4\times {\mathbf{m}})+23\times \mathit{w}+56$, where $\mathit{u}={\mathbf{nxest}}-4$, $\mathit{v}={\mathbf{nyest}}-4$ and $\mathit{w}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}(\mathit{u},\mathit{v})$.
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 $4\times \mathit{u}\times \mathit{v}\times \mathit{w}+2\times \mathit{u}\times \mathit{v}+4\times \mathit{w}$, where $\mathit{u}$, $\mathit{v}$ and $\mathit{w}$ 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 ($\mathit{lwrk2}=0$).
On entry: ifail must be set to $0$, $\mathrm{-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 $\mathrm{-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 $\mathrm{-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 $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).
6Error Indicators and Warnings
If on entry ${\mathbf{ifail}}=0$ or $\mathrm{-1}$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{liwrk}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{liwrk}}\ge \u27e8\mathit{\text{value}}\u27e9$.
On entry, ${\mathbf{lwrk}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{lwrk}}\ge \u27e8\mathit{\text{value}}\u27e9$.
On entry, $\mathrm{MM}<16$, where $\mathrm{MM}$ is the number of points with nonzero weight: $\mathrm{MM}=\u27e8\mathit{\text{value}}\u27e9$.
On entry, ${\mathbf{nxest}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{nxest}}\ge \u27e8\mathit{\text{value}}\u27e9$.
On entry, ${\mathbf{nyest}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{nyest}}\ge \u27e8\mathit{\text{value}}\u27e9$.
On entry, ${\mathbf{s}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{s}}>0.0$.
On entry, ${\mathbf{start}}\ne \text{'W'}$ or $\text{'C'}$: ${\mathbf{start}}=\u27e8\mathit{\text{value}}\u27e9$.
The number of knots needed in one direction is greater than nxest or nyest: nxest, ${\mathbf{nyest}}=\u27e8\mathit{\text{value}}\u27e9,\u27e8\mathit{\text{value}}\u27e9$. Possibly s is too small: ${\mathbf{s}}=\u27e8\mathit{\text{value}}\u27e9$.
${\mathbf{ifail}}=4$
No more knots added; the number of $B$-spline coefficients already exceeds m. Either m or s is probably too small: ${\mathbf{m}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{s}}=\u27e8\mathit{\text{value}}\u27e9$.
${\mathbf{ifail}}=5$
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: ${\mathbf{s}}=\u27e8\mathit{\text{value}}\u27e9$.
${\mathbf{ifail}}=6$
The iterative process has failed to converge. Possibly s is too small: ${\mathbf{s}}=\u27e8\mathit{\text{value}}\u27e9$.
${\mathbf{ifail}}=7$
lwrk is too small to compute the minimal least squares solution: ${\mathbf{lwrk}}=\u27e8\mathit{\text{value}}\u27e9$. Minimum requested value for lwrk is $\u27e8\mathit{\text{value}}\u27e9$; Safe requested value for lwrk is $\u27e8\mathit{\text{value}}\u27e9$.
${\mathbf{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.
${\mathbf{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.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.
7Accuracy
On successful exit, the approximation returned is such that its weighted sum of squared residuals fp is equal to the smoothing factor ${\mathbf{s}}$, up to a specified relative tolerance of $0.001$ – except that if ${n}_{x}=8$ and ${n}_{y}=8$, fp may be significantly less than ${\mathbf{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.
8Parallelism 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.
9Further Comments
9.1Timing
The time taken for a call of e02ddf depends on the complexity of the shape of the data, the value of the smoothing factor ${\mathbf{s}}$, and the number of data points. If e02ddf is to be called for different values of ${\mathbf{s}}$, much time can be saved by setting ${\mathbf{start}}=\text{'W'}$ after the first call.
It should be noted that choosing ${\mathbf{s}}$ 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 ${\mathit{w}}_{r}{f}_{r}$ would be the same for all $r$, equal to $\sigma $, say. In this case, choosing the smoothing factor ${\mathbf{s}}$ in the range ${\sigma}^{2}(m\pm \sqrt{2m})$, 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 ${\mathbf{s}}$ 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 ${\mathbf{s}}$ and so determine the least squares bicubic polynomial; the value returned for fp, call it ${{\mathbf{fp}}}_{0}$, gives an upper bound for ${\mathbf{s}}$. Then progressively decrease the value of ${\mathbf{s}}$ to obtain closer fits – say by a factor of $10$ in the beginning, i.e., ${\mathbf{s}}={{\mathbf{fp}}}_{0}/10$, ${\mathbf{s}}={{\mathbf{fp}}}_{0}/100$, and so on, and more carefully as the approximation shows more details.
To choose ${\mathbf{s}}$ 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 ${\mathbf{s}}$ and on the behaviour of the function underlying the data. However, if e02ddf is called with ${\mathbf{start}}=\text{'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 ${\mathbf{s}}$ and ${\mathbf{start}}=\text{'W'}$, a fit can finally be accepted as satisfactory, it may be worthwhile to call e02ddf once more with the selected value for ${\mathbf{s}}$ but now using ${\mathbf{start}}=\text{'C'}$. 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 ${n}_{x}$) has reached the value of its upper bound (nxest), then from that moment on all subsequent knots are added in the other $\left(y\right)$ 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 ${\mathbf{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.4Restriction of the approximation domain
The fit obtained is not defined outside the rectangle $[{\lambda}_{4},{\lambda}_{{n}_{x}-3}]\times [{\mu}_{4},{\mu}_{{n}_{y}-3}]$. The reason for taking the extreme data values of $x$ and $y$ 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 $(a,c,0)$ and $(b,d,0)$ with zero weight, where $[a,b]\times [c,d]$ denotes the enlarged rectangle.
9.5Outline 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 $\theta $, the sum of squares of residuals, is computed. If $\theta >{\mathbf{s}}$, a new knot is added to one knot set or the other so as to reduce $\theta $ at the next stage. The new knot is located in an interval where the fit is particularly poor. Sooner or later, we find that $\theta \le {\mathbf{s}}$ 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 $\theta ={\mathbf{s}}$. The routine computes the spline by an iterative scheme which is ended when $\theta ={\mathbf{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 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 ($\mathrm{N}=8$), the least squares spline already has its sum of squares of residuals $\text{}\le {\mathbf{s}}$. In this case, since this spline (which is simply a bicubic polynomial) also has an optimal value for the smoothness measure $\eta $, namely zero, it is returned at once as the (trivial) solution. It will usually mean that ${\mathbf{s}}$ has been chosen too large.
For further details of the algorithm and its use see Dierckx (1981b).
9.6Evaluation of Computed Spline
Values of the computed spline evaluated at a set of points, or on a rectangular grid, can be obtained by calls of e02def or e02dff respectively.
10Example
This example reads in a value of m, followed by a set of m data points $({x}_{r},{y}_{r},{f}_{r})$ and their weights ${\mathit{w}}_{r}$. 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.