hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_fit_1dspline_knots (e02ba)


    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example


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.


[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)


nag_fit_1dspline_knots (e02ba) determines a least squares cubic spline approximation sx to the set of data points xr,yr with weights wr, for r=1,2,,m. The value of ncap7=n-+7, where n- is the number of intervals of the spline (one greater than the number of interior knots), and the values of the knots λ5,λ6,,λn-+3, interior to the data interval, are prescribed by you.
sx has the property that it minimizes θ, the sum of squares of the weighted residuals εr, for r=1,2,,m, where
The function produces this minimizing value of θ and the coefficients c1,c2,,cq, where q=n-+3, in the B-spline representation
Here Nix denotes the normalized B-spline of degree 3 defined upon the knots λi,λi+1,,λi+4.
In order to define the full set of B-splines required, eight additional knots λ1,λ2,λ3,λ4 and λn-+4,λn-+5,λn-+6,λn-+7 are inserted automatically by the function. The first four of these are set equal to the smallest xr and the last four to the largest xr.
The representation of sx in terms of B-splines is the most compact form possible in that only n-+3 coefficients, in addition to the n-+7 knots, fully define sx.
The method employed involves forming and then computing the least squares solution of a set of m linear equations in the coefficients ci, for i=1,2,,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 Nix 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 sx from its B-spline representation may be carried out using nag_fit_1dspline_eval (e02bb). If derivatives of sx 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 sx.


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


Compulsory Input Parameters

1:     xm – double array
The values xr of the independent variable (abscissa), for r=1,2,,m.
Constraint: x1x2xm.
2:     ym – double array
The values yr of the dependent variable (ordinate), for r=1,2,,m.
3:     wm – double array
The values wr of the weights, for r=1,2,,m. For advice on the choice of weights, see the E02 Chapter Introduction.
Constraint: wr>0.0, for r=1,2,,m.
4:     lamdancap7 – double array
lamdai must be set to the i-4th (interior) knot, λi, for i=5,6,,n-+3.
Constraint: x1<lamda5lamda6lamdancap7-4<xm.

Optional Input Parameters

1:     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: mmdist4, where mdist is the number of distinct x values in the data.
2:     ncap7 int64int32nag_int scalar
Default: the dimension of the array lamda.
n-+7, where 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 x1 to xm) over which the spline is defined.
Constraint: 8ncap7mdist+4, where mdist is the number of distinct x values in the data.

Output Parameters

1:     lamdancap7 – double array
The input values are unchanged, and lamdai, for i=1,2,3,4, ncap7-3, ncap7-2, 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:     cncap7 – double array
The coefficient ci of the B-spline Nix, for i=1,2,,n-+3. The remaining elements of the array are not used.
3:     ss – double scalar
The residual sum of squares, θ.
4:     ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and Warnings

Errors or warnings detected by the function:
The knots fail to satisfy the condition
Thus the knots are not in correct order or are not interior to the data interval.
The weights are not all strictly positive.
The values of xr, for r=1,2,,m, are not in nondecreasing order.
ncap7<8 (so the number of interior knots is negative) or ncap7>mdist+4, where mdist is the number of distinct x values in the data (so there cannot be a unique solution).
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 ncap7-4 strictly increasing values, xR1, xR2, , xRncap7-4, among the abscissae such that
  • xR1<lamda1< xR5,
  • xR2<lamda2< xR6,
  • xRncap7-8<lamdancap7-8< xRncap7-4.
This means that there is no unique solution: there are regions containing too many knots compared with the number of data points.
An unexpected error has been triggered by this routine. Please contact NAG.
Your licence key may have expired or may not have been installed correctly.
Dynamic memory allocation failed.


The rounding errors committed are such that the computed coefficients are exact for a slightly perturbed set of ordinates yr+δyr. The ratio of the root-mean-square value for the δyr to the root-mean-square value of the yr can be expected to be less than a small multiple of κ×m×machine precision, where κ is a condition number for the problem. Values of κ for 2030 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 x1 and xm used in the function by various choices of non-coincident exterior knots gave values of κ between 16 and 180. Again see Cox (1975) for further details.) In general we would not expect κ 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.

Further Comments

The time taken is approximately C×2m+n-+7 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 λi<λi+4, for i=1,2,,n-+3, (see Error Indicators and Warnings). At a knot of multiplicity one (the usual case), sx and its first two derivatives are continuous. At a knot of multiplicity two, sx and its first derivative are continuous. At a knot of multiplicity three, sx is continuous, and at a knot of multiplicity four, sx is generally discontinuous.
The function can be used efficiently for cubic spline interpolation, i.e., if m=n-+3. The abscissae must then of course satisfy x1<x2<<xm. Recommended values for the knots in this case are λi=xi-2, for i=5,6,,n-+3.


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 n- intervals (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%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))

fig1 = figure;
hold on
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

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015