# NAG Library Routine Document

## 1Purpose

e02baf computes a weighted least squares approximation to an arbitrary set of data points by a cubic spline with knots prescribed by you. Cubic spline interpolation can also be carried out.

## 2Specification

Fortran Interface
 Subroutine e02baf ( m, x, y, w, c, ss,
 Integer, Intent (In) :: m, ncap7 Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: x(m), y(m), w(m) Real (Kind=nag_wp), Intent (Inout) :: lamda(ncap7) Real (Kind=nag_wp), Intent (Out) :: work1(m), work2(4*ncap7), c(ncap7), ss
#include <nagmk26.h>
 void e02baf_ (const Integer *m, const Integer *ncap7, const double x[], const double y[], const double w[], double lamda[], double work1[], double work2[], double c[], double *ss, Integer *ifail)

## 3Description

e02baf determines a least squares cubic spline approximation $s\left(x\right)$ to the set of data points $\left({x}_{\mathit{r}},{y}_{\mathit{r}}\right)$ with weights ${w}_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,m$. The value of ${\mathbf{ncap7}}=\stackrel{-}{n}+7$, where $\stackrel{-}{n}$ is the number of intervals of the spline (one greater than the number of interior knots), and the values of the knots ${\lambda }_{5},{\lambda }_{6},\dots ,{\lambda }_{\stackrel{-}{n}+3}$, interior to the data interval, are prescribed by you.
$s\left(x\right)$ has the property that it minimizes $\theta$, the sum of squares of the weighted residuals ${\epsilon }_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,m$, where
 $εr=wryr-sxr.$
The routine produces this minimizing value of $\theta$ and the coefficients ${c}_{1},{c}_{2},\dots ,{c}_{q}$, where $q=\stackrel{-}{n}+3$, in the B-spline representation
 $sx=∑i=1qciNix.$
Here ${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}$.
In order to define the full set of B-splines required, eight additional knots ${\lambda }_{1},{\lambda }_{2},{\lambda }_{3},{\lambda }_{4}$ and ${\lambda }_{\stackrel{-}{n}+4},{\lambda }_{\stackrel{-}{n}+5},{\lambda }_{\stackrel{-}{n}+6},{\lambda }_{\stackrel{-}{n}+7}$ are inserted automatically by the routine. The first four of these are set equal to the smallest ${x}_{r}$ and the last four to the largest ${x}_{r}$.
The representation of $s\left(x\right)$ in terms of B-splines is the most compact form possible in that only $\stackrel{-}{n}+3$ coefficients, in addition to the $\stackrel{-}{n}+7$ knots, fully define $s\left(x\right)$.
The method employed involves forming and then computing the least squares solution of a set of $m$ linear equations in the coefficients ${c}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\stackrel{-}{n}+3$. The equations are formed using a recurrence relation for B-splines that is unconditionally stable (see Cox (1972) and de Boor (1972)), even for multiple (coincident) knots. The least squares solution is also obtained in a stable manner by using orthogonal transformations, viz. a variant of Givens rotations (see Gentleman (1974) and Gentleman (1973)). This requires only one equation to be stored at a time. Full advantage is taken of the structure of the equations, there being at most four nonzero values of ${N}_{i}\left(x\right)$ for any value of $x$ and hence at most four coefficients in each equation.
For further details of the algorithm and its use see Cox (1974), Cox (1975) and Cox and Hayes (1973).
Subsequent evaluation of $s\left(x\right)$ from its B-spline representation may be carried out using e02bbf. If derivatives of $s\left(x\right)$ are also required, e02bcf may be used. e02bdf can be used to compute the definite integral of $s\left(x\right)$.

## 4References

Cox M G (1972) The numerical evaluation of B-splines J. Inst. Math. Appl. 10 134–149
Cox M G (1974) A data-fitting package for the non-specialist user Software for Numerical Mathematics (ed D J Evans) Academic Press
Cox M G (1975) Numerical methods for the interpolation and approximation of data by spline functions PhD Thesis City University, London
Cox M G and Hayes J G (1973) Curve fitting: a guide and suite of algorithms for the non-specialist user NPL Report NAC26 National Physical Laboratory
de Boor C (1972) On calculating with B-splines J. Approx. Theory 6 50–62
Gentleman W M (1973) Least squares computations by Givens transformations without square roots J. Inst. Math. Applic. 12 329–336
Gentleman W M (1974) Algorithm AS 75. Basic procedures for large sparse or weighted linear least squares problems Appl. Statist. 23 448–454
Schoenberg I J and Whitney A (1953) On Polya frequency functions III Trans. Amer. Math. Soc. 74 246–259

## 5Arguments

1:     $\mathbf{m}$ – IntegerInput
On entry: the number $m$ of data points.
Constraint: ${\mathbf{m}}\ge \mathit{mdist}\ge 4$, where $\mathit{mdist}$ is the number of distinct $x$ values in the data.
2:     $\mathbf{ncap7}$ – IntegerInput
On entry: $\stackrel{-}{n}+7$, where $\stackrel{-}{n}$ is the number of intervals of the spline (which is one greater than the number of interior knots, i.e., the knots strictly within the range ${x}_{1}$ to ${x}_{m}$) over which the spline is defined.
Constraint: $8\le {\mathbf{ncap7}}\le \mathit{mdist}+4$, where $\mathit{mdist}$ is the number of distinct $x$ values in the data.
3:     $\mathbf{x}\left({\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the values ${x}_{\mathit{r}}$ of the independent variable (abscissa), for $\mathit{r}=1,2,\dots ,m$. The values must satisfy the Schoenberg–Whitney conditions (see Section 9).
Constraint: ${x}_{1}\le {x}_{2}\le \cdots \le {x}_{m}$.
4:     $\mathbf{y}\left({\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the values ${y}_{\mathit{r}}$ of the dependent variable (ordinate), for $\mathit{r}=1,2,\dots ,m$.
5:     $\mathbf{w}\left({\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the values ${w}_{\mathit{r}}$ of the weights, for $\mathit{r}=1,2,\dots ,m$. For advice on the choice of weights, see the E02 Chapter Introduction.
Constraint: ${\mathbf{w}}\left(\mathit{r}\right)>0.0$, for $\mathit{r}=1,2,\dots ,m$.
6:     $\mathbf{lamda}\left({\mathbf{ncap7}}\right)$ – Real (Kind=nag_wp) arrayInput/Output
On entry: ${\mathbf{lamda}}\left(\mathit{i}\right)$ must be set to the $\left(\mathit{i}-4\right)$th (interior) knot, ${\lambda }_{\mathit{i}}$, for $\mathit{i}=5,6,\dots ,\stackrel{-}{n}+3$.
Constraint: ${\mathbf{x}}\left(1\right)<{\mathbf{lamda}}\left(5\right)\le {\mathbf{lamda}}\left(6\right)\le \cdots \le {\mathbf{lamda}}\left({\mathbf{ncap7}}-4\right)<{\mathbf{x}}\left({\mathbf{m}}\right)$.
On exit: the input values are unchanged, and ${\mathbf{lamda}}\left(\mathit{i}\right)$, for $\mathit{i}=1,2,3,4$, ${\mathbf{ncap7}}-3$, ${\mathbf{ncap7}}-2$, ${\mathbf{ncap7}}-1$, ncap7 contains the additional (exterior) knots introduced by the routine. For advice on the choice of knots, see Section 3.3 in the E02 Chapter Introduction.
7:     $\mathbf{work1}\left({\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayWorkspace
8:     $\mathbf{work2}\left(4×{\mathbf{ncap7}}\right)$ – Real (Kind=nag_wp) arrayWorkspace
9:     $\mathbf{c}\left({\mathbf{ncap7}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: the coefficient ${c}_{\mathit{i}}$ of the B-spline ${N}_{\mathit{i}}\left(x\right)$, for $\mathit{i}=1,2,\dots ,\stackrel{-}{n}+3$. The remaining elements of the array are not used.
10:   $\mathbf{ss}$ – Real (Kind=nag_wp)Output
On exit: the residual sum of squares, $\theta$.
11:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, . 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  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  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, $\mathit{J}=〈\mathit{\text{value}}〉$, ${\mathbf{lamda}}\left(\mathit{J}\right)=〈\mathit{\text{value}}〉$ and ${\mathbf{lamda}}\left(\mathit{J}+1\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{lamda}}\left(\mathit{J}\right)\le {\mathbf{lamda}}\left(\mathit{J}+1\right)$.
On entry, ${\mathbf{lamda}}\left(5\right)=〈\mathit{\text{value}}〉$ and ${\mathbf{x}}\left(1\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{lamda}}\left(5\right)>{\mathbf{x}}\left(1\right)$.
On entry, ${\mathbf{ncap7}}=〈\mathit{\text{value}}〉$, ${\mathbf{lamda}}\left({\mathbf{ncap7}}-4\right)=〈\mathit{\text{value}}〉$, ${\mathbf{m}}=〈\mathit{\text{value}}〉$ and ${\mathbf{x}}\left({\mathbf{m}}\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{lamda}}\left({\mathbf{ncap7}}-4\right)<{\mathbf{x}}\left({\mathbf{m}}\right)$.
${\mathbf{ifail}}=2$
On entry, $\mathit{i}=〈\mathit{\text{value}}〉$ and ${\mathbf{w}}\left(\mathit{i}\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{w}}\left(\mathit{i}\right)>0.0$.
${\mathbf{ifail}}=3$
On entry, the x values are not in nondecreasing order. $\mathit{I}=〈\mathit{\text{value}}〉$, ${\mathbf{x}}\left(\mathit{I}\right)=〈\mathit{\text{value}}〉$, $\mathit{J}=〈\mathit{\text{value}}〉$ and $\mathit{xdist}\left(\mathit{J}\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{x}}\left(\mathit{I}\right)\ge \mathit{xdist}\left(\mathit{J}\right)$, where xdist is the set of distinct $x$-values.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{ncap7}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ncap7}}\ge 8$.
On entry, ${\mathbf{ncap7}}=〈\mathit{\text{value}}〉$ and ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ncap7}}\le {\mathbf{m}}+4$.
On entry, ${\mathbf{ncap7}}=〈\mathit{\text{value}}〉$ and $\mathit{mdist}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ncap7}}\le \mathit{mdist}+4$, where mdist is the number of distinct x-values.
${\mathbf{ifail}}=5$
On entry, the Schoenberg–Whitney conditions fail to hold for at least one subset of the distinct data abscissae. $\mathit{I}=〈\mathit{\text{value}}〉$, $\mathit{xdist}\left(\mathit{I}\right)=〈\mathit{\text{value}}〉$, $\mathit{J}=〈\mathit{\text{value}}〉$ and ${\mathbf{lamda}}\left(\mathit{J}\right)=〈\mathit{\text{value}}〉$.
Constraint: $\mathit{xdist}\left(\mathit{I}\right)<{\mathbf{lamda}}\left(\mathit{J}\right)$, where xdist is the set of distinct $x$-values.
On entry, the Schoenberg–Whitney conditions fail to hold for at least one subset of the distinct data abscissae. $\mathit{J}=〈\mathit{\text{value}}〉$, $\mathit{xdist}\left(\mathit{J}\right)=〈\mathit{\text{value}}〉$, $\mathit{J}+4=〈\mathit{\text{value}}〉$ and ${\mathbf{lamda}}\left(\mathit{J}+4\right)=〈\mathit{\text{value}}〉$.
Constraint: $\mathit{xdist}\left(\mathit{J}\right)<{\mathbf{lamda}}\left(\mathit{J}+4\right)$, where xdist is the set of distinct $x$-values.
On entry, the Schoenberg–Whitney conditions fail to hold for at least one subset of the distinct data abscissae. $L=〈\mathit{\text{value}}〉$, $\mathit{xdist}\left(L\right)=〈\mathit{\text{value}}〉$, $\mathit{I}=〈\mathit{\text{value}}〉$ and ${\mathbf{lamda}}\left(\mathit{I}\right)=〈\mathit{\text{value}}〉$.
Constraint: $\mathit{xdist}\left(\mathit{L}\right)>{\mathbf{lamda}}\left(\mathit{I}\right)$, where xdist is the set of distinct $x$-values.
${\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

The rounding errors committed are such that the computed coefficients are exact for a slightly perturbed set of ordinates ${y}_{r}+\delta {y}_{r}$. The ratio of the root-mean-square value for the $\delta {y}_{r}$ to the root-mean-square value of the ${y}_{r}$ can be expected to be less than a small multiple of , where $\kappa$ is a condition number for the problem. Values of $\kappa$ for $20$$30$ practical datasets all proved to lie between $4.5$ and $7.8$ (see Cox (1975)). (Note that for these datasets, replacing the coincident end knots at the end points ${x}_{1}$ and ${x}_{m}$ used in the routine by various choices of non-coincident exterior knots gave values of $\kappa$ between $16$ and $180$. Again see Cox (1975) for further details.) In general we would not expect $\kappa$ to be large unless the choice of knots results in near-violation of the Schoenberg–Whitney conditions.
A cubic spline which adequately fits the data and is free from spurious oscillations is more likely to be obtained if the knots are chosen to be grouped more closely in regions where the function (underlying the data) or its derivatives change more rapidly than elsewhere.

## 8Parallelism and Performance

e02baf is not threaded in any implementation.

The time taken is approximately $C×\left(2m+\stackrel{-}{n}+7\right)$ seconds, where $C$ is a machine-dependent constant.
Multiple knots are permitted as long as their multiplicity does not exceed $4$, i.e., the complete set of knots must satisfy ${\lambda }_{\mathit{i}}<{\lambda }_{\mathit{i}+4}$, for $\mathit{i}=1,2,\dots ,\stackrel{-}{n}+3$, (see Section 6). At a knot of multiplicity one (the usual case), $s\left(x\right)$ and its first two derivatives are continuous. At a knot of multiplicity two, $s\left(x\right)$ and its first derivative are continuous. At a knot of multiplicity three, $s\left(x\right)$ is continuous, and at a knot of multiplicity four, $s\left(x\right)$ is generally discontinuous.
The routine can be used efficiently for cubic spline interpolation, i.e., if $m=\stackrel{-}{n}+3$. The abscissae must then of course satisfy ${x}_{1}<{x}_{2}<\cdots <{x}_{m}$. Recommended values for the knots in this case are ${\lambda }_{\mathit{i}}={x}_{\mathit{i}-2}$, for $\mathit{i}=5,6,\dots ,\stackrel{-}{n}+3$.
The Schoenberg–Whitney conditions (see Schoenberg and Whitney (1953)) state that there must be a subset of ${\mathbf{ncap7}}-4$ strictly increasing values, ${\mathbf{x}}\left(R\left(1\right)\right),{\mathbf{x}}\left(R\left(2\right)\right),\dots ,{\mathbf{x}}\left(R\left({\mathbf{ncap7}}-4\right)\right)$, among the abscissae such that
• ${\mathbf{x}}\left(R\left(1\right)\right)<{\mathbf{lamda}}\left(1\right)<{\mathbf{x}}\left(R\left(5\right)\right)$,
• ${\mathbf{x}}\left(R\left(2\right)\right)<{\mathbf{lamda}}\left(2\right)<{\mathbf{x}}\left(R\left(6\right)\right)$,
• $⋮$
• ${\mathbf{x}}\left(R\left({\mathbf{ncap7}}-8\right)\right)<{\mathbf{lamda}}\left({\mathbf{ncap7}}-8\right)<{\mathbf{x}}\left(R\left({\mathbf{ncap7}}-4\right)\right)$.
If this condition is not satisfied, then there is no unique solution: there are regions containing too many knots compared with the number of data points.

## 10Example

Determine a weighted least squares cubic spline approximation with five intervals (four interior knots) to a set of $14$ given data points. Tabulate the data and the corresponding values of the approximating spline, together with the residual errors, and also the values of the approximating spline at points half-way between each pair of adjacent data points.
The example program is written in a general form that will enable a cubic spline approximation with $\stackrel{-}{n}$ intervals ($\stackrel{-}{n}-1$ interior knots) to be obtained to $m$ data points, with arbitrary positive weights, and the approximation to be tabulated. Note that e02bbf is used to evaluate the approximating spline. The program is self-starting in that any number of datasets can be supplied.

### 10.1Program Text

Program Text (e02bafe.f90)

### 10.2Program Data

Program Data (e02bafe.d)

### 10.3Program Results

Program Results (e02bafe.r)