# NAG Library Routine Document

## 1Purpose

e01sgf generates a two-dimensional interpolant to a set of scattered data points, using a modified Shepard method.

## 2Specification

Fortran Interface
 Subroutine e01sgf ( m, x, y, f, nw, nq, iq, liq, rq, lrq,
 Integer, Intent (In) :: m, nw, nq, liq, lrq Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: iq(liq) Real (Kind=nag_wp), Intent (In) :: x(m), y(m), f(m) Real (Kind=nag_wp), Intent (Out) :: rq(lrq)
#include nagmk26.h
 void e01sgf_ (const Integer *m, const double x[], const double y[], const double f[], const Integer *nw, const Integer *nq, Integer iq[], const Integer *liq, double rq[], const Integer *lrq, Integer *ifail)

## 3Description

e01sgf constructs a smooth function $Q\left(x,y\right)$ which interpolates a set of $m$ scattered data points $\left({x}_{r},{y}_{r},{f}_{r}\right)$, for $r=1,2,\dots ,m$, using a modification of Shepard's method. The surface is continuous and has continuous first partial derivatives.
The basic Shepard (1968) method interpolates the input data with the weighted mean
 $Qx,y=∑r=1mwrx,yqr ∑r=1mwrx,y ,$
where ${q}_{r}={f}_{r}$, ${w}_{r}\left(x,y\right)=\frac{1}{{d}_{r}^{2}}$ and ${d}_{r}^{2}={\left(x-{x}_{r}\right)}^{2}+{\left(y-{y}_{r}\right)}^{2}$.
The basic method is global in that the interpolated value at any point depends on all the data, but this routine uses a modification (see Franke and Nielson (1980) and Renka (1988a)), whereby the method becomes local by adjusting each ${w}_{r}\left(x,y\right)$ to be zero outside a circle with centre $\left({x}_{r},{y}_{r}\right)$ and some radius ${R}_{w}$. Also, to improve the performance of the basic method, each ${q}_{r}$ above is replaced by a function ${q}_{r}\left(x,y\right)$, which is a quadratic fitted by weighted least squares to data local to $\left({x}_{r},{y}_{r}\right)$ and forced to interpolate $\left({x}_{r},{y}_{r},{f}_{r}\right)$. In this context, a point $\left(x,y\right)$ is defined to be local to another point if it lies within some distance ${R}_{q}$ of it. Computation of these quadratics constitutes the main work done by this routine.
The efficiency of the routine is further enhanced by using a cell method for nearest neighbour searching due to Bentley and Friedman (1979).
The radii ${R}_{w}$ and ${R}_{q}$ are chosen to be just large enough to include ${N}_{w}$ and ${N}_{q}$ data points, respectively, for user-supplied constants ${N}_{w}$ and ${N}_{q}$. Default values of these arguments are provided by the routine, and advice on alternatives is given in Section 9.2.
This routine is derived from the routine QSHEP2 described by Renka (1988b).
Values of the interpolant $Q\left(x,y\right)$ generated by this routine, and its first partial derivatives, can subsequently be evaluated for points in the domain of the data by a call to e01shf.

## 4References

Bentley J L and Friedman J H (1979) Data structures for range searching ACM Comput. Surv. 11 397–409
Franke R and Nielson G (1980) Smooth interpolation of large sets of scattered data Internat. J. Num. Methods Engrg. 15 1691–1704
Renka R J (1988a) Multivariate interpolation of large sets of scattered data ACM Trans. Math. Software 14 139–148
Renka R J (1988b) Algorithm 660: QSHEP2D: Quadratic Shepard method for bivariate interpolation of scattered data ACM Trans. Math. Software 14 149–150
Shepard D (1968) A two-dimensional interpolation function for irregularly spaced data Proc. 23rd Nat. Conf. ACM 517–523 Brandon/Systems Press Inc., Princeton

## 5Arguments

1:     $\mathbf{m}$ – IntegerInput
On entry: $m$, the number of data points.
Constraint: ${\mathbf{m}}\ge 6$.
2:     $\mathbf{x}\left({\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayInput
3:     $\mathbf{y}\left({\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the Cartesian coordinates of the data points $\left({x}_{\mathit{r}},{y}_{\mathit{r}}\right)$, for $\mathit{r}=1,2,\dots ,m$.
Constraint: these coordinates must be distinct, and must not all be collinear.
4:     $\mathbf{f}\left({\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: ${\mathbf{f}}\left(\mathit{r}\right)$ must be set to the data value ${f}_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,m$.
5:     $\mathbf{nw}$ – IntegerInput
On entry: the number ${N}_{w}$ of data points that determines each radius of influence ${R}_{w}$, appearing in the definition of each of the weights ${w}_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,m$ (see Section 3). Note that ${R}_{w}$ is different for each weight. If ${\mathbf{nw}}\le 0$ the default value ${\mathbf{nw}}=\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(19,{\mathbf{m}}-1\right)$ is used instead.
Constraint: ${\mathbf{nw}}\le \mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(40,{\mathbf{m}}-1\right)$.
6:     $\mathbf{nq}$ – IntegerInput
On entry: the number ${N}_{q}$ of data points to be used in the least squares fit for coefficients defining the nodal functions ${q}_{r}\left(x,y\right)$ (see Section 3). If ${\mathbf{nq}}\le 0$ the default value ${\mathbf{nq}}=\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(13,{\mathbf{m}}-1\right)$ is used instead.
Constraint: ${\mathbf{nq}}\le 0$ or $5\le {\mathbf{nq}}\le \mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(40,{\mathbf{m}}-1\right)$.
7:     $\mathbf{iq}\left({\mathbf{liq}}\right)$ – Integer arrayOutput
On exit: integer data defining the interpolant $Q\left(x,y\right)$.
8:     $\mathbf{liq}$ – IntegerInput
On entry: the dimension of the array iq as declared in the (sub)program from which e01sgf is called.
Constraint: ${\mathbf{liq}}\ge 2×{\mathbf{m}}+1$.
9:     $\mathbf{rq}\left({\mathbf{lrq}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: real data defining the interpolant $Q\left(x,y\right)$.
10:   $\mathbf{lrq}$ – IntegerInput
On entry: the dimension of the array rq as declared in the (sub)program from which e01sgf is called.
Constraint: ${\mathbf{lrq}}\ge 6×{\mathbf{m}}+5$.
11:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{m}}<6$, or $0<{\mathbf{nq}}<5$, or ${\mathbf{nq}}>\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(40,{\mathbf{m}}-1\right)$, or ${\mathbf{nw}}>\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(40,{\mathbf{m}}-1\right)$, or ${\mathbf{liq}}<2×{\mathbf{m}}+1$, or ${\mathbf{lrq}}<6×{\mathbf{m}}+5$.
${\mathbf{ifail}}=2$
 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}}=3$
 On entry, all the data points are collinear. No unique solution exists.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

On successful exit, the function generated interpolates the input data exactly and has quadratic accuracy.

## 8Parallelism and Performance

e01sgf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
e01sgf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

### 9.1Timing

The time taken for a call to e01sgf will depend in general on the distribution of the data points. If x and y are uniformly randomly distributed, then the time taken should be $\mathit{O}\left({\mathbf{m}}\right)$. At worst $\mathit{O}\left({{\mathbf{m}}}^{2}\right)$ time will be required.

### 9.2Choice of ${N}_{w}$ and ${N}_{q}$

Default values of the arguments ${N}_{w}$ and ${N}_{q}$ may be selected by calling e01sgf with ${\mathbf{nw}}\le 0$ and ${\mathbf{nq}}\le 0$. These default values may well be satisfactory for many applications.
If non-default values are required they must be supplied to e01sgf through positive values of nw and nq. Increasing these arguments makes the method less local. This may increase the accuracy of the resulting interpolant at the expense of increased computational cost. The default values ${\mathbf{nw}}=\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(19,{\mathbf{m}}-1\right)$ and ${\mathbf{nq}}=\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(13,{\mathbf{m}}-1\right)$ have been chosen on the basis of experimental results reported in Renka (1988a). In these experiments the error norm was found to vary smoothly with ${N}_{w}$ and ${N}_{q}$, generally increasing monotonically and slowly with distance from the optimal pair. The method is not therefore thought to be particularly sensitive to the argument values. For further advice on the choice of these arguments see Renka (1988a).

### 9.3Internal Changes

Internal changes have been made to this routine as follows:
• At Mark 26: The algorithm used by this routine, based on a Modified Shepard method, has been changed to produce more reliable results for some data sets which were previously not well handled. In addition, handling of evaluation points which are far away from the original data points has been improved by use of an extrapolation method which returns useful results rather than just an error message as was done at earlier Marks.
• At Mark 26.1: The algorithm has undergone further changes which enable it to work better on certain data sets, for example data presented on a regular grid. The results returned when evaluating the function at points which are not in the original data set used to construct the interpolating function are now likely to be slightly different from those returned at previous Marks of the Library, but the function still interpolates the original data.
For details of all known issues which have been reported for the NAG Library please refer to the Known Issues list.

## 10Example

This program reads in a set of $30$ data points and calls e01sgf to construct an interpolating function $Q\left(x,y\right)$. It then calls e01shf to evaluate the interpolant at a set of points.
Note that this example is not typical of a realistic problem: the number of data points would normally be larger.

### 10.1Program Text

Program Text (e01sgfe.f90)

### 10.2Program Data

Program Data (e01sgfe.d)

### 10.3Program Results

Program Results (e01sgfe.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017