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_derivm (e02dh)

## Purpose

nag_fit_2dspline_derivm (e02dh) computes the partial derivative (of order ${\nu }_{x}$, ${\nu }_{y}$), of a bicubic spline approximation to a set of data values, from its B-spline representation, at points on a rectangular grid in the $x$-$y$ plane. This function may be used to calculate derivatives of a bicubic spline given in the form produced by nag_interp_2d_spline_grid (e01da), nag_fit_2dspline_panel (e02da), nag_fit_2dspline_grid (e02dc) and nag_fit_2dspline_sctr (e02dd).

## Syntax

[z, ifail] = e02dh(x, y, lamda, mu, c, nux, nuy, 'mx', mx, 'my', my, 'px', px, 'py', py)
[z, ifail] = nag_fit_2dspline_derivm(x, y, lamda, mu, c, nux, nuy, 'mx', mx, 'my', my, 'px', px, 'py', py)

## Description

nag_fit_2dspline_derivm (e02dh) determines the partial derivative $\frac{{\partial }^{{\nu }_{x}+{\nu }_{y}}}{\partial {x}^{{\nu }_{x}}\partial {y}^{{\nu }_{y}}}$ of a smooth bicubic spline approximation $s\left(x,y\right)$ at the set of data points $\left({x}_{q},{y}_{r}\right)$.
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}$, with ${n}_{x}$ and ${n}_{y}$ the total numbers of knots of the computed spline with respect to the $x$ and $y$ variables respectively. For further details, see Hayes and Halliday (1974) for bicubic splines and de Boor (1972) for normalized B-splines. This function is suitable for B-spline representations returned by nag_interp_2d_spline_grid (e01da), nag_fit_2dspline_panel (e02da), nag_fit_2dspline_grid (e02dc) and nag_fit_2dspline_sctr (e02dd).
The partial derivatives can be up to order $2$ in each direction; thus the highest mixed derivative available is $\frac{{\partial }^{4}}{\partial {x}^{2}\partial {y}^{2}}$.
The points in the grid are defined by coordinates ${x}_{\mathit{q}}$, for $\mathit{q}=1,2,\dots ,{m}_{x}$, along the $x$ axis, and coordinates ${y}_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,{m}_{y}$, along the $y$ axis.

## 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{x}\left({\mathbf{mx}}\right)$ – double array
${\mathbf{x}}\left(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}$, on which values of the partial derivative are sought.
Constraint: ${x}_{1}<{x}_{2}<\cdots <{x}_{{m}_{x}}$.
2:     $\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}$ on which values of the partial derivative are sought.
Constraint: ${y}_{1}<{y}_{2}<\cdots <{y}_{{m}_{y}}$.
3:     $\mathrm{lamda}\left({\mathbf{px}}\right)$ – double array
Contains the position of the knots in the $x$-direction of the bicubic spline approximation to be differentiated, e.g., lamda as returned by nag_fit_2dspline_grid (e02dc).
4:     $\mathrm{mu}\left({\mathbf{py}}\right)$ – double array
Contains the position of the knots in the $y$-direction of the bicubic spline approximation to be differentiated, e.g., mu as returned by nag_fit_2dspline_grid (e02dc).
5:     $\mathrm{c}\left(\left({\mathbf{px}}-4\right)×\left({\mathbf{py}}-4\right)\right)$ – double array
The coefficients of the bicubic spline approximation to be differentiated, e.g., c as returned by nag_fit_2dspline_grid (e02dc).
6:     $\mathrm{nux}$int64int32nag_int scalar
Specifies the order, ${\nu }_{x}$ of the partial derivative in the $x$-direction.
Constraint: $0\le {\mathbf{nux}}\le 2$.
7:     $\mathrm{nuy}$int64int32nag_int scalar
Specifies the order, ${\nu }_{y}$ of the partial derivative in the $y$-direction.
Constraint: $0\le {\mathbf{nuy}}\le 2$.

### 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 1$.
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 1$.
3:     $\mathrm{px}$int64int32nag_int scalar
Default: the dimension of the array lamda.
The total number of knots in the $x$-direction of the bicubic spline approximation, e.g., the value nx as returned by nag_fit_2dspline_grid (e02dc).
4:     $\mathrm{py}$int64int32nag_int scalar
Default: the dimension of the array mu.
The total number of knots in the $y$-direction of the bicubic spline approximation, e.g., the value ny as returned by nag_fit_2dspline_grid (e02dc).

### Output Parameters

1:     $\mathrm{z}\left({\mathbf{mx}}×{\mathbf{my}}\right)$ – double array
${\mathbf{z}}\left({m}_{y}×\left(\mathit{q}-1\right)+\mathit{r}\right)$ contains the derivative $\frac{{\partial }^{{\nu }_{x}+{\nu }_{y}}}{{\partial x}^{{\nu }_{x}}{\partial y}^{{\nu }_{y}}}s\left({x}_{q},{y}_{r}\right)$, for $\mathit{q}=1,2,\dots ,{m}_{x}$ and $\mathit{r}=1,2,\dots ,{m}_{y}$.
2:     $\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$
Constraint: $0\le {\mathbf{nux}}\le 2$.
${\mathbf{ifail}}=2$
Constraint: $0\le {\mathbf{nuy}}\le 2$.
${\mathbf{ifail}}=3$
Constraint: ${\mathbf{mx}}\ge 1$.
${\mathbf{ifail}}=4$
Constraint: ${\mathbf{my}}\ge 1$.
${\mathbf{ifail}}=5$
Constraint: ${\mathbf{x}}\left(\mathit{i}-1\right)\le {\mathbf{x}}\left(\mathit{i}\right)$, for $\mathit{i}=2,3,\dots ,{\mathbf{mx}}$.
${\mathbf{ifail}}=6$
Constraint: ${\mathbf{y}}\left(\mathit{i}-1\right)\le {\mathbf{y}}\left(\mathit{i}\right)$, for $\mathit{i}=2,3,\dots ,{\mathbf{my}}$.
${\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 partial derivatives on the given mesh are accurate to machine precision with respect to the supplied bicubic spline. Please refer to Accuracy in nag_interp_2d_spline_grid (e01da), nag_fit_2dspline_panel (e02da), nag_fit_2dspline_grid (e02dc) and nag_fit_2dspline_sctr (e02dd) of the function document for the respective function which calculated the spline approximant for details on the accuracy of that approximation.

None.

## Example

This example reads in values of ${m}_{x}$, ${m}_{y}$, ${x}_{\mathit{q}}$, for $\mathit{q}=1,2,\dots ,{m}_{x}$, 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_fit_2dspline_grid (e02dc) to compute a bicubic spline approximation for one specified value of $S$. Finally it evaluates the spline and its first $x$ derivative at a small sample of points on a rectangular grid by calling nag_fit_2dspline_derivm (e02dh).
```function e02dh_example

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

mx = 11;
my = 9;
start = 'C';
x = [0:0.5:5];
y = [0:0.5:4];
% f = (x+1)*cosy + noise
f = [ 1.0000, 0.88758, 0.54030, 0.070737,-0.41515, ...
-0.80114,-0.97999,-0.93446, -0.65664, ...
1.5000, 1.3564,  0.82045, 0.10611, -0.62422, ...
-1.2317, -1.4850, -1.3047,  -0.98547, ...
2.0600, 1.7552,  1.0806,  0.15147, -0.83229, ...
-1.6023, -1.9700, -1.8729,  -1.4073,  ...
2.5700, 2.1240,  1.3508,  0.17684, -1.0404,  ...
-2.0029, -2.4750, -2.3511,  -1.6741,  ...
3.0000, 2.6427,  1.6309,  0.21221, -1.2484,  ...
-2.2034, -2.9700, -2.8094,  -1.9809, ...
3.5000, 3.1715,  1.8611,  0.24458, -1.4565, ...
-2.8640, -3.2650, -3.2776,  -2.2878, ...
4.0400, 3.5103,  2.0612,  0.28595, -1.6946, ...
-3.2046, -3.9600, -3.7958,  -2.6146, ...
4.5000, 3.9391,  2.4314,  0.31632, -1.8627, ...
-3.6351, -4.4550, -4.2141,  -2.9314, ...
5.0400, 4.3879,  2.7515,  0.35369, -2.0707, ...
-4.0057, -4.9700, -4.6823,  -3.2382, ...
5.5050, 4.8367,  2.9717,  0.38505, -2.2888, ...
-4.4033, -5.4450, -5.1405,  -3.5950, ...
6.0000, 5.2755,  3.2418,  0.42442, -2.4769, ...
-4.8169, -5.9300, -5.6387,  -3.9319];
s = 0.1;
ngx = 6;
xlo = 0;
xhi = 5;
ngy = 5;
ylo = 0;
yhi = 4;

nxest = mx + 4;
nyest = my + 4;
lamda = zeros(nxest, 1);
mu    = zeros(nyest, 1);
wrk   = zeros(4*(mx+my) + 11*(nxest+nyest) + nxest*my + max(my,nxest) + 54, 1);
iwrk  = zeros(3 + mx + my + nxest + nyest, 1, 'int64');

% Determine the spline approximation
[nx, lamda, ny, mu, c, fp, wrk, iwrk, ifail] = ...
e02dc( ...
start, x, y, f, s, int64(0), lamda, int64(0), mu, wrk, iwrk);

fprintf('\nSpline fit used smoothing factor S = %13.4e.\n', s);
fprintf('Number of knots in each direction  = %d, %d\n', nx, ny);
fprintf('\nSum of squared residuals           = %13.4e.\n', fp);

% Evaluate the spline derivative on a different rectangular grid with
% ngx*ngy points over the domain (xlo to xhi) x (ylo to yhi).
gridx = [xlo:(xhi-xlo)/(ngx-1):xhi];
gridy = [ylo:(yhi-ylo)/(ngy-1):yhi];

% Evaluate spline (nux=nuy=0)
nux = int64(0);
nuy = int64(0);
[z, ifail] = e02dh( ...
gridx, gridy, lamda(1:nx), mu(1:ny), c, nux, nuy);

nux = int64(1);
% Evaluate spline partial derivative of order (nux, nuy)
[zder, ifail] = e02dh( ...
gridx, gridy, lamda(1:nx), mu(1:ny), c, nux, nuy);

% Spline partial derivatives to evaluate have order nux,nuy.
fprintf('\nDerivative of spline has order nux, nuy = %d, %d.\n\n', nux, nuy);

% Print the spline evaluations
rlabs = cell(size(gridy));
for i = 1:ngy
rlabs{i} = num2str(gridy(i));
end
clabs = cell(size(gridx));
for i = 1:ngx
clabs{i} = num2str(gridx(i));
end
title = 'Spline evaluated on X-Y grid (X across, Y down):';
[ifail] = x04cb('G', 'N', reshape(z, ngy, ngx), 'F8.3', title, ...
'C', rlabs, 'C', clabs, int64(80), int64(0));
fprintf('\n');
title = 'Spline derivative evaluated on X-Y grid:';
[ifail] = x04cb('G', 'N', reshape(zder, ngy, ngx), 'F8.3', title, ...
'C', rlabs, 'C', clabs, int64(80), int64(0));

```
```e02dh example results

Spline fit used smoothing factor S =    1.0000e-01.
Number of knots in each direction  = 10, 13

Sum of squared residuals           =    1.0004e-01.

Derivative of spline has order nux, nuy = 1, 0.

Spline evaluated on X-Y grid (X across, Y down):
0       1       2       3       4       5
0    0.992   2.043   3.029   4.014   5.021   5.997
1    0.541   1.088   1.607   2.142   2.705   3.239
2   -0.417  -0.829  -1.241  -1.665  -2.083  -2.485
3   -0.978  -1.975  -2.914  -3.913  -4.965  -5.924
4   -0.648  -1.363  -1.991  -2.606  -3.251  -3.933

Spline derivative evaluated on X-Y grid:
0       1       2       3       4       5
0    1.093   1.013   0.970   1.004   1.001   0.939
1    0.565   0.531   0.515   0.558   0.559   0.499
2   -0.429  -0.404  -0.421  -0.423  -0.412  -0.389
3   -1.060  -0.951  -0.949  -1.048  -1.031  -0.861
4   -0.779  -0.661  -0.608  -0.628  -0.663  -0.701
```