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$-$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 $\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=1mx∑j=1mycijMixNjy,$
such that
 $sxq,yr=fq,r,$
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}$, and the ${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 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 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:     $\mathrm{x}\left({\mathbf{mx}}\right)$ – double array
2:     $\mathrm{y}\left({\mathbf{my}}\right)$ – double array
${\mathbf{x}}\left(\mathit{q}\right)$ and ${\mathbf{y}}\left(\mathit{r}\right)$ must contain ${x}_{\mathit{q}}$, for $\mathit{q}=1,2,\dots ,{m}_{x}$, and ${y}_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,{m}_{y}$, respectively.
Constraints:
• ${\mathbf{x}}\left(\mathit{q}\right)<{\mathbf{x}}\left(\mathit{q}+1\right)$, for $\mathit{q}=1,2,\dots ,{m}_{x}-1$;
• ${\mathbf{y}}\left(\mathit{r}\right)<{\mathbf{y}}\left(\mathit{r}+1\right)$, for $\mathit{r}=1,2,\dots ,{m}_{y}-1$.
3:     $\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 ${f}_{\mathit{q},\mathit{r}}$, for $\mathit{q}=1,2,\dots ,{m}_{x}$ and $\mathit{r}=1,2,\dots ,{m}_{y}$.

Optional Input Parameters

1:     $\mathrm{mx}$int64int32nag_int scalar
2:     $\mathrm{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 ${m}_{x}$ and ${m}_{y}$ respectively, the number of points along the $x$ and $y$ axis that define the rectangular grid.
Constraint: ${\mathbf{mx}}\ge 4$ and ${\mathbf{my}}\ge 4$.

Output Parameters

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

Accuracy

The main sources of rounding errors are in steps $2$, $3$, $6$ and $7$ of the algorithm described in Outline of Method Used. It can be shown (see Cox (1975)) that the matrix ${A}_{x}$ formed in step $2$ has elements differing relatively from their true values by at most a small multiple of $3\epsilon$, where $\epsilon$ is the machine precision. ${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$ and $7$. Thus the complete process is numerically stable.

Timing

The time taken by nag_interp_2d_spline_grid (e01da) is approximately proportional to ${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$-knots ${\lambda }_{5}$, ${\lambda }_{6},\dots ,{\lambda }_{{m}_{x}}$ as ${\lambda }_{\mathit{i}}={x}_{\mathit{i}-2}$, for $\mathit{i}=5,6,\dots ,{m}_{x}$,
2. formation of the system
 $AxE=F,$
where ${A}_{x}$ is a band matrix of order ${m}_{x}$ and bandwidth $4$, containing in its $q$th row the values at ${x}_{q}$ of the B-splines in $x$, ${\mathbf{f}}$ is the ${m}_{x}$ by ${m}_{y}$ rectangular matrix of values ${f}_{q,r}$, and $E$ denotes an ${m}_{x}$ by ${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$,
5. choice of the interior $y$ knots ${\mu }_{5}$, ${\mu }_{6},\dots ,{\mu }_{{m}_{y}}$ as ${\mu }_{\mathit{i}}={y}_{\mathit{i}-2}$, for $\mathit{i}=5,6,\dots ,{m}_{y}$,
6. formation of the system
 $AyCT=ET,$
where ${A}_{y}$ is the counterpart of ${A}_{x}$ for the $y$ variable, and $C$ denotes the ${m}_{x}$ by ${m}_{y}$ rectangular matrix of values of ${c}_{ij}$,
7. use of Gaussian elimination to reduce this system to band triangular form,
8. solution of this triangular system for ${C}^{\mathrm{T}}$ and hence $C$.
For computational convenience, steps $2$ and $3$, and likewise steps $6$ and $7$, are combined so that the formation of ${A}_{x}$ and ${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 $\left({x}_{\mathit{k}},{y}_{\mathit{k}}\right)$, for $\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$, by the following call:
```[ff, ifail] = e02de(x, y, lamda, mu, c);
```
where $\mathtt{M}=m$ and the coordinates ${x}_{k}$, ${y}_{k}$ are stored in $\mathtt{X}\left(k\right)$, $\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 ${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{j}\right)$, for $\mathit{j}=1,2,\dots ,{m}_{x}$, and the $y$ coordinates stored in $\mathtt{Y}\left(\mathit{k}\right)$, for $\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 ${\mathbf{mx}}×{\mathbf{my}}$, the following call may be used:
```[fg, 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_interp_2d_spline_grid (e01da).
The result of the spline evaluated at grid point $\left(j,k\right)$ is returned in element ($\mathtt{MY}×\left(j-1\right)+k$) of the array FG.

Example

This example reads in values of ${m}_{x}$, ${x}_{\mathit{q}}$, for $\mathit{q}=1,2,\dots ,{m}_{x}$, ${m}_{y}$ and ${y}_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,{m}_{y}$, followed by values of the ordinates ${f}_{q,r}$ defined at the grid points $\left({x}_{q},{y}_{r}\right)$.
It then calls nag_interp_2d_spline_grid (e01da) to compute a bicubic spline interpolant of the data values, and prints the values of the knots and B-spline coefficients. Finally it evaluates the spline at a small sample of points on a rectangular grid.
```function e01da_example

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

x = [1.0   1.10    1.30    1.50    1.60    1.80    2.0];
f = [1.0   1.21    1.69    2.25    2.56    3.24    4.0;
1.1   1.31    1.79    2.35    2.66    3.34    4.1;
1.4   1.61    2.09    2.65    2.96    3.64    4.4;
1.7   1.91    2.39    2.95    3.26    3.94    4.7;
1.9   2.11    2.59    3.15    3.46    4.14    4.9;
2.0   2.21    2.69    3.25    3.56    4.24    5.0];
y = [0.0;
0.1;
0.4;
0.7;
0.9;
1.0];

[px, py, lamda, mu, c, ifail] = e01da( ...
x, y, f);

% Display the knot sets, LAMDA and MU.

fprintf('\n               i   knot lamda(i)      j     knot mu(j)\n');
j = 4:min(px,py)-3;
fprintf('%16d%12.4f%11d%12.4f\n',[j' lamda(j) j' mu(j)]');
if (px>py);
j = py-2:px-3;
fprintf('%16d%12.4f\n',[j' lamda(j)]');
elseif (px<py);
j = px-2:py-3;
fprintf('%16d%12.4f\n',[j' mu(j)]')
end

% Display the spline coefficients.
c = reshape(c, size(f'));
fprintf('\n');
disp('The B-Spline coefficients:');
disp(c');

% Evaluate spline on regular 6-by-6 mesh
dx = (x(end)-x(1))/5;
dy = (y(end)-y(1))/5;
tx = [x(1):dx:x(end)];
ty = [y(1):dy:y(end)]';

[ff, ifail] = e02df( ...
tx, ty, lamda, mu, c);

% Display evaluations as matrix
ff = reshape(ff,[6,6]);

matrix = 'General';
diag = 'Non-unit';
format = 'F8.3';
title  = 'Spline evaluated on a regular mesh (x across, y down):';
chlab  = 'Character';
rlabs  = cellstr(num2str(tx'));
clabs  = cellstr(num2str(ty));
ncols  = int64(80);
indent = int64(0);
[ifail] =  x04cb( ...
matrix, diag, ff, format, title, chlab, ...
rlabs, chlab, clabs, ncols, indent);

```
```e01da example results

i   knot lamda(i)      j     knot mu(j)
4      1.0000          4      0.0000
5      1.0000          5      0.0000
6      2.0000          6      1.0000
7      2.0000          7      1.0000
8      2.0000

The B-Spline coefficients:
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

Spline evaluated on a regular mesh (x across, y down):
0     0.2     0.4     0.6     0.8       1
1    1.000   1.440   1.960   2.560   3.240   4.000
1.2    1.200   1.640   2.160   2.760   3.440   4.200
1.4    1.400   1.840   2.360   2.960   3.640   4.400
1.6    1.600   2.040   2.560   3.160   3.840   4.600
1.8    1.800   2.240   2.760   3.360   4.040   4.800
2    2.000   2.440   2.960   3.560   4.240   5.000
```