Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_fit_2dspline_grid (e02dc)

## Purpose

nag_fit_2dspline_grid (e02dc) 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.

## Syntax

[nx, lamda, ny, mu, c, fp, wrk, iwrk, ifail] = e02dc(start, x, y, f, s, nx, lamda, ny, mu, wrk, iwrk, 'mx', mx, 'my', my, 'nxest', nxest, 'nyest', nyest)
[nx, lamda, ny, mu, c, fp, wrk, iwrk, ifail] = nag_fit_2dspline_grid(start, x, y, f, s, nx, lamda, ny, mu, wrk, iwrk, 'mx', mx, 'my', my, 'nxest', nxest, 'nyest', nyest)
Note: the interface to this routine has changed since earlier releases of the toolbox:
 At Mark 22: lwrk and liwrk were removed from the interface

## Description

nag_fit_2dspline_grid (e02dc) determines a smooth bicubic spline approximation $s\left(x,y\right)$ to the set of data points $\left({x}_{\mathit{q}},{y}_{\mathit{r}},{f}_{\mathit{q},\mathit{r}}\right)$, for $\mathit{q}=1,2,\dots ,{m}_{x}$ and $\mathit{r}=1,2,\dots ,{m}_{y}$.
The spline is given in the B-spline representation
 $sx,y = ∑ i=1 nx-4 ∑ j=1 ny-4 cij Mix Njy ,$ (1)
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 function. 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 $\left[{x}_{1},{x}_{{m}_{x}}\right]×\left[{y}_{1},{y}_{{m}_{y}}\right]$ into $\left({n}_{x}-7\right)×\left({n}_{y}-7\right)$ subpanels $\left[{\lambda }_{\mathit{i}},{\lambda }_{\mathit{i}+1}\right]×\left[{\mu }_{\mathit{j}},{\mu }_{\mathit{j}+1}\right]$, 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 nag_fit_1dspline_auto (e02be)), the coefficients ${c}_{\mathit{i}\mathit{j}}$ 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 $\eta$ is a measure of the (lack of) smoothness of $s\left(x,y\right)$. Its value depends on the discontinuity jumps in $s\left(x,y\right)$ 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). ${\epsilon }_{q,r}$ denotes the residual ${f}_{q,r}-s\left({x}_{q},{y}_{r}\right)$, 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 function will return an interpolating spline $\left(\theta =0\right)$ if $S$ is set to zero, and the least squares bicubic polynomial $\left(\eta =0\right)$ if $S$ is set very large. Experimenting with $S$-values between these two extremes should result in a good compromise. (See Choice of s for advice on choice of $S$.)
The method employed is outlined in Outline of Method Used 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 nag_fit_2dspline_evalv (e02de), nag_fit_2dspline_evalm (e02df) or nag_fit_2dspline_derivm (e02dh) as described in Evaluation of Computed Spline.

## 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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{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).
${\mathbf{start}}=\text{'C'}$
The function 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.
${\mathbf{start}}=\text{'W'}$
The function will restart the knot-placing strategy using the knots found in a previous call of the function. 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: ${\mathbf{start}}=\text{'C'}$ or $\text{'W'}$.
2:     $\mathrm{x}\left({\mathbf{mx}}\right)$ – double array
${\mathbf{x}}\left(\mathit{q}\right)$ must be set to ${x}_{\mathit{q}}$, the $x$ coordinate of the $\mathit{q}$th grid point along the $x$ axis, for $\mathit{q}=1,2,\dots ,{m}_{x}$.
Constraint: ${x}_{1}<{x}_{2}<\cdots <{x}_{{m}_{x}}$.
3:     $\mathrm{y}\left({\mathbf{my}}\right)$ – double array
${\mathbf{y}}\left(\mathit{r}\right)$ must be set to ${y}_{\mathit{r}}$, the $y$ coordinate of the $\mathit{r}$th grid point along the $y$ axis, for $\mathit{r}=1,2,\dots ,{m}_{y}$.
Constraint: ${y}_{1}<{y}_{2}<\cdots <{y}_{{m}_{y}}$.
4:     $\mathrm{f}\left({\mathbf{mx}}×{\mathbf{my}}\right)$ – double array
${\mathbf{f}}\left({m}_{y}×\left(\mathit{q}-1\right)+\mathit{r}\right)$ must contain the data value ${f}_{\mathit{q},\mathit{r}}$, for $\mathit{q}=1,2,\dots ,{m}_{x}$ and $\mathit{r}=1,2,\dots ,{m}_{y}$.
5:     $\mathrm{s}$ – double scalar
The smoothing factor, $S$.
If ${\mathbf{s}}=0.0$, the function 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 Description and Choice of s.
Constraint: ${\mathbf{s}}\ge 0.0$.
6:     $\mathrm{nx}$int64int32nag_int scalar
If the warm start option is used, the value of nx must be left unchanged from the previous call.
7:     $\mathrm{lamda}\left({\mathbf{nxest}}\right)$ – double array
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.
8:     $\mathrm{ny}$int64int32nag_int scalar
If the warm start option is used, the value of ny must be left unchanged from the previous call.
9:     $\mathrm{mu}\left({\mathbf{nyest}}\right)$ – double array
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.
10:   $\mathrm{wrk}\left(\mathit{lwrk}\right)$ – double array
lwrk, the dimension of the array, must satisfy the constraint $\mathit{lwrk}\ge 4×\left({\mathbf{mx}}+{\mathbf{my}}\right)+11×\left({\mathbf{nxest}}+{\mathbf{nyest}}\right)+{\mathbf{nxest}}×{\mathbf{my}}+\phantom{\rule{0ex}{0ex}}\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{my}},{\mathbf{nxest}}\right)+54$.
If the warm start option is used, on entry, the values ${\mathbf{wrk}}\left(1\right),\dots ,{\mathbf{wrk}}\left(4\right)$ must be left unchanged from the previous call.
This array is used as workspace.
11:   $\mathrm{iwrk}\left(\mathit{liwrk}\right)$int64int32nag_int array
liwrk, the dimension of the array, must satisfy the constraint $\mathit{liwrk}\ge 3+{\mathbf{mx}}+{\mathbf{my}}+{\mathbf{nxest}}+{\mathbf{nyest}}$.
If the warm start option is used, on entry, the values ${\mathbf{iwrk}}\left(1\right),\dots ,{\mathbf{iwrk}}\left(3\right)$ must be left unchanged from the previous call.
This array is used as workspace.

### Optional Input Parameters

1:     $\mathrm{mx}$int64int32nag_int scalar
Default: the dimension of the array x.
${m}_{x}$, the number of grid points along the $x$ axis.
Constraint: ${\mathbf{mx}}\ge 4$.
2:     $\mathrm{my}$int64int32nag_int scalar
Default: the dimension of the array y.
${m}_{y}$, the number of grid points along the $y$ axis.
Constraint: ${\mathbf{my}}\ge 4$.
3:     $\mathrm{nxest}$int64int32nag_int scalar
4:     $\mathrm{nyest}$int64int32nag_int scalar
Default: For nxest, the dimension of the array lamda. For nyest, the dimension of the array mu.
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}}={m}_{x}/2$ and ${\mathbf{nyest}}={m}_{y}/2$ is sufficient. nxest and nyest never need to be larger than ${m}_{x}+4$ and ${m}_{y}+4$ respectively, the numbers of knots needed for interpolation $\left({\mathbf{s}}=0.0\right)$. See also Choice of nxest and nyest.
Constraints:
• ${\mathbf{nxest}}\ge 8$;
• ${\mathbf{nyest}}\ge 8$.

### Output Parameters

1:     $\mathrm{nx}$int64int32nag_int scalar
The total number of knots, ${n}_{x}$, of the computed spline with respect to the $x$ variable.
2:     $\mathrm{lamda}\left({\mathbf{nxest}}\right)$ – double array
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
 $lamda1=lamda2=lamda3=lamda4=x1$
and
 $lamdanx- 3=lamdanx- 2=lamdanx- 1=lamdanx=xmx$
needed for the B-spline representation.
3:     $\mathrm{ny}$int64int32nag_int scalar
The total number of knots, ${n}_{y}$, of the computed spline with respect to the $y$ variable.
4:     $\mathrm{mu}\left({\mathbf{nyest}}\right)$ – double array
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
 $mu1=mu2=mu3=mu4=y1$
and
 $muny- 3=muny- 2=muny- 1=muny=ymy$
needed for the B-spline representation.
5:     $\mathrm{c}\left(\left({\mathbf{nxest}}-4\right)×\left({\mathbf{nyest}}-4\right)\right)$ – double array
The coefficients of the spline approximation. ${\mathbf{c}}\left(\left({n}_{y}-4\right)×\left(i-1\right)+j\right)$ is the coefficient ${c}_{ij}$ defined in Description.
6:     $\mathrm{fp}$ – double scalar
The sum of squared residuals, $\theta$, of the computed spline approximation. If ${\mathbf{fp}}=0.0$, this is an interpolating spline. fp should equal ${\mathbf{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.
7:     $\mathrm{wrk}\left(\mathit{lwrk}\right)$ – double array
This array is used as workspace.
8:     $\mathrm{iwrk}\left(\mathit{liwrk}\right)$int64int32nag_int array
This array is used as workspace.
9:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{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:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{start}}\ne \text{'C'}$ or $\text{'W'}$, or ${\mathbf{mx}}<4$, or ${\mathbf{my}}<4$, or ${\mathbf{s}}<0.0$, or ${\mathbf{s}}=0.0$ and ${\mathbf{nxest}}<{\mathbf{mx}}+4$, or ${\mathbf{s}}=0.0$ and ${\mathbf{nyest}}<{\mathbf{my}}+4$, or ${\mathbf{nxest}}<8$, or ${\mathbf{nyest}}<8$, or $\mathit{lwrk}<4×\left({\mathbf{mx}}+{\mathbf{my}}\right)+11×\left({\mathbf{nxest}}+{\mathbf{nyest}}\right)+{\mathbf{nxest}}×{\mathbf{my}}+\phantom{\rule{0ex}{0ex}}\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{my}},{\mathbf{nxest}}\right)+54$, or $\mathit{liwrk}<3+{\mathbf{mx}}+{\mathbf{my}}+{\mathbf{nxest}}+{\mathbf{nyest}}$.
${\mathbf{ifail}}=2$
The values of ${\mathbf{x}}\left(\mathit{q}\right)$, for $\mathit{q}=1,2,\dots ,{\mathbf{mx}}$, are not in strictly increasing order.
${\mathbf{ifail}}=3$
The values of ${\mathbf{y}}\left(\mathit{r}\right)$, for $\mathit{r}=1,2,\dots ,{\mathbf{my}}$, are not in strictly increasing order.
${\mathbf{ifail}}=4$
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 arguments lamda, mu, c, wrk and iwrk. However, if nxest and nyest are already large, say ${\mathbf{nxest}}>{\mathbf{mx}}/2$ and ${\mathbf{nyest}}>{\mathbf{my}}/2$, then this error exit may indicate that s is too small.
${\mathbf{ifail}}=5$
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.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
If ${\mathbf{ifail}}={\mathbf{4}}$ or ${\mathbf{5}}$, a spline approximation is returned, but it fails to satisfy the fitting criterion (see (2) and (3) in Description) – perhaps by only a small amount, however.

## Accuracy

On successful exit, the approximation returned is such that its 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.

### Timing

The time taken for a call of nag_fit_2dspline_grid (e02dc) depends on the complexity of the shape of the data, the value of the smoothing factor $S$, and the number of data points. If nag_fit_2dspline_grid (e02dc) is to be called for different values of $S$, much time can be saved by setting ${\mathbf{start}}=\text{'W'}$ after the first call.

### Weighting of Data Points

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

### Choice of s

If the standard deviation of ${f}_{q,r}$ is the same for all $q$ and $r$ (the case for which nag_fit_2dspline_grid (e02dc) is designed – see Weighting of Data Points.) and known to be equal, at least approximately, to $\sigma$, say, then following Reinsch (1967) and choosing the argument s in the range ${\sigma }^{2}\left(m±\sqrt{2m}\right)$, where $m={m}_{x}{m}_{y}$, 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 Description.
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 ${{\mathbf{fp}}}_{0}$, 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., ${\mathbf{s}}={{\mathbf{fp}}}_{0}/10$, ${\mathbf{s}}={{\mathbf{fp}}}_{0}/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 ${\mathbf{s}}$ and on the behaviour of the function underlying the data. However, if nag_fit_2dspline_grid (e02dc) 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 nag_fit_2dspline_grid (e02dc) once more with the selected value for ${\mathbf{s}}$ but now using ${\mathbf{start}}=\text{'C'}$. Often, nag_fit_2dspline_grid (e02dc) 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_grid (e02dc) 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. Therefore you have the option of limiting the number of knots the function 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$.

### Outline of Method Used

If $S=0$, the requisite number of knots is known in advance, i.e., ${n}_{x}={m}_{x}+4$ and ${n}_{y}={m}_{y}+4$; the interior knots are located immediately as ${\lambda }_{\mathit{i}}={x}_{\mathit{i}-2}$ and ${\mu }_{\mathit{j}}={y}_{\mathit{j}-2}$, for $\mathit{i}=5,6,\dots ,{n}_{x}-4$ and $\mathit{j}=5,6,\dots ,{n}_{y}-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 $\theta$, the sum of squares of residuals, is computed. If $\theta >S$, new knots are added to one knot set or the other so as to reduce $\theta$ 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 $\theta$. Sooner or later, we find that $\theta \le S$ 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 $\theta =S$. The function computes the spline by an iterative scheme which is ended when $\theta =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 nag_fit_1dspline_knots (e02ba) for least squares curve-fitting.
An exception occurs when the function finds at the start that, even with no interior knots $\left({n}_{x}={n}_{y}=8\right)$, the least squares spline already has its sum of residuals $\text{}\le 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$ 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 $\left({x}_{\mathit{r}},{y}_{\mathit{r}}\right)$, for $\mathit{r}=1,2,\dots ,m$, may be obtained in the double array ff (see nag_fit_2dspline_evalv (e02de)), of length at least $m$, by the following call:
```[ff, ifail] = e02de(x, y, lamda, mu, c);
```
where $\mathtt{M}=m$ and the coordinates ${x}_{r}$, ${y}_{r}$ are stored in $\mathtt{X}\left(k\right)$, $\mathtt{Y}\left(k\right)$. PX and PY have the same values as nx and ny as output from nag_fit_2dspline_grid (e02dc), and LAMDA, MU and C have the same values as lamda, mu and c output from nag_fit_2dspline_grid (e02dc). WRK is a double workspace array of length at least $\mathtt{PY}-4$, and IWRK is an integer workspace array of length at least $\mathtt{PY}-4$.
To evaluate the computed spline on a ${m}_{x}$ by ${m}_{y}$ rectangular grid of points in the $x$-$y$ plane, which is defined by the $x$ coordinates stored in $\mathtt{X}\left(\mathit{q}\right)$, for $\mathit{q}=1,2,\dots ,{m}_{x}$, and the $y$ coordinates stored in $\mathtt{Y}\left(\mathit{r}\right)$, for $\mathit{r}=1,2,\dots ,{m}_{y}$, returning the results in the double array ff (see nag_fit_2dspline_evalm (e02df)) which is of length at least ${\mathbf{mx}}×{\mathbf{my}}$, the following call may be used:
```[ff, ifail] = e02df(x, y, lamda, mu, c);
```
where $\mathtt{MX}={m}_{x}$, $\mathtt{MY}={m}_{y}$. LAMDA, MU and C have the same values as lamda, mu and c output from nag_fit_2dspline_grid (e02dc). WRK is a double workspace array of length at least $\mathtt{LWRK}=\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(\mathit{nwrk1},\mathit{nwrk2}\right)$, where $\mathit{nwrk1}=\mathtt{MX}×4+\mathtt{PX}$ and $\mathit{nwrk2}=\mathtt{MY}×4+\mathtt{PY}$. IWRK is an integer workspace array of length at least $\mathtt{LIWRK}=\mathtt{MY}+\mathtt{PY}-4$ if $\mathit{nwrk1}\ge \mathit{nwrk2}$, or $\mathtt{MX}+\mathtt{PX}-4$ otherwise.
The result of the spline evaluated at grid point $\left(q,r\right)$ is returned in element $\left(\mathtt{MY}×\left(q-1\right)+r\right)$ of the array FG.

## Example

This example reads in values of mx, my, ${x}_{\mathit{q}}$, for $\mathit{q}=1,2,\dots ,{\mathbf{mx}}$, and ${y}_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,{\mathbf{my}}$, followed by values of the ordinates ${f}_{q,r}$ defined at the grid points $\left({x}_{q},{y}_{r}\right)$. It then calls nag_fit_2dspline_grid (e02dc) 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.
```function e02dc_example

fprintf('e02dc example results\n\n');

start = 'C';
x = [0:0.5:5];
y = [0:0.5:4];
f = [ 1         1.5      2.06     2.57     3        3.5;
0.88758   1.3564   1.7552   2.124    2.6427   3.1715;
0.5403    0.82045  1.0806   1.3508   1.6309   1.8611;
0.070737  0.10611  0.15147  0.17684  0.21221  0.24458;
-0.41515  -0.62422 -0.83229 -1.0404  -1.2484  -1.4565;
-0.80114  -1.2317  -1.6023  -2.0029  -2.2034  -2.864;
-0.97999  -1.485   -1.97    -2.475   -2.97    -3.265;
-0.93446  -1.3047  -1.8729  -2.3511  -2.8094  -3.2776;
-0.65664  -0.98547 -1.4073  -1.6741  -1.9809  -2.2878];

f(:,7:11) = [   4.04     4.5      5.04     5.505    6;
3.5103   3.9391   4.3879   4.8367   5.2755;
2.0612   2.4314   2.7515   2.9717   3.2418;
0.28595  0.31632  0.35369  0.38505  0.42442;
-1.6946  -1.8627  -2.0707  -2.2888  -2.4769;
-3.2046  -3.6351  -4.0057  -4.4033  -4.8169;
-3.96    -4.455   -4.97    -5.445   -5.93;
-3.7958  -4.2141  -4.6823  -5.1405  -5.6387;
-2.6146  -2.9314  -3.2382  -3.595   -3.9319];

% Input argument initializations
s     = 0.1;
nx    = int64(0); ny = nx;
mx    = size(x,2);
my    = size(y,2);
lamda = zeros(mx+4, 1);
mu    = zeros(my+4, 1);
wrk   = zeros(1000, 1);
iwrk = zeros(100, 1, 'int64');

% Compute bi-cubic spline for grid data
[nx, lamda, ny, mu, c, fp, wrk, iwrk, ifail] = ...
e02dc( ...
start, x, y, f, s, nx, lamda, ny, mu, wrk, iwrk);

% Print details of spline
fprintf('\nCalling with smoothing factor S = %5.2f\n\n', s);
fprintf('Knots:   lamda      mu\n');
for j = 4:max(nx,ny)-3
if j<=min(nx,ny)-3
fprintf('%4d%10.4f%10.4f\n', j, lamda(j), mu(j));
elseif j<=nx-3
fprintf('%4d%10.4f\n', j, lamda(j));
else
fprintf('%4d%20.4f\n', j, mu(j));
end
end

cp = c(1:(ny-4)*(nx-4));
cp = reshape(cp,[ny-4,nx-4]);
fprintf('\nB-spline coefficients:\n');
disp(cp);

fprintf('Weighted sum of squared residuals = %7.4f\n', fp);
if fp==0
fprintf('(The spline is an interpolating spline)\n');
elseif nx==8 && ny==8
fprintf('(The spline is the weighted least-squares cubic polynomial)\n');
end
fprintf('\n');

% Evaluate spline on mesh
mx = [0:0.2:5];
my = [0:0.2:4];
[ff, ifail] = e02df( ...
mx, my, lamda(1:nx), mu(1:ny), c);

fig1 = figure;
ff = reshape(ff,[21,26]);
meshc(mx,my,ff);
xlabel('x');
ylabel('y');
title('Least-squares bi-cubic spline fit');

```
```e02dc example results

Calling with smoothing factor S =  0.10

Knots:   lamda      mu
4    0.0000    0.0000
5    1.5000    1.0000
6    2.5000    2.0000
7    5.0000    2.5000
8              3.0000
9              3.5000
10              4.0000

B-spline coefficients:
0.9918    1.5381    2.3913    3.9845    5.2138    5.9965
1.0546    1.5270    2.2441    4.2217    5.0860    6.0821
0.6098    0.9557    1.5587    2.3458    3.3860    3.7716
-0.2915   -0.4199   -0.7399   -1.1763   -1.5527   -1.7775
-0.8476   -1.3296   -1.8521   -3.3468   -4.3628   -5.0085
-1.0168   -1.5952   -2.4022   -3.9390   -5.4680   -6.1656
-0.9529   -1.3381   -2.2844   -3.9559   -5.0032   -5.8709
-0.7711   -1.0914   -1.8488   -3.2549   -3.9444   -4.7297
-0.6476   -1.0373   -1.5936   -2.5887   -3.3485   -3.9330

Weighted sum of squared residuals =  0.1000

``` 