PDF version (NAG web site
, 64bit version, 64bit version)
NAG Toolbox: nag_fit_2dspline_sctr (e02dd)
Purpose
nag_fit_2dspline_sctr (e02dd) computes a bicubic spline approximation to a set of scattered data. The knots of the spline are located automatically, but a single parameter must be specified to control the tradeoff between closeness of fit and smoothness of fit.
Syntax
[
nx,
lamda,
ny,
mu,
c,
fp,
rank,
wrk,
ifail] = e02dd(
start,
x,
y,
f,
w,
s,
nx,
lamda,
ny,
mu,
wrk, 'm',
m, 'nxest',
nxest, 'nyest',
nyest)
[
nx,
lamda,
ny,
mu,
c,
fp,
rank,
wrk,
ifail] = nag_fit_2dspline_sctr(
start,
x,
y,
f,
w,
s,
nx,
lamda,
ny,
mu,
wrk, 'm',
m, 'nxest',
nxest, 'nyest',
nyest)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 22: lwrk has been removed from the interface
.
Description
nag_fit_2dspline_sctr (e02dd) determines a smooth bicubic spline approximation s(x,y)$s(x,y)$ to the set of data points (x_{r},y_{r},f_{r})$({x}_{\mathit{r}},{y}_{\mathit{r}},{f}_{\mathit{r}})$ with weights w_{r}${\mathit{w}}_{\mathit{r}}$, for r = 1,2, … ,m$\mathit{r}=1,2,\dots ,m$.
The approximation domain is considered to be the rectangle [x_{min},x_{max}] × [y_{min},y_{max}]$[{x}_{\mathrm{min}},{x}_{\mathrm{max}}]\times [{y}_{\mathrm{min}},{y}_{\mathrm{max}}]$, where x_{min}(y_{min})${x}_{\mathrm{min}}\left({y}_{\mathrm{min}}\right)$ and x_{max}(y_{max})${x}_{\mathrm{max}}\left({y}_{\mathrm{max}}\right)$ denote the lowest and highest data values of x(y)$x\left(y\right)$.
The spline is given in the Bspline representation
where
M_{i}(x)${M}_{i}\left(x\right)$ and
N_{j}(y)${N}_{j}\left(y\right)$ denote normalized cubic Bsplines, the former defined on the knots
λ_{i}${\lambda}_{i}$ to
λ_{i + 4}${\lambda}_{i+4}$ and the latter on the knots
μ_{j}${\mu}_{j}$ to
μ_{j + 4}${\mu}_{j+4}$. For further details, see
Hayes and Halliday (1974) for bicubic splines and
de Boor (1972) for normalized Bsplines.
The total numbers
n_{x}${n}_{x}$ and
n_{y}${n}_{y}$ of these knots and their values
λ_{1}, … ,λ_{nx}${\lambda}_{1},\dots ,{\lambda}_{{n}_{x}}$ and
μ_{1}, … ,μ_{ny}${\mu}_{1},\dots ,{\mu}_{{n}_{y}}$ are chosen automatically by the function. The knots
λ_{5}, … ,λ_{nx − 4}${\lambda}_{5},\dots ,{\lambda}_{{n}_{x}4}$ and
μ_{5}, … ,μ_{ny − 4}${\mu}_{5},\dots ,{\mu}_{{n}_{y}4}$ are the interior knots; they divide the approximation domain
[x_{min},x_{max}] × [y_{min},y_{max}]$[{x}_{\mathrm{min}},{x}_{\mathrm{max}}]\times [{y}_{\mathrm{min}},{y}_{\mathrm{max}}]$ into
(n_{x} − 7) × (n_{y} − 7)$({n}_{x}7)\times ({n}_{y}7)$ subpanels
[λ_{i},λ_{i + 1}] × [μ_{j},μ_{j + 1}]$[{\lambda}_{\mathit{i}},{\lambda}_{\mathit{i}+1}]\times [{\mu}_{\mathit{j}},{\mu}_{\mathit{j}+1}]$, for
i = 4,5, … ,n_{x} − 4$\mathit{i}=4,5,\dots ,{n}_{x}4$ and
j = 4,5, … ,n_{y} − 4$\mathit{j}=4,5,\dots ,{n}_{y}4$. Then, much as in the curve case (see
nag_fit_1dspline_auto (e02be)), the coefficients
c_{ij}${c}_{ij}$ are determined as the solution of the following constrained minimization problem:
minimize
subject to the constraint
where: 
η$\eta $ 
is a measure of the (lack of) smoothness of s(x,y)$s(x,y)$. Its value depends on the discontinuity jumps in s(x,y)$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). 

ε_{r}${\epsilon}_{r}$ 
denotes the weighted residual
w_{r}
(f_{r} − s(x_{r},y_{r}))
${\mathit{w}}_{r}({f}_{r}s({x}_{r},{y}_{r}))$, 
and 
s${\mathbf{s}}$ 
is a nonnegative number to be specified by you. 
By means of the parameter
s${\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
s${\mathbf{s}}$ is too large, the spline will be too smooth and signal will be lost (underfit); if
s${\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
(θ = 0)$(\theta =0)$ if
s${\mathbf{s}}$ were set to zero, and returns the least squares bicubic polynomial
(η = 0)$(\eta =0)$ if
s${\mathbf{s}}$ is set very large. Experimenting with
s${\mathbf{s}}$values between these two extremes should result in a good compromise. (See
Section [Choice of ] for advice on choice of
s${\mathbf{s}}$.) Note however, that this function, unlike
nag_fit_1dspline_auto (e02be) and
nag_fit_2dspline_grid (e02dc), does not allow
s${\mathbf{s}}$ to be set exactly to zero: to compute an interpolant to scattered data,
nag_interp_2d_scat (e01sa) or
nag_interp_2d_scat_shep (e01sg) should be used.
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
s${\mathbf{s}}$), and an iterative method for solving the constrained minimization problem once the knots have been determined.
References
de Boor C (1972) On calculating with Bsplines 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 pseudoinverses Comput. J. 13 309–316
Reinsch C H (1967) Smoothing by spline functions Numer. Math. 10 177–183
Parameters
Compulsory Input Parameters
 1:
start – string (length ≥ 1)
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'${\mathbf{start}}=\text{'C'}$
 The function will build up the knot set starting with no interior knots. No values need be assigned to the parameters nx, ny, lamda, mu or wrk.
 start = 'W'${\mathbf{start}}=\text{'W'}$
 The function will restart the knotplacing strategy using the knots found in a previous call of the function. In this case, the parameters 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:
start = 'C'${\mathbf{start}}=\text{'C'}$ or
'W'$\text{'W'}$.
 2:
x(m) – double array
 3:
y(m) – double array
 4:
f(m) – double array
x(r)${\mathbf{x}}\left(\mathit{r}\right)$,
y(r)${\mathbf{y}}\left(\mathit{r}\right)$,
f(r)${\mathbf{f}}\left(\mathit{r}\right)$ must be set to the coordinates of
(x_{r},y_{r},f_{r})$({x}_{\mathit{r}},{y}_{\mathit{r}},{f}_{\mathit{r}})$, the
r$\mathit{r}$th data point, for
r = 1,2, … ,m$\mathit{r}=1,2,\dots ,m$. The order of the data points is immaterial.
 5:
w(m) – double array
w(r)${\mathbf{w}}\left(\mathit{r}\right)$ must be set to
w_{r}${\mathit{w}}_{\mathit{r}}$, the
r$\mathit{r}$th value in the set of weights, for
r = 1,2, … ,m$\mathit{r}=1,2,\dots ,m$. Zero weights are permitted and the corresponding points are ignored, except when determining
x_{min}${x}_{\mathrm{min}}$,
x_{max}${x}_{\mathrm{max}}$,
y_{min}${y}_{\mathrm{min}}$ and
y_{max}${y}_{\mathrm{max}}$ (see
Section [Restriction of the approximation domain]). For advice on the choice of weights, see
Section [Weighting of data points] in the E02 Chapter Introduction.
Constraint:
the number of data points with nonzero weight must be at least 16$16$.
 6:
s – double scalar
The smoothing factor,
s.
For advice on the choice of
s, see
Sections [Description] and
[Choice of ].
Constraint:
s > 0.0${\mathbf{s}}>0.0$.
 7:
nx – int64int32nag_int scalar
If the warm start option is used, the value of
nx must be left unchanged from the previous call.
 8:
lamda(nxest) – double array
nxest, the dimension of the array, must satisfy the constraint
nxest ≥ 8${\mathbf{nxest}}\ge 8$ and
nyest ≥ 8${\mathbf{nyest}}\ge 8$.
If the warm start option is used, the values
lamda(1),lamda(2), … ,lamda(nx)${\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.
 9:
ny – int64int32nag_int scalar
If the warm start option is used, the value of
ny must be left unchanged from the previous call.
 10:
mu(nyest) – double array
nyest, the dimension of the array, must satisfy the constraint
nxest ≥ 8${\mathbf{nxest}}\ge 8$ and
nyest ≥ 8${\mathbf{nyest}}\ge 8$.
If the warm start option is used, the values
mu(1),mu(2), … ,mu(ny)${\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.
 11:
wrk(lwrk) – double array
lwrk, the dimension of the array, must satisfy the constraint
lwrk ≥ (7 × u × v + 25 × w) × (w + 1) + 2 × (u + v + 4 × m) + 23 × w + 56$\mathit{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
u = nxest − 4$\mathit{u}={\mathbf{nxest}}4$,
v = nyest − 4$\mathit{v}={\mathbf{nyest}}4$ and
w = max (u,v)$\mathit{w}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}(\mathit{u},\mathit{v})$...
If the warm start option is used, on entry, the value of
wrk(1)${\mathbf{wrk}}\left(1\right)$ must be left unchanged from the previous call.
This array is used as workspace.
Optional Input Parameters
 1:
m – int64int32nag_int scalar
Default:
The dimension of the arrays
x,
y,
f,
w. (An error is raised if these dimensions are not equal.)
m$m$, the number of data points.
The number of data points with nonzero weight (see
w) must be at least
16$16$.
 2:
nxest – int64int32nag_int scalar
 3:
nyest – int64int32nag_int scalar
Default:
The dimension of the arrays
lamda,
mu. (An error is raised if these dimensions are not equal.)
An upper bound for the number of knots
n_{x}${n}_{x}$ and
n_{y}${n}_{y}$ required in the
x$x$ and
y$y$directions respectively.
In most practical situations,
nxest = nyest = 4 + sqrt(m / 2)${\mathbf{nxest}}={\mathbf{nyest}}=4+\sqrt{m/2}$ is sufficient. See also
Section [Choice of nxest and nyest].
Constraint:
nxest ≥ 8${\mathbf{nxest}}\ge 8$ and
nyest ≥ 8${\mathbf{nyest}}\ge 8$.
Input Parameters Omitted from the MATLAB Interface
 lwrk iwrk liwrk
Output Parameters
 1:
nx – int64int32nag_int scalar
The total number of knots, n_{x}${n}_{x}$, of the computed spline with respect to the x$x$ variable.
 2:
lamda(nxest) – double array
Contains the complete set of knots
λ_{i}${\lambda}_{i}$ associated with the
x$x$ variable, i.e., the interior knots
lamda(5),lamda(6), … ,lamda(nx − 4)${\mathbf{lamda}}\left(5\right),{\mathbf{lamda}}\left(6\right),\dots ,{\mathbf{lamda}}\left({\mathbf{nx}}4\right)$ as well as the additional knots
and
needed for the Bspline representation (where
x_{min}${x}_{\mathrm{min}}$ and
x_{max}${x}_{\mathrm{max}}$ are as described in
Section [Description]).
 3:
ny – int64int32nag_int scalar
The total number of knots, n_{y}${n}_{y}$, of the computed spline with respect to the y$y$ variable.
 4:
mu(nyest) – double array
Contains the complete set of knots
μ_{i}${\mu}_{i}$ associated with the
y$y$ variable, i.e., the interior knots
mu(5),mu(6), … ,mu(ny − 4)${\mathbf{mu}}\left(5\right),{\mathbf{mu}}\left(6\right),\dots ,{\mathbf{mu}}\left({\mathbf{ny}}4\right)$ as well as the additional knots
and
needed for the Bspline representation (where
y_{min}${y}_{\mathrm{min}}$ and
y_{max}${y}_{\mathrm{max}}$ are as described in
Section [Description]).
 5:
c((nxest − 4) × (nyest − 4)$({\mathbf{nxest}}4)\times ({\mathbf{nyest}}4)$) – double array
The coefficients of the spline approximation.
c((n_{y} − 4) × (i − 1) + j)${\mathbf{c}}(({n}_{y}4)\times (i1)+j)$ is the coefficient
c_{ij}${c}_{ij}$ defined in
Section [Description].
 6:
fp – double scalar
The weighted sum of squared residuals,
θ$\theta $, of the computed spline approximation.
fp should equal
s within a relative tolerance of
0.001$0.001$ unless
nx = ny = 8${\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.
 7:
rank – int64int32nag_int scalar
Gives the rank of the system of equations used to compute the final spline (as determined by a suitable machinedependent threshold). When
rank = (nx − 4) × (ny − 4)${\mathbf{rank}}=({\mathbf{nx}}4)\times ({\mathbf{ny}}4)$, the solution is unique; otherwise the system is rankdeficient and the minimumnorm solution is computed. The latter case may be caused by too small a value of
s.
 8:
wrk(lwrk) – double array
This array is used as workspace.
 9:
ifail – int64int32nag_int scalar
ifail = 0${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see
[Error Indicators and Warnings]).
Error Indicators and Warnings
Errors or warnings detected by the function:
 ifail = 1${\mathbf{ifail}}=1$
On entry,  start ≠ 'C'${\mathbf{start}}\ne \text{'C'}$ or 'W'$\text{'W'}$, 
or  the number of data points with nonzero weight < 16$\text{}<16$, 
or  s ≤ 0.0${\mathbf{s}}\le 0.0$, 
or  nxest < 8${\mathbf{nxest}}<8$, 
or  nyest < 8${\mathbf{nyest}}<8$, 
or  lwrk < (7 × u × v + 25 × w) × (w + 1) + 2 × (u + v + 4 × m) + 23 × w + 56$\mathit{lwrk}<(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 u = nxest − 4$\mathit{u}={\mathbf{nxest}}4$, v = nyest − 4$\mathit{v}={\mathbf{nyest}}4$ and w = max (u,v)$\mathit{w}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}(\mathit{u},\mathit{v})$, 
or  liwrk < m + 2 × (nxest − 7) × (nyest − 7)$\mathit{liwrk}<{\mathbf{m}}+2\times ({\mathbf{nxest}}7)\times ({\mathbf{nyest}}7)$. 
 ifail = 2${\mathbf{ifail}}=2$
On entry, either all the
x(r)${\mathbf{x}}\left(\mathit{r}\right)$, for
r = 1,2, … ,m$\mathit{r}=1,2,\dots ,{\mathbf{m}}$, are equal, or all the
y(r)${\mathbf{y}}\left(\mathit{r}\right)$, for
r = 1,2, … ,m$\mathit{r}=1,2,\dots ,{\mathbf{m}}$, are equal.
 ifail = 3${\mathbf{ifail}}=3$
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 parameters
lamda,
mu,
c,
wrk and
iwrk. However, if
nxest and
nyest are already large, say
nxest,
nyest > 4 + sqrt(m / 2)${\mathbf{nyest}}>4+\sqrt{{\mathbf{m}}/2}$, then this error exit may indicate that
s is too small.
 ifail = 4${\mathbf{ifail}}=4$
No more knots can be added because the number of Bspline coefficients
(nx − 4) × (ny − 4)$({\mathbf{nx}}4)\times ({\mathbf{ny}}4)$ already exceeds the number of data points
m. This error exit may occur if either of
s or
m is too small.
 ifail = 5${\mathbf{ifail}}=5$
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.
 ifail = 6${\mathbf{ifail}}=6$

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.
 ifail = 7${\mathbf{ifail}}=7$
lwrk is too small; the function needs to compute the minimal least squares solution of a rankdeficient 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
nag_fit_2dspline_sctr (e02dd) with
start = 'W'${\mathbf{start}}=\text{'W'}$. Note that the requested value for
lwrk is only large enough for the current phase of the algorithm. If the function 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 [Parameters] for the upper bound on
lwrk. On soft failure, the minimum requested value for
lwrk is returned in
iwrk(1)$\mathit{iwrk}\left(1\right)$ and the safe value for
lwrk is returned in
iwrk(2)$\mathit{iwrk}\left(2\right)$.
If
ifail = 3${\mathbf{ifail}}={\mathbf{3}}$,
4${\mathbf{4}}$,
5${\mathbf{5}}$ or
6${\mathbf{6}}$, a spline approximation is returned, but it fails to satisfy the fitting criterion (see
(2) and
(3) in
Section [Description] – perhaps only by a small amount, however.
Accuracy
On successful exit, the approximation returned is such that its weighted sum of squared residuals
fp is equal to the smoothing factor
s${\mathbf{s}}$, up to a specified relative tolerance of
0.001$0.001$ – except that if
n_{x} = 8${n}_{x}=8$ and
n_{y} = 8${n}_{y}=8$,
fp may be significantly less than
s${\mathbf{s}}$: in this case the computed spline is simply the least squares bicubic polynomial approximation of degree
3$3$, i.e., a spline with no interior knots.
Further Comments
Timing
The time taken for a call of
nag_fit_2dspline_sctr (e02dd) depends on the complexity of the shape of the data, the value of the smoothing factor
s${\mathbf{s}}$, and the number of data points. If
nag_fit_2dspline_sctr (e02dd) is to be called for different values of
s${\mathbf{s}}$, much time can be saved by setting
start = 'W'${\mathbf{start}}=\text{'W'}$ after the first call.
It should be noted that choosing
s${\mathbf{s}}$ very small considerably increases computation time.
Choice of s
If the weights have been correctly chosen (see
Section [Weighting of data points] in the E02 Chapter Introduction), the standard deviation of
w_{r}f_{r}${\mathit{w}}_{r}{f}_{r}$ would be the same for all
r$r$, equal to
σ$\sigma $, say. In this case, choosing the smoothing factor
s${\mathbf{s}}$ in the range
σ^{2}(m ± sqrt(2m))${\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
s${\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
s${\mathbf{s}}$ and so determine the least squares bicubic polynomial; the value returned for
fp, call it
fp_{0}${{\mathbf{fp}}}_{0}$, gives an upper bound for
s${\mathbf{s}}$. Then progressively decrease the value of
s${\mathbf{s}}$ to obtain closer fits – say by a factor of
10$10$ in the beginning, i.e.,
s = fp_{0} / 10${\mathbf{s}}={{\mathbf{fp}}}_{0}/10$,
s = fp_{0} / 100${\mathbf{s}}={{\mathbf{fp}}}_{0}/100$, and so on, and more carefully as the approximation shows more details.
To choose
s${\mathbf{s}}$ very small is strongly discouraged. This considerably increases computation time and memory requirements. It may also cause rankdeficiency (as indicated by the parameter
rank) and endanger numerical stability.
The number of knots of the spline returned, and their location, generally depend on the value of
s${\mathbf{s}}$ and on the behaviour of the function underlying the data. However, if
nag_fit_2dspline_sctr (e02dd) is called with
start = 'W'${\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
s${\mathbf{s}}$ and
start = 'W'${\mathbf{start}}=\text{'W'}$, a fit can finally be accepted as satisfactory, it may be worthwhile to call
nag_fit_2dspline_sctr (e02dd) once more with the selected value for
s${\mathbf{s}}$ but now using
start = 'C'${\mathbf{start}}=\text{'C'}$. Often,
nag_fit_2dspline_sctr (e02dd) then returns an approximation with the same quality of fit but with fewer knots, which is therefore better if data reduction is also important.
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
nag_fit_2dspline_sctr (e02dd) the number of knots in one direction (say
n_{x}${n}_{x}$) has reached the value of its upper bound (
nxest), then from that moment on all subsequent knots are added in the other
(y)$\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 function locates in any direction. For example, by setting
nxest = 8${\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$x$.
Restriction of the approximation domain
The fit obtained is not defined outside the rectangle [λ_{4},λ_{nx − 3}] × [μ_{4},μ_{ny − 3}]$[{\lambda}_{4},{\lambda}_{{n}_{x}3}]\times [{\mu}_{4},{\mu}_{{n}_{y}3}]$. The reason for taking the extreme data values of x$x$ and y$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)$(a,c,0)$ and (b,d,0)$(b,d,0)$ with zero weight, where [a,b] × [c,d]$[a,b]\times [c,d]$ denotes the enlarged rectangle.
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
θ$\theta $, the sum of squares of residuals, is computed. If
θ > s$\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
θ ≤ s$\theta \le {\mathbf{s}}$ 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
θ = s$\theta ={\mathbf{s}}$. The function computes the spline by an iterative scheme which is ended when
θ = s$\theta ={\mathbf{s}}$ within a relative tolerance of
0.001$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
nag_fit_2dspline_panel (e02da). As there also, the minimal least squares solution is computed wherever the linear system is found to be rankdeficient.
An exception occurs when the function finds at the start that, even with no interior knots (
N = 8$\mathrm{N}=8$), the least squares spline already has its sum of squares of residuals
≤ s$\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
s${\mathbf{s}}$ 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
(x_{r},y_{r})
$({x}_{\mathit{r}},{y}_{\mathit{r}})$, for
r = 1,2, … ,n$\mathit{r}=1,2,\dots ,n$, may be obtained in the double array
ff (see
nag_fit_2dspline_evalv (e02de)), of length at least
n$n$, by the following call:
[ff, ifail] = e02de(x, y, lamda, mu, c);
where
N = n$\mathtt{N}=n$ and the coordinates
x_{r}${x}_{r}$,
y_{r}${y}_{r}$ are stored in
X(k)$\mathtt{X}\left(k\right)$,
Y(k)$\mathtt{Y}\left(k\right)$.
PX and
PY have the same values as
nx and
ny as output from
nag_fit_2dspline_sctr (e02dd), and
LAMDA,
MU and
C have the same values as
lamda,
mu and
c output from
nag_fit_2dspline_sctr (e02dd).
WRK is a double workspace array of length at least
PY − 4$\mathtt{PY}4$, and
IWRK is an integer workspace array of length at least
PY − 4$\mathtt{PY}4$.
To evaluate the computed spline on a
k_{x}${k}_{x}$ by
k_{y}${k}_{y}$ rectangular grid of points in the
x$x$
y$y$ plane, which is defined by the
x$x$ coordinates stored in
X(q)$\mathtt{X}\left(\mathit{q}\right)$, for
q = 1,2, … ,k_{x}$\mathit{q}=1,2,\dots ,{k}_{x}$, and the
y$y$ coordinates stored in
Y(r)$\mathtt{Y}\left(\mathit{r}\right)$, for
r = 1,2, … ,k_{y}$\mathit{r}=1,2,\dots ,{k}_{y}$, returning the results in the double array
ff (see
nag_fit_2dspline_evalm (e02df)) which is of length at least
mx × my${\mathbf{mx}}\times {\mathbf{my}}$, the following call may be used:
[fg, ifail] = e02df(tx, ty, lamda, mu, c);
where
KX = k_{x}$\mathtt{KX}={k}_{x}$,
KY = k_{y}$\mathtt{KY}={k}_{y}$.
LAMDA,
MU and
C have the same values as
lamda,
mu and
c output from
nag_fit_2dspline_sctr (e02dd).
WRK is a double workspace array of length at least
LWRK
=
min (nwrk1,nwrk2)
$\mathtt{LWRK}=\mathrm{min}\phantom{\rule{0.125em}{0ex}}(\mathit{nwrk1},\mathit{nwrk2})$, where
nwrk1 = KX × 4 + PX$\mathit{nwrk1}=\mathtt{KX}\times 4+\mathtt{PX}$ and
nwrk2 = KY × 4 + PY$\mathit{nwrk2}=\mathtt{KY}\times 4+\mathtt{PY}$.
IWRK is an integer workspace array of length at least
LIWRK = KY + PY − 4$\mathtt{LIWRK}=\mathtt{KY}+\mathtt{PY}4$ if
nwrk1 ≥ nwrk2$\mathit{nwrk1}\ge \mathit{nwrk2}$, or
KX + PX − 4$\mathtt{KX}+\mathtt{PX}4$ otherwise.
The result of the spline evaluated at grid point (q,r)$(q,r)$ is returned in element (KY × (q − 1) + r)$(\mathtt{KY}\times (q1)+r)$ of the array FG.
Example
Open in the MATLAB editor:
nag_fit_2dspline_sctr_example
function nag_fit_2dspline_sctr_example
start = 'C';
x = [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];
y = [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.53;
0;
10.69;
13.78;
15.03;
8.37;
19.63;
17.13;
14.36;
0.33];
f = [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];
w = [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];
s = 10;
nx = int64(0);
lamda = zeros(14,1);
ny = int64(0);
mu = zeros(14,1);
wrk = zeros(11016, 1);
[nxOut, lamdaOut, nyOut, muOut, c, fp, rank, wrkOut, ifail] = ...
nag_fit_2dspline_sctr(start, x, y, f, w, s, nx, lamda, ny, mu, wrk);
nxOut, lamdaOut, nyOut, muOut, c, fp, rank, ifail
nxOut =
10
lamdaOut =
0
0
0
0
9.7575
18.2582
25.0000
25.0000
25.0000
25.0000
0
0
0
0
nyOut =
9
muOut =
0
0
0
0
9.0008
20.0000
20.0000
20.0000
20.0000
0
0
0
0
0
c =
58.1559
63.7813
40.8392
75.4362
34.6068
46.3067
46.7449
33.7898
111.9175
42.6140
6.0058
33.3668
5.1688
6.9393
25.2015
31.9987
18.2980
13.0954
17.3287
1.9641
5.8554
14.3600
4.1317
7.0928
10.3721
23.7779
15.9518
19.3683
13.2436
9.0871
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
fp =
10.0021
rank =
30
ifail =
0
Open in the MATLAB editor:
e02dd_example
function e02dd_example
start = 'C';
x = [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];
y = [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.53;
0;
10.69;
13.78;
15.03;
8.37;
19.63;
17.13;
14.36;
0.33];
f = [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];
w = [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];
s = 10;
nx = int64(0);
lamda = zeros(14,1);
ny = int64(0);
mu = zeros(14,1);
wrk = zeros(11016, 1);
[nxOut, lamdaOut, nyOut, muOut, c, fp, rank, wrkOut, ifail] = ...
e02dd(start, x, y, f, w, s, nx, lamda, ny, mu, wrk);
nxOut, lamdaOut, nyOut, muOut, c, fp, rank, ifail
nxOut =
10
lamdaOut =
0
0
0
0
9.7575
18.2582
25.0000
25.0000
25.0000
25.0000
0
0
0
0
nyOut =
9
muOut =
0
0
0
0
9.0008
20.0000
20.0000
20.0000
20.0000
0
0
0
0
0
c =
58.1559
63.7813
40.8392
75.4362
34.6068
46.3067
46.7449
33.7898
111.9175
42.6140
6.0058
33.3668
5.1688
6.9393
25.2015
31.9987
18.2980
13.0954
17.3287
1.9641
5.8554
14.3600
4.1317
7.0928
10.3721
23.7779
15.9518
19.3683
13.2436
9.0871
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
fp =
10.0021
rank =
30
ifail =
0
PDF version (NAG web site
, 64bit version, 64bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013