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(x)$s\left(x\right)$, defined in the range x1xxm${x}_{1}\le x\le {x}_{m}$, which interpolates (passes exactly through) the set of data points (xi,yi)$\left({x}_{\mathit{i}},{y}_{\mathit{i}}\right)$, for i = 1,2,,m$\mathit{i}=1,2,\dots ,m$, where m4$m\ge 4$ and x1 < x2 < < xm${x}_{1}<{x}_{2}<\cdots <{x}_{m}$. Unlike some other spline interpolation algorithms, derivative end conditions are not imposed. The spline interpolant chosen has m4$m-4$ interior knots λ5,λ6,,λm${\lambda }_{5},{\lambda }_{6},\dots ,{\lambda }_{m}$, which are set to the values of x3,x4,,xm2${x}_{3},{x}_{4},\dots ,{x}_{m-2}$ respectively. This spline is represented in its B-spline form (see Cox (1975)):
 m s(x) = ∑ ciNi(x), i = 1
$s(x)=∑i=1mciNi(x),$
where Ni(x)${N}_{i}\left(x\right)$ denotes the normalized B-spline of degree 3$3$, defined upon the knots λi,λi + 1,,λi + 4${\lambda }_{i},{\lambda }_{i+1},\dots ,{\lambda }_{i+4}$, and ci${c}_{i}$ denotes its coefficient, whose value is to be determined by the function.
The use of B-splines requires eight additional knots λ1${\lambda }_{1}$, λ2${\lambda }_{2}$, λ3${\lambda }_{3}$, λ4${\lambda }_{4}$, λm + 1${\lambda }_{m+1}$, λm + 2${\lambda }_{m+2}$, λm + 3${\lambda }_{m+3}$ and λm + 4${\lambda }_{m+4}$ to be specified; nag_interp_1d_spline (e01ba) sets the first four of these to x1${x}_{1}$ and the last four to xm${x}_{m}$.
The algorithm for determining the coefficients is as described in Cox (1975) except that QR$QR$ factorization is used instead of LU$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 Section [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:     x(m) – double array
m, the dimension of the array, must satisfy the constraint m4${\mathbf{m}}\ge 4$.
x(i)${\mathbf{x}}\left(\mathit{i}\right)$ must be set to xi${x}_{\mathit{i}}$, the i$\mathit{i}$th data value of the independent variable x$x$, for i = 1,2,,m$\mathit{i}=1,2,\dots ,m$.
Constraint: x(i) < x(i + 1)${\mathbf{x}}\left(\mathit{i}\right)<{\mathbf{x}}\left(\mathit{i}+1\right)$, for i = 1,2,,m1$\mathit{i}=1,2,\dots ,{\mathbf{m}}-1$.
2:     y(m) – double array
m, the dimension of the array, must satisfy the constraint m4${\mathbf{m}}\ge 4$.
y(i)${\mathbf{y}}\left(\mathit{i}\right)$ must be set to yi${y}_{\mathit{i}}$, the i$\mathit{i}$th data value of the dependent variable y$y$, for i = 1,2,,m$\mathit{i}=1,2,\dots ,m$.

### Optional Input Parameters

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

lck wrk lwrk

### Output Parameters

1:     lamda(lck) – double array
lckm + 4$\mathit{lck}\ge {\mathbf{m}}+4$.
The value of λi${\lambda }_{\mathit{i}}$, the i$\mathit{i}$th knot, for i = 1,2,,m + 4$\mathit{i}=1,2,\dots ,m+4$.
2:     c(lck) – double array
lckm + 4$\mathit{lck}\ge {\mathbf{m}}+4$.
The coefficient ci${c}_{\mathit{i}}$ of the B-spline Ni(x)${N}_{\mathit{i}}\left(x\right)$, for i = 1,2,,m$\mathit{i}=1,2,\dots ,m$. The remaining elements of the array are not used.
3:     ifail – int64int32nag_int scalar
${\mathrm{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:
ifail = 1${\mathbf{ifail}}=1$
 On entry, m < 4${\mathbf{m}}<4$, or lck < m + 4$\mathit{lck}<{\mathbf{m}}+4$, or lwrk < 6 × m + 16$\mathit{lwrk}<6×{\mathbf{m}}+16$.
ifail = 2${\mathbf{ifail}}=2$
The x-values fail to satisfy the condition
x(1) < x(2) < x(3) < < x(m)${\mathbf{x}}\left(1\right)<{\mathbf{x}}\left(2\right)<{\mathbf{x}}\left(3\right)<\cdots <{\mathbf{x}}\left({\mathbf{m}}\right)$.

## Accuracy

The rounding errors incurred are such that the computed spline is an exact interpolant for a slightly perturbed set of ordinates yi + δyi${y}_{i}+\delta {y}_{i}$. The ratio of the root-mean-square value of the δyi$\delta {y}_{i}$ to that of the yi${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$m$.
All the xi${x}_{i}$ are used as knot positions except x2${x}_{2}$ and xm1${x}_{m-1}$. This choice of knots (see Cox (1977)) means that s(x)$s\left(x\right)$ is composed of m3$m-3$ cubic arcs as follows. If m = 4$m=4$, there is just a single arc space spanning the whole interval x1${x}_{1}$ to x4${x}_{4}$. If m5$m\ge 5$, the first and last arcs span the intervals x1${x}_{1}$ to x3${x}_{3}$ and xm2${x}_{m-2}$ to xm${x}_{m}$ respectively. Additionally if m6$m\ge 6$, the i$\mathit{i}$th arc, for i = 2,3,,m4$\mathit{i}=2,3,\dots ,m-4$, spans the interval xi + 1${x}_{\mathit{i}+1}$ to xi + 2${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(x)$s\left(x\right)$.
The value of s(x)$s\left(x\right)$ at x = x$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(x)$s\left(x\right)$ and its first three derivatives at x = x$x={\mathbf{x}}$ can be provided in the double array s of dimension 4$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(x)$s\left(x\right)$ over the range x1${x}_{1}$ to xm${x}_{m}$ can be provided in the double variable dint by
```[dint, ifail] = e02bd(lamda, c);
```
(see nag_fit_1dspline_integ (e02bd)).

## Example

```function nag_interp_1d_spline_example
x = [0;
0.2;
0.4;
0.6;
0.75;
0.9;
1];
y = [1;
1.22140275816017;
1.49182469764127;
1.822118800390509;
2.117000016612675;
2.45960311115695;
2.718281828459045];
[lamda, c, ifail] = nag_interp_1d_spline(x, y)
```
```

lamda =

0
0
0
0
0.4000
0.6000
0.7500
1.0000
1.0000
1.0000
1.0000

c =

1.0000
1.1336
1.3726
1.7827
2.1744
2.4918
2.7183
0
0
0
0

ifail =

0

```
```function e01ba_example
x = [0;
0.2;
0.4;
0.6;
0.75;
0.9;
1];
y = [1;
1.22140275816017;
1.49182469764127;
1.822118800390509;
2.117000016612675;
2.45960311115695;
2.718281828459045];
[lamda, c, ifail] = e01ba(x, y)
```
```

lamda =

0
0
0
0
0.4000
0.6000
0.7500
1.0000
1.0000
1.0000
1.0000

c =

1.0000
1.1336
1.3726
1.7827
2.1744
2.4918
2.7183
0
0
0
0

ifail =

0

```