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_interp_2d_spline_grid (e01da)

## Purpose

nag_interp_2d_spline_grid (e01da) computes a bicubic spline interpolating surface through a set of data values, given on a rectangular grid in the x$x$-y$y$ plane.

## Syntax

[px, py, lamda, mu, c, ifail] = e01da(x, y, f, 'mx', mx, 'my', my)
[px, py, lamda, mu, c, ifail] = nag_interp_2d_spline_grid(x, y, f, 'mx', mx, 'my', my)

## Description

nag_interp_2d_spline_grid (e01da) determines a bicubic spline interpolant to the set of data points (xq,yr,f q , r ) $\left({x}_{\mathit{q}},{y}_{\mathit{r}},{f}_{\mathit{q},\mathit{r}}\right)$, for q = 1,2,,mx$\mathit{q}=1,2,\dots ,{m}_{x}$ and r = 1,2,,my$\mathit{r}=1,2,\dots ,{m}_{y}$. The spline is given in the B-spline representation
 mx my s(x,y) = ∑ ∑ cijMi(x)Nj(y), i = 1 j = 1
$s(x,y)=∑i=1mx∑j=1mycijMi(x)Nj(y),$
such that
 s(xq,yr) = fq,r, $s(xq,yr)=fq,r,$
where Mi(x)${M}_{i}\left(x\right)$ and Nj(y)${N}_{j}\left(y\right)$ denote normalized cubic B-splines, 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}$, and the cij${c}_{ij}$ are the spline coefficients. These knots, as well as the coefficients, are determined by the function, which is derived from the function B2IRE in Anthony et al. (1982). The method used is described in Section [Outline of Method Used].
For further information on splines, see Hayes and Halliday (1974) for bicubic splines and de Boor (1972) for normalized B-splines.
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 Section [Evaluation of Computed Spline].

## References

Anthony G T, Cox M G and Hayes J G (1982) DASL – Data Approximation Subroutine Library National Physical Laboratory
Cox M G (1975) An algorithm for spline interpolation J. Inst. Math. Appl. 15 95–108
de Boor C (1972) On calculating with B-splines J. Approx. Theory 6 50–62
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

## Parameters

### Compulsory Input Parameters

1:     x(mx) – double array
2:     y(my) – double array
mx, the dimension of the array, must satisfy the constraint mx4${\mathbf{mx}}\ge 4$ and my4${\mathbf{my}}\ge 4$.
x(q)${\mathbf{x}}\left(\mathit{q}\right)$ and y(r)${\mathbf{y}}\left(\mathit{r}\right)$ must contain xq${x}_{\mathit{q}}$, for q = 1,2,,mx$\mathit{q}=1,2,\dots ,{m}_{x}$, and yr${y}_{\mathit{r}}$, for r = 1,2,,my$\mathit{r}=1,2,\dots ,{m}_{y}$, respectively.
Constraints:
• x(q) < x(q + 1)${\mathbf{x}}\left(\mathit{q}\right)<{\mathbf{x}}\left(\mathit{q}+1\right)$, for q = 1,2,,mx1$\mathit{q}=1,2,\dots ,{m}_{x}-1$;
• y(r) < y(r + 1)${\mathbf{y}}\left(\mathit{r}\right)<{\mathbf{y}}\left(\mathit{r}+1\right)$, for r = 1,2,,my1$\mathit{r}=1,2,\dots ,{m}_{y}-1$.
3:     f(mx × my${\mathbf{mx}}×{\mathbf{my}}$) – double array
f(my × (q1) + r)${\mathbf{f}}\left({m}_{y}×\left(\mathit{q}-1\right)+\mathit{r}\right)$ must contain fq,r${f}_{\mathit{q},\mathit{r}}$, for q = 1,2,,mx$\mathit{q}=1,2,\dots ,{m}_{x}$ and r = 1,2,,my$\mathit{r}=1,2,\dots ,{m}_{y}$.

### Optional Input Parameters

1:     mx – int64int32nag_int scalar
2:     my – int64int32nag_int scalar
Default: The dimension of the arrays x, y. (An error is raised if these dimensions are not equal.)
mx and my must specify mx${m}_{x}$ and my${m}_{y}$ respectively, the number of points along the x$x$ and y$y$ axis that define the rectangular grid.
Constraint: mx4${\mathbf{mx}}\ge 4$ and my4${\mathbf{my}}\ge 4$.

wrk

### Output Parameters

1:     px – int64int32nag_int scalar
2:     py – int64int32nag_int scalar
px and py contain mx + 4${m}_{x}+4$ and my + 4${m}_{y}+4$, the total number of knots of the computed spline with respect to the x$x$ and y$y$ variables, respectively.
3:     lamda(mx + 4${\mathbf{mx}}+4$) – double array
4:     mu(my + 4${\mathbf{my}}+4$) – double array
lamda 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(px4)${\mathbf{lamda}}\left(5\right),{\mathbf{lamda}}\left(6\right),\dots ,{\mathbf{lamda}}\left({\mathbf{px}}-4\right)$, as well as the additional knots
 lamda(1) = lamda(2) = lamda(3) = lamda(4) = x(1) $lamda1=lamda2=lamda3=lamda4=x1$
and
 lamda(px − 3) = lamda(px − 2) = lamda(px − 1) = lamda(px) = x(mx) $lamdapx-3=lamdapx-2=lamdapx-1=lamdapx=xmx$
needed for the B-spline representation.
5:     c(mx × my${\mathbf{mx}}×{\mathbf{my}}$) – double array
The coefficients of the spline interpolant. c(my × (i1) + j)${\mathbf{c}}\left({m}_{y}×\left(i-1\right)+j\right)$ contains the coefficient cij${c}_{ij}$ described in Section [Description].
6:     ifail – int64int32nag_int scalar
${\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, mx < 4${\mathbf{mx}}<4$, or my < 4${\mathbf{my}}<4$.
ifail = 2${\mathbf{ifail}}=2$
On entry, either the values in the x array or the values in the y array are not in increasing order if not already there.
ifail = 3${\mathbf{ifail}}=3$
A system of linear equations defining the B-spline coefficients was singular; the problem is too ill-conditioned to permit solution.

## Accuracy

The main sources of rounding errors are in steps 2$2$, 3$3$, 6$6$ and 7$7$ of the algorithm described in Section [Outline of Method Used]. It can be shown (see Cox (1975)) that the matrix Ax${A}_{x}$ formed in step 2$2$ has elements differing relatively from their true values by at most a small multiple of 3ε$3\epsilon$, where ε$\epsilon$ is the machine precision. Ax${A}_{x}$ is ‘totally positive’, and a linear system with such a coefficient matrix can be solved quite safely by elimination without pivoting. Similar comments apply to steps 6$6$ and 7$7$. Thus the complete process is numerically stable.

### Timing

The time taken by nag_interp_2d_spline_grid (e01da) is approximately proportional to mxmy${m}_{x}{m}_{y}$.

### Outline of Method Used

The process of computing the spline consists of the following steps:
1. choice of the interior x$x$-knots λ5${\lambda }_{5}$, λ6,,λmx${\lambda }_{6},\dots ,{\lambda }_{{m}_{x}}$ as λi = xi2${\lambda }_{\mathit{i}}={x}_{\mathit{i}-2}$, for i = 5,6,,mx$\mathit{i}=5,6,\dots ,{m}_{x}$,
2. formation of the system
 AxE = F, $AxE=F,$
where Ax${A}_{x}$ is a band matrix of order mx${m}_{x}$ and bandwidth 4$4$, containing in its q$q$th row the values at xq${x}_{q}$ of the B-splines in x$x$, f${\mathbf{f}}$ is the mx${m}_{x}$ by my${m}_{y}$ rectangular matrix of values fq,r${f}_{q,r}$, and E$E$ denotes an mx${m}_{x}$ by my${m}_{y}$ rectangular matrix of intermediate coefficients,
3. use of Gaussian elimination to reduce this system to band triangular form,
4. solution of this triangular system for E$E$,
5. choice of the interior y$y$ knots μ5${\mu }_{5}$, μ6,,μmy${\mu }_{6},\dots ,{\mu }_{{m}_{y}}$ as μi = yi2${\mu }_{\mathit{i}}={y}_{\mathit{i}-2}$, for i = 5,6,,my$\mathit{i}=5,6,\dots ,{m}_{y}$,
6. formation of the system
 AyCT = ET, $AyCT=ET,$
where Ay${A}_{y}$ is the counterpart of Ax${A}_{x}$ for the y$y$ variable, and C$C$ denotes the mx${m}_{x}$ by my${m}_{y}$ rectangular matrix of values of cij${c}_{ij}$,
7. use of Gaussian elimination to reduce this system to band triangular form,
8. solution of this triangular system for CT${C}^{\mathrm{T}}$ and hence C$C$.
For computational convenience, steps 2$2$ and 3$3$, and likewise steps 6$6$ and 7$7$, are combined so that the formation of Ax${A}_{x}$ and Ay${A}_{y}$ and the reductions to triangular form are carried out one row at a time.

### Evaluation of Computed Spline

The values of the computed spline at the points (xk,yk) $\left({x}_{\mathit{k}},{y}_{\mathit{k}}\right)$, for k = 1,2,,m$\mathit{k}=1,2,\dots ,m$, may be obtained in the double array ff (see nag_fit_2dspline_evalv (e02de)), of length at least m$m$, by the following call:
```[ff, ifail] = e02de(x, y, lamda, mu, c);
```
where M = m$\mathtt{M}=m$ and the coordinates xk${x}_{k}$, yk${y}_{k}$ are stored in X(k)$\mathtt{X}\left(k\right)$, Y(k)$\mathtt{Y}\left(k\right)$. LAMDA, MU and C have the same values as lamda, mu and c output from nag_interp_2d_spline_grid (e01da). (See nag_fit_2dspline_evalv (e02de).)
To evaluate the computed spline on an mx${m}_{x}$ by my${m}_{y}$ rectangular grid of points in the x$x$-y$y$ plane, which is defined by the x$x$ coordinates stored in X(j)$\mathtt{X}\left(\mathit{j}\right)$, for j = 1,2,,mx$\mathit{j}=1,2,\dots ,{m}_{x}$, and the y$y$ coordinates stored in Y(k)$\mathtt{Y}\left(\mathit{k}\right)$, for k = 1,2,,my$\mathit{k}=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 mx × my${\mathbf{mx}}×{\mathbf{my}}$, the following call may be used:
```[fg, ifail] = e02df(x, y, lamda, mu, c);
```
where MX = mx$\mathtt{MX}={m}_{x}$, MY = my$\mathtt{MY}={m}_{y}$. LAMDA, MU and C have the same values as lamda, mu and c output from nag_interp_2d_spline_grid (e01da).
The result of the spline evaluated at grid point (j,k) $\left(j,k\right)$ is returned in element ( MY × (j1) + k $\mathtt{MY}×\left(j-1\right)+k$) of the array FG.

## Example

```function nag_interp_2d_spline_grid_example
x = [1;
1.1;
1.3;
1.5;
1.6;
1.8;
2];
y = [0;
0.1;
0.4;
0.7;
0.9;
1];
f = [1;
1.1;
1.4;
1.7;
1.9;
2;
1.21;
1.31;
1.61;
1.91;
2.11;
2.21;
1.69;
1.79;
2.09;
2.39;
2.59;
2.69;
2.25;
2.35;
2.65;
2.95;
3.15;
3.25;
2.56;
2.66;
2.96;
3.26;
3.46;
3.56;
3.24;
3.34;
3.64;
3.94;
4.14;
4.24;
4;
4.1;
4.4;
4.7;
4.9;
5];
[px, py, lamda, mu, c, ifail] = nag_interp_2d_spline_grid(x, y, f)
```
```

px =

11

py =

10

lamda =

1.0000
1.0000
1.0000
1.0000
1.3000
1.5000
1.6000
2.0000
2.0000
2.0000
2.0000

mu =

0
0
0
0
0.4000
0.7000
1.0000
1.0000
1.0000
1.0000

c =

1.0000
1.1333
1.3667
1.7000
1.9000
2.0000
1.2000
1.3333
1.5667
1.9000
2.1000
2.2000
1.5833
1.7167
1.9500
2.2833
2.4833
2.5833
2.1433
2.2767
2.5100
2.8433
3.0433
3.1433
2.8667
3.0000
3.2333
3.5667
3.7667
3.8667
3.4667
3.6000
3.8333
4.1667
4.3667
4.4667
4.0000
4.1333
4.3667
4.7000
4.9000
5.0000

ifail =

0

```
```function e01da_example
x = [1;
1.1;
1.3;
1.5;
1.6;
1.8;
2];
y = [0;
0.1;
0.4;
0.7;
0.9;
1];
f = [1;
1.1;
1.4;
1.7;
1.9;
2;
1.21;
1.31;
1.61;
1.91;
2.11;
2.21;
1.69;
1.79;
2.09;
2.39;
2.59;
2.69;
2.25;
2.35;
2.65;
2.95;
3.15;
3.25;
2.56;
2.66;
2.96;
3.26;
3.46;
3.56;
3.24;
3.34;
3.64;
3.94;
4.14;
4.24;
4;
4.1;
4.4;
4.7;
4.9;
5];
[px, py, lamda, mu, c, ifail] = e01da(x, y, f)
```
```

px =

11

py =

10

lamda =

1.0000
1.0000
1.0000
1.0000
1.3000
1.5000
1.6000
2.0000
2.0000
2.0000
2.0000

mu =

0
0
0
0
0.4000
0.7000
1.0000
1.0000
1.0000
1.0000

c =

1.0000
1.1333
1.3667
1.7000
1.9000
2.0000
1.2000
1.3333
1.5667
1.9000
2.1000
2.2000
1.5833
1.7167
1.9500
2.2833
2.4833
2.5833
2.1433
2.2767
2.5100
2.8433
3.0433
3.1433
2.8667
3.0000
3.2333
3.5667
3.7667
3.8667
3.4667
3.6000
3.8333
4.1667
4.3667
4.4667
4.0000
4.1333
4.3667
4.7000
4.9000
5.0000

ifail =

0

```

Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013