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_scat (e01sa)

## Purpose

nag_interp_2d_scat (e01sa) generates a two-dimensional surface interpolating a set of scattered data points, using the method of Renka and Cline.

## Syntax

[triang, grads, ifail] = e01sa(x, y, f, 'm', m)
[triang, grads, ifail] = nag_interp_2d_scat(x, y, f, 'm', m)

## Description

nag_interp_2d_scat (e01sa) constructs an interpolating surface $F\left(x,y\right)$ through a set of $m$ scattered data points $\left({x}_{\mathit{r}},{y}_{\mathit{r}},{f}_{\mathit{r}}\right)$, for $\mathit{r}=1,2,\dots ,m$, using a method due to Renka and Cline. In the $\left(x,y\right)$ plane, the data points must be distinct. The constructed surface is continuous and has continuous first derivatives.
The method involves firstly creating a triangulation with all the $\left(x,y\right)$ data points as nodes, the triangulation being as nearly equiangular as possible (see Cline and Renka (1984)). Then gradients in the $x$- and $y$-directions are estimated at node $\mathit{r}$, for $\mathit{r}=1,2,\dots ,m$, as the partial derivatives of a quadratic function of $x$ and $y$ which interpolates the data value ${f}_{r}$, and which fits the data values at nearby nodes (those within a certain distance chosen by the algorithm) in a weighted least squares sense. The weights are chosen such that closer nodes have more influence than more distant nodes on derivative estimates at node $r$. The computed partial derivatives, with the ${f}_{r}$ values, at the three nodes of each triangle define a piecewise polynomial surface of a certain form which is the interpolant on that triangle. See Renka and Cline (1984) for more detailed information on the algorithm, a development of that by Lawson (1977). The code is derived from Renka (1984).
The interpolant $F\left(x,y\right)$ can subsequently be evaluated at any point $\left(x,y\right)$ inside or outside the domain of the data by a call to nag_interp_2d_scat_eval (e01sb). Points outside the domain are evaluated by extrapolation.

## References

Cline A K and Renka R L (1984) A storage-efficient method for construction of a Thiessen triangulation Rocky Mountain J. Math. 14 119–139
Lawson C L (1977) Software for ${C}^{1}$ surface interpolation Mathematical Software III (ed J R Rice) 161–194 Academic Press
Renka R L (1984) Algorithm 624: triangulation and interpolation of arbitrarily distributed points in the plane ACM Trans. Math. Software 10 440–442
Renka R L and Cline A K (1984) A triangle-based ${C}^{1}$ interpolation method Rocky Mountain J. Math. 14 223–237

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{x}\left({\mathbf{m}}\right)$ – double array
2:     $\mathrm{y}\left({\mathbf{m}}\right)$ – double array
3:     $\mathrm{f}\left({\mathbf{m}}\right)$ – double array
The coordinates of the $\mathit{r}$th data point, for $\mathit{r}=1,2,\dots ,m$. The data points are accepted in any order, but see Further Comments.
Constraint: the $\left(x,y\right)$ nodes must not all be collinear, and each node must be unique.

### Optional Input Parameters

1:     $\mathrm{m}$int64int32nag_int scalar
Default: the dimension of the arrays x, y, f. (An error is raised if these dimensions are not equal.)
$m$, the number of data points.
Constraint: ${\mathbf{m}}\ge 3$.

### Output Parameters

1:     $\mathrm{triang}\left(7×{\mathbf{m}}\right)$int64int32nag_int array
A data structure defining the computed triangulation, in a form suitable for passing to nag_interp_2d_scat_eval (e01sb).
2:     $\mathrm{grads}\left(2,{\mathbf{m}}\right)$ – double array
The estimated partial derivatives at the nodes, in a form suitable for passing to nag_interp_2d_scat_eval (e01sb). The derivatives at node $\mathit{r}$ with respect to $x$ and $y$ are contained in ${\mathbf{grads}}\left(1,\mathit{r}\right)$ and ${\mathbf{grads}}\left(2,\mathit{r}\right)$ respectively, for $\mathit{r}=1,2,\dots ,m$.
3:     $\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{m}}<3$.
${\mathbf{ifail}}=2$
 On entry, all the (x,y) pairs are collinear.
${\mathbf{ifail}}=3$
 On entry, $\left({\mathbf{x}}\left(i\right),{\mathbf{y}}\left(i\right)\right)=\left({\mathbf{x}}\left(j\right),{\mathbf{y}}\left(j\right)\right)$ for some $i\ne j$.
${\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

On successful exit, the computational errors should be negligible in most situations but you should always check the computed surface for acceptability, by drawing contours for instance. The surface always interpolates the input data exactly.

The time taken for a call of nag_interp_2d_scat (e01sa) is approximately proportional to the number of data points, $m$. The function is more efficient if, before entry, the values in x, y and f are arranged so that the x array is in ascending order.

## Example

This example reads in a set of $30$ data points and calls nag_interp_2d_scat (e01sa) to construct an interpolating surface. It then calls nag_interp_2d_scat_eval (e01sb) to evaluate the interpolant at a sample of points on a rectangular grid.
Note that this example is not typical of a realistic problem: the number of data points would normally be larger, and the interpolant would need to be evaluated on a finer grid to obtain an accurate plot, say.
```function e01sa_example

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

% Scattered Grid Data
x = [11.16; 12.85; 19.85; 19.72; 15.91;  0.00; 20.87;  3.45; 14.26; ...
17.43; 22.80;  7.58; 25.00;  0.00;  9.66;  5.22; 17.25; 25.00; ...
12.13; 22.23; 11.52; 15.20;  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.00; 20.00; 12.78; 17.87; ...
3.46; 12.39;  1.98; 11.87;  0.00; 20.00; 14.66; 19.57;  3.87; ...
10.79;  6.21;  8.53;  0.00; 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.30; 34.60;  5.74; 41.24; 10.74; ...
18.60;  5.47; 29.87;  4.40; 58.20;  4.73; 40.36;  6.43;  8.74; ...
13.71; 10.25; 15.74; 21.60; 19.31; 12.11; 53.10; 49.43;  3.25; ...
28.63;  5.52; 44.08];
% Triangulate and obtain details of interpolant

% Convert to for for trimesh plotter.
n       = size(x,1);
[tri,k] = triang2tri(triang,n);
fprintf('Number of triangles in triangulation = %d\n\n',k);

% Display mesh
fig1 = figure;
trimesh(tri(1:k,1:3),x,y,f);
xlabel('x');
ylabel('y');
zlabel('f(x,y)')
title('Triangulation of scattered data using e01sa');
view(51,18);
% Evaluate interpolant at on regular mesh (px,py)
px = [3:3:21];
py = [2:3:17];
for i = 1:6
for j = 1:7
[pf(i,j), ifail] = e01sb( ...
x, y, f, triang, grads, px(j), py(i));
end
end

% Display interpolated values
matrix = 'General';
diag = 'Non-unit';
format = 'F7.2';
mtitl  = 'Spline evaluated on a regular mesh (x across, y down):';
chlab  = 'Character';
rlabs  = cellstr(num2str(py'));
clabs  = cellstr(num2str(px'));
ncols  = int64(80);
indent = int64(0);
[ifail] =  x04cb( ...
matrix, diag, pf, format, mtitl, chlab, ...
rlabs, chlab, clabs, ncols, indent);

function [tri,k] = triang2tri(triang,n)
%  Convert triang array to form that MATLAB trimesh function can use

max_t = int64(2*n-5);
tri   = int64(zeros(max_t,3));
iend  = int64(0);
k     = iend;
t     = int64([0;0;0]);

for i = 1:n
% set up loop of nodes connected to node i
ibeg = iend + 1;
iend = triang(6*n+i);
% first connection setup
t(1) = i;
t(2) = triang(ibeg);
% loop over remaining connections
ibeg = ibeg + 1;
for j = ibeg:iend
t(3) = triang(j);
if t(3)>0
if t(2)>i || t(3)>i
% new triangle here
k = k + 1;
tri(k,1:3) = t(1:3);
end
% shuffle down for next connected node
t(2) = t(3);
end
end
end
```
```e01sa example results

Number of triangles in triangulation = 88

Spline evaluated on a regular mesh (x across, y down):
3      6      9     12     15     18     21
2   43.52  33.91  26.59  22.23  21.15  18.67  14.88
5   40.49  29.26  22.51  20.72  19.30  16.72  12.87
8   37.90  23.97  16.79  16.43  15.46  13.02   9.30
11   38.55  25.25  16.72  13.83  13.08  10.71   6.88
14   47.61  36.66  22.87  14.02  13.44  11.20   6.46
17   41.25  27.62  18.03  12.29  11.68   9.09   5.37
``` 