e01 Chapter Contents
e01 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_1d_spline_interpolant (e01bac)

## 1  Purpose

nag_1d_spline_interpolant (e01bac) determines a cubic spline interpolant to a given set of data.

## 2  Specification

 #include #include
 void nag_1d_spline_interpolant (Integer m, const double x[], const double y[], Nag_Spline *spline, NagError *fail)

## 3  Description

nag_1d_spline_interpolant (e01bac) 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)):
 $s x = ∑ i=1 m c i N i x$
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; the function 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_1d_spline_fit_knots (e02bac) followed by a call of that function. (For further details of nag_1d_spline_fit_knots (e02bac), see the function document.)
Values of the spline interpolant, or of its derivatives or definite integral, can subsequently be computed as detailed in Section 8.

## 4  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

## 5  Arguments

1:     mIntegerInput
On entry: $m$, the number of data points.
Constraint: ${\mathbf{m}}\ge 4$.
2:     x[m]const doubleInput
On entry: ${\mathbf{x}}\left[\mathit{i}-1\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}=0,1,\dots ,m-2$.
3:     y[m]const doubleInput
On entry: ${\mathbf{y}}\left[\mathit{i}-1\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$.
4:     splineNag_Spline *
Pointer to structure of type Nag_Spline with the following members:
nInteger *Output
On exit: the size of the storage internally allocated to $\mathbf{lamda}$ and $\mathbf{c}$. This is set to ${\mathbf{m}}+4$.
On exit: the pointer to which storage of size $\mathbf{n}$ is internally allocated. $\mathbf{lamda}\left[\mathit{i}-1\right]$ contains the $\mathit{i}$th knot, for $\mathit{i}=1,2,\dots ,m+4$.
cdouble *Output
On exit: the pointer to which storage of size $\mathbf{n}-4$ is internally allocated. $\mathbf{c}\left[\mathit{i}-1\right]$ contains the coefficient ${c}_{\mathit{i}}$ of the B-spline ${N}_{\mathit{i}}\left(x\right)$, for $\mathit{i}=1,2,\dots ,m$.
Note that when the information contained in the pointers $\mathbf{lamda}$ and $\mathbf{c}$ is no longer of use, or before a new call to nag_1d_spline_interpolant (e01bac) with the same spline, you should free this storage using the NAG macro NAG_FREE. This storage will not have been allocated if this function returns with ${\mathbf{fail}}\mathbf{.}\mathbf{code}\ne$ NE_NOERROR.
5:     failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
NE_INT_ARG_LT
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m}}\ge 4$.
NE_NOT_STRICTLY_INCREASING
The sequence x is not strictly increasing: ${\mathbf{x}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$, ${\mathbf{x}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$.

## 7  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_1d_spline_interpolant (e01bac) 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}_{i+1}$ to ${x}_{i+2}$.
After the call
e01bac(m, x, y, &spline, &fail)
the following operations may be carried out on the interpolant $s\left(x\right)$.
The value of $s\left(x\right)$ at $x=\mathbf{xval}$ can be provided in the variable sval by calling the function
e02bbc(xval, &sval, &spline, &fail)
The values of $s\left(x\right)$ and its first three derivatives at $x=\mathbf{xval}$ can be provided in the array sdif of dimension 4, by the call
e02bcc(derivs, xval, sdif, &spline, &fail)
Here derivs must specify whether the left- or right-hand value of the third derivative is required (see nag_1d_spline_deriv (e02bcc) 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 variable sint by
e02bdc(&spline, &sint, &fail)

## 9  Example

The following example program sets up data from 7 values of the exponential function in the interval 0 to 1. nag_1d_spline_interpolant (e01bac) is then called to compute a spline interpolant to these data.
The spline is evaluated by nag_1d_spline_evaluate (e02bbc), 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.

### 9.1  Program Text

Program Text (e01bace.c)

None.

### 9.3  Program Results

Program Results (e01bace.r)