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_1d_spline (e01ba)

## Purpose

nag_interp_1d_spline (e01ba) determines a cubic spline interpolant to a given set of data.

## Syntax

[lamda, c, ifail] = e01ba(x, y, 'm', m)
[lamda, c, ifail] = nag_interp_1d_spline(x, y, 'm', m)

## Description

nag_interp_1d_spline (e01ba) determines a cubic spline $s\left(x\right)$, defined in the range ${x}_{1}\le x\le {x}_{m}$, which interpolates (passes exactly through) the set of data points $\left({x}_{\mathit{i}},{y}_{\mathit{i}}\right)$, for $\mathit{i}=1,2,\dots ,m$, where $m\ge 4$ and ${x}_{1}<{x}_{2}<\cdots <{x}_{m}$. Unlike some other spline interpolation algorithms, derivative end conditions are not imposed. The spline interpolant chosen has $m-4$ interior knots ${\lambda }_{5},{\lambda }_{6},\dots ,{\lambda }_{m}$, which are set to the values of ${x}_{3},{x}_{4},\dots ,{x}_{m-2}$ respectively. This spline is represented in its B-spline form (see Cox (1975)):
 $sx=∑i=1mciNix,$
where ${N}_{i}\left(x\right)$ denotes the normalized B-spline of degree $3$, defined upon the knots ${\lambda }_{i},{\lambda }_{i+1},\dots ,{\lambda }_{i+4}$, and ${c}_{i}$ denotes its coefficient, whose value is to be determined by the function.
The use of B-splines requires eight additional knots ${\lambda }_{1}$, ${\lambda }_{2}$, ${\lambda }_{3}$, ${\lambda }_{4}$, ${\lambda }_{m+1}$, ${\lambda }_{m+2}$, ${\lambda }_{m+3}$ and ${\lambda }_{m+4}$ to be specified; nag_interp_1d_spline (e01ba) sets the first four of these to ${x}_{1}$ and the last four to ${x}_{m}$.
The algorithm for determining the coefficients is as described in Cox (1975) except that $QR$ factorization is used instead of $LU$ decomposition. The implementation of the algorithm involves setting up appropriate information for the related function nag_fit_1dspline_knots (e02ba) followed by a call of that function. (See nag_fit_1dspline_knots (e02ba) for further details.)
Values of the spline interpolant, or of its derivatives or definite integral, can subsequently be computed as detailed in Further Comments.

## References

Cox M G (1975) An algorithm for spline interpolation J. Inst. Math. Appl. 15 95–108
Cox M G (1977) A survey of numerical methods for data and function approximation The State of the Art in Numerical Analysis (ed D A H Jacobs) 627–668 Academic Press

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{x}\left({\mathbf{m}}\right)$ – double array
${\mathbf{x}}\left(\mathit{i}\right)$ must be set to ${x}_{\mathit{i}}$, the $\mathit{i}$th data value of the independent variable $x$, for $\mathit{i}=1,2,\dots ,m$.
Constraint: ${\mathbf{x}}\left(\mathit{i}\right)<{\mathbf{x}}\left(\mathit{i}+1\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{m}}-1$.
2:     $\mathrm{y}\left({\mathbf{m}}\right)$ – double array
${\mathbf{y}}\left(\mathit{i}\right)$ must be set to ${y}_{\mathit{i}}$, the $\mathit{i}$th data value of the dependent variable $y$, for $\mathit{i}=1,2,\dots ,m$.

### Optional Input Parameters

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

### Output Parameters

1:     $\mathrm{lamda}\left(\mathit{lck}\right)$ – double array
$\mathit{lck}={\mathbf{m}}+4$.
The value of ${\lambda }_{\mathit{i}}$, the $\mathit{i}$th knot, for $\mathit{i}=1,2,\dots ,m+4$.
2:     $\mathrm{c}\left(\mathit{lck}\right)$ – double array
$\mathit{lck}={\mathbf{m}}+4$.
The coefficient ${c}_{\mathit{i}}$ of the B-spline ${N}_{\mathit{i}}\left(x\right)$, for $\mathit{i}=1,2,\dots ,m$. The remaining elements of the array are not used.
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}}<4$, or $\mathit{lck}<{\mathbf{m}}+4$, or $\mathit{lwrk}<6×{\mathbf{m}}+16$.
${\mathbf{ifail}}=2$
The x-values fail to satisfy the condition
${\mathbf{x}}\left(1\right)<{\mathbf{x}}\left(2\right)<{\mathbf{x}}\left(3\right)<\cdots <{\mathbf{x}}\left({\mathbf{m}}\right)$.
${\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 rounding errors incurred are such that the computed spline is an exact interpolant for a slightly perturbed set of ordinates ${y}_{i}+\delta {y}_{i}$. The ratio of the root-mean-square value of the $\delta {y}_{i}$ to that of the ${y}_{i}$ is no greater than a small multiple of the relative machine precision.

The time taken by nag_interp_1d_spline (e01ba) is approximately proportional to $m$.
All the ${x}_{i}$ are used as knot positions except ${x}_{2}$ and ${x}_{m-1}$. This choice of knots (see Cox (1977)) means that $s\left(x\right)$ is composed of $m-3$ cubic arcs as follows. If $m=4$, there is just a single arc space spanning the whole interval ${x}_{1}$ to ${x}_{4}$. If $m\ge 5$, the first and last arcs span the intervals ${x}_{1}$ to ${x}_{3}$ and ${x}_{m-2}$ to ${x}_{m}$ respectively. Additionally if $m\ge 6$, the $\mathit{i}$th arc, for $\mathit{i}=2,3,\dots ,m-4$, spans the interval ${x}_{\mathit{i}+1}$ to ${x}_{\mathit{i}+2}$.
After the call
```[lamda, c, ifail] = e01ba(x, y, lck);
```
the following operations may be carried out on the interpolant $s\left(x\right)$.
The value of $s\left(x\right)$ at $x={\mathbf{x}}$ can be provided in the double variable s by the call
```[s, ifail] = e02bb(lamda, c, x);
```
(see nag_fit_1dspline_eval (e02bb)).
The values of $s\left(x\right)$ and its first three derivatives at $x={\mathbf{x}}$ can be provided in the double array s of dimension $4$, by the call
```[s, ifail] = e02bc(lamda, c, x, left);
```
(see nag_fit_1dspline_deriv (e02bc)).
Here left must specify whether the left- or right-hand value of the third derivative is required (see nag_fit_1dspline_deriv (e02bc) for details).
The value of the integral of $s\left(x\right)$ over the range ${x}_{1}$ to ${x}_{m}$ can be provided in the double variable dint by
```[dint, ifail] = e02bd(lamda, c);
```
(see nag_fit_1dspline_integ (e02bd)).

## Example

This example sets up data from $7$ values of the exponential function in the interval $0$ to $1$. nag_interp_1d_spline (e01ba) is then called to compute a spline interpolant to these data.
The spline is evaluated by nag_fit_1dspline_eval (e02bb), at the data points and at points halfway between each adjacent pair of data points, and the spline values and the values of ${e}^{x}$ are printed out.
```function e01ba_example

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

x = [0     0.2     0.4     0.6     0.75     0.9     1];
y = exp(x);
[lamda, c, ifail] = e01ba(x, y);

fprintf('\n   j    knot lamda(j+2)   b-spline coeff c(j)\n\n');
j = 1;
fprintf('%4d%35.4f\n', j, c(1));
m = size(x,2);
for j = 2:m - 1;
fprintf('%4d%15.4f%20.4f\n', j, lamda(j+2), c(j));
end
fprintf('%4d%35.4f\n', m, c(m));
fprintf('\n   R        Abscissa            Ordinate             Spline\n\n');
for r = 1:m;
[fit, ifail] = e02bb( ...
lamda, c, x(r));

fprintf('%4d%15.4f%20.4f%20.4f\n', r, x(r), y(r), fit);
if r<m;
xarg = (x(r)+x(r+1))/2;

[fit, ifail] = e02bb( ...
lamda, c, xarg);
fprintf('%19.4f%40.4f\n', xarg, fit);
end
end

```
```e01ba example results

j    knot lamda(j+2)   b-spline coeff c(j)

1                             1.0000
2         0.0000              1.1336
3         0.4000              1.3726
4         0.6000              1.7827
5         0.7500              2.1744
6         1.0000              2.4918
7                             2.7183

R        Abscissa            Ordinate             Spline

1         0.0000              1.0000              1.0000
0.1000                                  1.1052
2         0.2000              1.2214              1.2214
0.3000                                  1.3498
3         0.4000              1.4918              1.4918
0.5000                                  1.6487
4         0.6000              1.8221              1.8221
0.6750                                  1.9640
5         0.7500              2.1170              2.1170
0.8250                                  2.2819
6         0.9000              2.4596              2.4596
0.9500                                  2.5857
7         1.0000              2.7183              2.7183
```