NAG Library Routine Document
E02BCF
1 Purpose
E02BCF evaluates a cubic spline and its first three derivatives from its Bspline representation.
2 Specification
INTEGER 
NCAP7, LEFT, IFAIL 
REAL (KIND=nag_wp) 
LAMDA(NCAP7), C(NCAP7), X, S(4) 

3 Description
E02BCF evaluates the cubic spline
$s\left(x\right)$ and its first three derivatives at a prescribed argument
$x$. It is assumed that
$s\left(x\right)$ is represented in terms of its Bspline coefficients
${c}_{\mathit{i}}$, for
$\mathit{i}=1,2,\dots ,\stackrel{}{n}+3$ and (augmented) ordered knot set
${\lambda}_{\mathit{i}}$, for
$\mathit{i}=1,2,\dots ,\stackrel{}{n}+7$,
(see
E02BAF),
i.e.,
Here
$q=\stackrel{}{n}+3$,
$\stackrel{}{n}$ is the number of intervals of the spline and
${N}_{i}\left(x\right)$ denotes the normalized Bspline of degree
$3$ (order
$4$) defined upon the knots
${\lambda}_{i},{\lambda}_{i+1},\dots ,{\lambda}_{i+4}$. The prescribed argument
$x$ must satisfy
At a simple knot ${\lambda}_{i}$ (i.e., one satisfying ${\lambda}_{i1}<{\lambda}_{i}<{\lambda}_{i+1}$), the third derivative of the spline is in general discontinuous. At a multiple knot (i.e., two or more knots with the same value), lower derivatives, and even the spline itself, may be discontinuous. Specifically, at a point $x=u$ where (exactly) $r$ knots coincide (such a point is termed a knot of multiplicity $r$), the values of the derivatives of order $4\mathit{j}$, for $\mathit{j}=1,2,\dots ,r$, are in general discontinuous. (Here $1\le r\le 4$; $r>4$ is not meaningful.) You must specify whether the value at such a point is required to be the left or righthand derivative.
The method employed is based upon:
(i) 
carrying out a binary search for the knot interval containing the argument $x$ (see Cox (1978)), 
(ii) 
evaluating the nonzero Bsplines of orders $1$, $2$, $3$ and $4$ by recurrence (see Cox (1972) and Cox (1978)), 
(iii) 
computing all derivatives of the Bsplines of order $4$ by applying a second recurrence to these computed Bspline values (see de Boor (1972)), 
(iv) 
multiplying the fourthorder Bspline values and their derivative by the appropriate Bspline coefficients, and summing, to yield the values of $s\left(x\right)$ and its derivatives. 
E02BCF can be used to compute the values and derivatives of cubic spline fits and interpolants produced by
E02BAF.
If only values and not derivatives are required,
E02BBF may be used instead of E02BCF, which takes about
$50\%$ longer than
E02BBF.
4 References
Cox M G (1972) The numerical evaluation of Bsplines J. Inst. Math. Appl. 10 134–149
Cox M G (1978) The numerical evaluation of a spline from its Bspline representation J. Inst. Math. Appl. 21 135–143
de Boor C (1972) On calculating with Bsplines J. Approx. Theory 6 50–62
5 Parameters
 1: 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 ${\lambda}_{4}$ to ${\lambda}_{\stackrel{}{n}+4}$ over which the spline is defined).
Constraint:
${\mathbf{NCAP7}}\ge 8$.
 2: LAMDA(NCAP7) – REAL (KIND=nag_wp) arrayInput
On entry: ${\mathbf{LAMDA}}\left(\mathit{j}\right)$ must be set to the value of the $\mathit{j}$th member of the complete set of knots, ${\lambda}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,\stackrel{}{n}+7$.
Constraint:
the ${\mathbf{LAMDA}}\left(j\right)$ must be in nondecreasing order with ${\mathbf{LAMDA}}\left({\mathbf{NCAP7}}3\right)>{\mathbf{LAMDA}}\left(4\right)$.
 3: C(NCAP7) – REAL (KIND=nag_wp) arrayInput
On entry: the coefficient
${c}_{\mathit{i}}$ of the Bspline ${N}_{\mathit{i}}\left(x\right)$, for $\mathit{i}=1,2,\dots ,\stackrel{}{n}+3$. The remaining elements of the array are not referenced.
 4: X – REAL (KIND=nag_wp)Input
On entry: the argument $x$ at which the cubic spline and its derivatives are to be evaluated.
Constraint:
${\mathbf{LAMDA}}\left(4\right)\le {\mathbf{X}}\le {\mathbf{LAMDA}}\left({\mathbf{NCAP7}}3\right)$.
 5: LEFT – INTEGERInput
On entry: specifies whether left or righthand values of the spline and its derivatives are to be computed (see
Section 3). Left or righthand values are formed according to whether
LEFT is equal or not equal to
$1$.
If
$x$ does not coincide with a knot, the value of
LEFT is immaterial.
If $x={\mathbf{LAMDA}}\left(4\right)$, righthand values are computed.
If
$x={\mathbf{LAMDA}}\left({\mathbf{NCAP7}}3\right)$, lefthand values are formed, regardless of the value of
LEFT.
 6: S($4$) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{S}}\left(\mathit{j}\right)$ contains the value of the $\left(\mathit{j}1\right)$th derivative of the spline at the argument $x$, for $\mathit{j}=1,2,3,4$. Note that ${\mathbf{S}}\left(1\right)$ contains the value of the spline.
 7: IFAIL – INTEGERInput/Output

On entry:
IFAIL must be set to
$0$,
$1\text{ or}1$. If you are unfamiliar with this parameter you should refer to
Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value
$1\text{ or}1$ is recommended. If the output of error messages is undesirable, then the value
$1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is
$0$.
When the value $\mathbf{1}\text{ or}\mathbf{1}$ 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).
6 Error Indicators and Warnings
If on entry
${\mathbf{IFAIL}}={\mathbf{0}}$ or
${{\mathbf{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$

${\mathbf{NCAP7}}<8$, i.e., the number of intervals is not positive.
 ${\mathbf{IFAIL}}=2$
Either
${\mathbf{LAMDA}}\left(4\right)\ge {\mathbf{LAMDA}}\left({\mathbf{NCAP7}}3\right)$, i.e., the range over which
$s\left(x\right)$ is defined is null or negative in length, or
X is an invalid argument, i.e.,
${\mathbf{X}}<{\mathbf{LAMDA}}\left(4\right)$ or
${\mathbf{X}}>{\mathbf{LAMDA}}\left({\mathbf{NCAP7}}3\right)$.
7 Accuracy
The computed value of
$s\left(x\right)$ has negligible error in most practical situations. Specifically, this value has an
absolute error bounded in modulus by
$18\times {c}_{\mathrm{max}}\times \mathit{machineprecision}$, where
${c}_{\mathrm{max}}$ is the largest in modulus of
${c}_{j},{c}_{j+1},{c}_{j+2}$ and
${c}_{j+3}$, and
$j$ is an integer such that
${\lambda}_{j+3}\le x\le {\lambda}_{j+4}$. If
${c}_{j},{c}_{j+1},{c}_{j+2}$ and
${c}_{j+3}$ are all of the same sign, then the computed value of
$s\left(x\right)$ has
relative error bounded by
$20\times \mathit{machineprecision}$. For full details see
Cox (1978).
No complete error analysis is available for the computation of the derivatives of $s\left(x\right)$. However, for most practical purposes the absolute errors in the computed derivatives should be small.
The time taken is approximately linear in $\mathrm{log}\left(\stackrel{}{n}+7\right)$.
Note: the routine does not test all the conditions on the knots given in the description of
LAMDA in
Section 5, since to do this would result in a computation time approximately linear in
$\stackrel{}{n}+7$ instead of
$\mathrm{log}\left(\stackrel{}{n}+7\right)$. All the conditions are tested in
E02BAF, however.
9 Example
Compute, at the $7$ arguments
$x=0$,
$1$,
$2$,
$3$,
$4$,
$5$,
$6$,
the left and righthand values and first $3$ derivatives of the cubic spline defined over the interval $0\le x\le 6$ having the $6$ interior knots
$x=1$,
$3$,
$3$,
$3$,
$4$,
$4$, the $8$ additional knots
$0$,
$0$,
$0$,
$0$,
$6$,
$6$,
$6$,
$6$, and the $10$ Bspline coefficients
$10$,
$12$,
$13$,
$15$,
$22$,
$26$,
$24$,
$18$,
$14$,
$12$.
The input data items (using the notation of
Section 5) comprise the following values in the order indicated:
$\stackrel{}{n}$ 
$m$ 
${\mathbf{LAMDA}}\left(j\right)$, 
for $j=1,2,\dots ,{\mathbf{NCAP7}}$ 
${\mathbf{C}}\left(j\right)$, 
for $j=1,2,\dots ,{\mathbf{NCAP7}}4$ 
${\mathbf{X}}\left(i\right)$, 
for $i=1,2,\dots ,m$ 
This example program is written in a general form that will enable the values and derivatives of a cubic spline having an arbitrary number of knots to be evaluated at a set of arbitrary points. Any number of datasets may be supplied.
The only changes required to the program relate to the dimensions of the arrays
LAMDA and
C.
9.1 Program Text
Program Text (e02bcfe.f90)
9.2 Program Data
Program Data (e02bcfe.d)
9.3 Program Results
Program Results (e02bcfe.r)