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_1dspline_knots (e02ba)

## Purpose

nag_fit_1dspline_knots (e02ba) 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.

## Syntax

[lamda, c, ss, ifail] = e02ba(x, y, w, lamda, 'm', m, 'ncap7', ncap7)
[lamda, c, ss, ifail] = nag_fit_1dspline_knots(x, y, w, lamda, 'm', m, 'ncap7', ncap7)

## Description

nag_fit_1dspline_knots (e02ba) 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 function 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 function. 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 nag_fit_1dspline_eval (e02bb). If derivatives of $s\left(x\right)$ are also required, nag_fit_1dspline_deriv (e02bc) may be used. nag_fit_1dspline_integ (e02bd) can be used to compute the definite integral of $s\left(x\right)$.

## References

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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{x}\left({\mathbf{m}}\right)$ – double array
The values ${x}_{\mathit{r}}$ of the independent variable (abscissa), for $\mathit{r}=1,2,\dots ,m$.
Constraint: ${x}_{1}\le {x}_{2}\le \cdots \le {x}_{m}$.
2:     $\mathrm{y}\left({\mathbf{m}}\right)$ – double array
The values ${y}_{\mathit{r}}$ of the dependent variable (ordinate), for $\mathit{r}=1,2,\dots ,m$.
3:     $\mathrm{w}\left({\mathbf{m}}\right)$ – double array
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$.
4:     $\mathrm{lamda}\left({\mathbf{ncap7}}\right)$ – double array
${\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)$.

### Optional Input Parameters

1:     $\mathrm{m}$int64int32nag_int scalar
Default: the dimension of the arrays x, y, w. (An error is raised if these dimensions are not equal.)
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:     $\mathrm{ncap7}$int64int32nag_int scalar
Default: the dimension of the array lamda.
$\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.

### Output Parameters

1:     $\mathrm{lamda}\left({\mathbf{ncap7}}\right)$ – double array
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 function. For advice on the choice of knots, see Cubic Spline Curves in the E02 Chapter Introduction.
2:     $\mathrm{c}\left({\mathbf{ncap7}}\right)$ – double array
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.
3:     $\mathrm{ss}$ – double scalar
The residual sum of squares, $\theta$.
4:     $\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$
The knots fail to satisfy the condition
${\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)$.
Thus the knots are not in correct order or are not interior to the data interval.
${\mathbf{ifail}}=2$
The weights are not all strictly positive.
${\mathbf{ifail}}=3$
The values of ${\mathbf{x}}\left(\mathit{r}\right)$, for $\mathit{r}=1,2,\dots ,{\mathbf{m}}$, are not in nondecreasing order.
${\mathbf{ifail}}=4$
${\mathbf{ncap7}}<8$ (so the number of interior knots is negative) or ${\mathbf{ncap7}}>\mathit{mdist}+4$, where $\mathit{mdist}$ is the number of distinct $x$ values in the data (so there cannot be a unique solution).
${\mathbf{ifail}}=5$
The conditions specified by Schoenberg and Whitney (1953) fail to hold for at least one subset of the distinct data abscissae. That is, there is no 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)$.
This means that there is no unique solution: there are regions containing too many knots compared with the number of data points.
${\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 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 function 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.

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 Error Indicators and Warnings). 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 function 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$.

## Example

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 nag_fit_1dspline_eval (e02bb) is used to evaluate the approximating spline. The program is self-starting in that any number of datasets can be supplied.
```function e02ba_example

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

m = 14;
data = [  0.20  0.00  0.2;
0.47  2.00  0.2;
0.74  4.00  0.3;
1.09  6.00  0.7;
1.60  8.00  0.9;
1.90  8.62  1.0;
2.60  9.10  1.0;
3.10  8.90  1.0;
4.00  8.15  0.8;
5.15  7.00  0.5;
6.17  6.00  0.7;
8.00  4.54  1.0;
10.00  3.39  1.0;
12.00  2.56  1.0];
x = data(:,1);
y = data(:,2);
w = data(:,3);

knots = [1.5  2.6   4   8];
nbar  = size(knots,2) + 1;
ncap7 = nbar + 7;
lamda = zeros(ncap7,1);
lamda(5:nbar+3) = knots;

[lamda, c, ss, ifail] = e02ba( ...
x, y, w, lamda);

fprintf('\n  j       lamda(j+2)     b-spline coeff c(j)\n\n');
fprintf('%3d%35.4f\n', 1, c(1));
for j = 2:nbar+2
fprintf('%3d%15.4f%20.4f\n',j,lamda(j+2),c(j));
end
fprintf('%3d%35.4f\n', nbar+3, c(nbar+3));
fprintf('\nResidual sum of squares = %12.2e\n\n', ss)
fprintf('Cubic spline approximation and residuals\n\n');
fprintf('       x          w          y         Fit     Residual\n');

k = 0;
for i = 1:m
% data point evaluation
k = k + 1;
xp(k) = x(i);
[fit(k), ifail] = e02bb( ...
lamda, c, x(i));
fprintf('%11.4f%11.4f%11.4f%11.4f%11.2e\n', ...
x(i), w(i), y(i), fit(k), fit(k)-y(i));
% mid point evaluation
if i<m
xh = (x(i)+x(i+1))/2;
k = k + 1;
xp(k) = xh;
[fit(k), ifail] = e02bb( ...
lamda, c, xh);
fprintf('%11.4f%33.4f\n',xh, fit(k))
end
end

fig1 = figure;
hold on
plot(x,y,'*')
plot(xp,fit);
xlabel('x');
title({'Weighted least-squares cubic spline approximation', ...
'to a set of 14 data points'});
legend('data points','cubic spline fit');
hold off;

```
```e02ba example results

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

1                            -0.0465
2         0.2000              3.6150
3         1.5000              8.5724
4         2.6000              9.4261
5         4.0000              7.2716
6         8.0000              4.1207
7        12.0000              3.0822
8                             2.5597

Residual sum of squares =     1.78e-03

Cubic spline approximation and residuals

x          w          y         Fit     Residual
0.2000     0.2000     0.0000    -0.0465  -4.65e-02
0.3350                           1.0622
0.4700     0.2000     2.0000     2.1057   1.06e-01
0.6050                           3.0817
0.7400     0.3000     4.0000     3.9880  -1.20e-02
0.9150                           5.0558
1.0900     0.7000     6.0000     5.9983  -1.73e-03
1.3450                           7.1376
1.6000     0.9000     8.0000     7.9872  -1.28e-02
1.7500                           8.3544
1.9000     1.0000     8.6200     8.6348   1.48e-02
2.2500                           9.0076
2.6000     1.0000     9.1000     9.0896  -1.04e-02
2.8500                           9.0353
3.1000     1.0000     8.9000     8.9125   1.25e-02
3.5500                           8.5660
4.0000     0.8000     8.1500     8.1321  -1.79e-02
4.5750                           7.5592
5.1500     0.5000     7.0000     6.9925  -7.53e-03
5.6600                           6.5010
6.1700     0.7000     6.0000     6.0255   2.55e-02
7.0850                           5.2292
8.0000     1.0000     4.5400     4.5315  -8.51e-03
9.0000                           3.9045
10.0000     1.0000     3.3900     3.3928   2.76e-03
11.0000                           2.9574
12.0000     1.0000     2.5600     2.5597  -3.45e-04
```