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_interp_1d_spline (e01ba)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

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 sx, defined in the range x1xxm, which interpolates (passes exactly through) the set of data points xi,yi, for i=1,2,,m, where m4 and x1<x2<<xm. Unlike some other spline interpolation algorithms, derivative end conditions are not imposed. The spline interpolant chosen has m-4 interior knots λ5,λ6,,λm, which are set to the values of x3,x4,,xm-2 respectively. This spline is represented in its B-spline form (see Cox (1975)):
sx=i=1mciNix,  
where Nix denotes the normalized B-spline of degree 3, defined upon the knots λi,λi+1,,λi+4, and ci denotes its coefficient, whose value is to be determined by the function.
The use of B-splines requires eight additional knots λ1, λ2, λ3, λ4, λm+1, λm+2, λm+3 and λm+4 to be specified; nag_interp_1d_spline (e01ba) sets the first four of these to x1 and the last four to xm.
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_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 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:     xm – double array
xi must be set to xi, the ith data value of the independent variable x, for i=1,2,,m.
Constraint: xi<xi+1, for i=1,2,,m-1.
2:     ym – double array
yi must be set to yi, the ith data value of the dependent variable y, for i=1,2,,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, the number of data points.
Constraint: m4.

Output Parameters

1:     lamdalck – double array
lck=m+4.
The value of λi, the ith knot, for i=1,2,,m+4.
2:     clck – double array
lck=m+4.
The coefficient ci of the B-spline Nix, for i=1,2,,m. The remaining elements of the array are not used.
3:     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:
   ifail=1
On entry,m<4,
orlck<m+4,
orlwrk<6×m+16.
   ifail=2
The x-values fail to satisfy the condition
x1<x2<x3<<xm.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

Accuracy

The rounding errors incurred are such that the computed spline is an exact interpolant for a slightly perturbed set of ordinates yi+δyi. The ratio of the root-mean-square value of the δyi to that of the yi is no greater than a small multiple of the relative machine precision.

Further Comments

The time taken by nag_interp_1d_spline (e01ba) is approximately proportional to m.
All the xi are used as knot positions except x2 and xm-1. This choice of knots (see Cox (1977)) means that sx is composed of m-3 cubic arcs as follows. If m=4, there is just a single arc space spanning the whole interval x1 to x4. If m5, the first and last arcs span the intervals x1 to x3 and xm-2 to xm respectively. Additionally if m6, the ith arc, for i=2,3,,m-4, spans the interval xi+1 to xi+2.
After the call
[lamda, c, ifail] = e01ba(x, y, lck);
the following operations may be carried out on the interpolant sx.
The value of sx at x=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 sx and its first three derivatives at x=x can be provided in the double array s of dimension 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 sx over the range x1 to xm can be provided in the double variable dint by
[dint, ifail] = e02bd(lamda, c);
(see nag_fit_1dspline_integ (e02bd)).

Example

This example sets up data from 7 values of the exponential function in the interval 0 to 1. nag_interp_1d_spline (e01ba) is then called to compute a spline interpolant to these data.
The spline is evaluated by nag_fit_1dspline_eval (e02bb), at the data points and at points halfway between each adjacent pair of data points, and the spline values and the values of ex are printed out.
function e01ba_example


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

x = [0     0.2     0.4     0.6     0.75     0.9     1];
y = exp(x);
[lamda, c, ifail] = e01ba(x, y);

fprintf('\n   j    knot lamda(j+2)   b-spline coeff c(j)\n\n');
j = 1;
fprintf('%4d%35.4f\n', j, c(1));
m = size(x,2);
for j = 2:m - 1;
  fprintf('%4d%15.4f%20.4f\n', j, lamda(j+2), c(j));
end
fprintf('%4d%35.4f\n', m, c(m));
fprintf('\n   R        Abscissa            Ordinate             Spline\n\n');
for r = 1:m;
  [fit, ifail] = e02bb( ...
                      lamda, c, x(r));

  fprintf('%4d%15.4f%20.4f%20.4f\n', r, x(r), y(r), fit);
  if r<m;
    xarg = (x(r)+x(r+1))/2;

    [fit, ifail] = e02bb( ...
                          lamda, c, xarg);
    fprintf('%19.4f%40.4f\n', xarg, fit);
  end
end


e01ba example results


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

   1                             1.0000
   2         0.0000              1.1336
   3         0.4000              1.3726
   4         0.6000              1.7827
   5         0.7500              2.1744
   6         1.0000              2.4918
   7                             2.7183

   R        Abscissa            Ordinate             Spline

   1         0.0000              1.0000              1.0000
             0.1000                                  1.1052
   2         0.2000              1.2214              1.2214
             0.3000                                  1.3498
   3         0.4000              1.4918              1.4918
             0.5000                                  1.6487
   4         0.6000              1.8221              1.8221
             0.6750                                  1.9640
   5         0.7500              2.1170              2.1170
             0.8250                                  2.2819
   6         0.9000              2.4596              2.4596
             0.9500                                  2.5857
   7         1.0000              2.7183              2.7183

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