# NAG FL Interfacee02bcf (dim1_​spline_​deriv)

## 1Purpose

e02bcf evaluates a cubic spline and its first three derivatives from its B-spline representation.

## 2Specification

Fortran Interface
 Subroutine e02bcf ( c, x, left, s,
 Integer, Intent (In) :: ncap7, left Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: lamda(ncap7), c(ncap7), x Real (Kind=nag_wp), Intent (Out) :: s(4)
C Header Interface
#include <nag.h>
 void e02bcf_ (const Integer *ncap7, const double lamda[], const double c[], const double *x, const Integer *left, double s[], Integer *ifail)
The routine may be called by the names e02bcf or nagf_fit_dim1_spline_deriv.

## 3Description

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 B-spline coefficients ${c}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\overline{n}+3$ and (augmented) ordered knot set ${\lambda }_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\overline{n}+7$, (see e02baf), i.e.,
 $sx = ∑ i=1q ci Nix .$
Here $q=\overline{n}+3$, $\overline{n}$ is the number of intervals of the spline and ${N}_{i}\left(x\right)$ denotes the normalized B-spline of degree $3$ (order $4$) defined upon the knots ${\lambda }_{i},{\lambda }_{i+1},\dots ,{\lambda }_{i+4}$. The prescribed argument $x$ must satisfy
 $λ4 ≤ x ≤ λ n¯+4 .$
At a simple knot ${\lambda }_{i}$ (i.e., one satisfying ${\lambda }_{i-1}<{\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 right-hand derivative.
The method employed is based upon:
1. (i)carrying out a binary search for the knot interval containing the argument $x$ (see Cox (1978)),
2. (ii)evaluating the nonzero B-splines of orders $1$, $2$, $3$ and $4$ by recurrence (see Cox (1972) and Cox (1978)),
3. (iii)computing all derivatives of the B-splines of order $4$ by applying a second recurrence to these computed B-spline values (see de Boor (1972)),
4. (iv)multiplying the fourth-order B-spline values and their derivative by the appropriate B-spline 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.

## 4References

Cox M G (1972) The numerical evaluation of B-splines J. Inst. Math. Appl. 10 134–149
Cox M G (1978) The numerical evaluation of a spline from its B-spline representation J. Inst. Math. Appl. 21 135–143
de Boor C (1972) On calculating with B-splines J. Approx. Theory 6 50–62

## 5Arguments

1: $\mathbf{ncap7}$Integer Input
On entry: $\overline{n}+7$, where $\overline{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 }_{\overline{n}+4}$ over which the spline is defined).
Constraint: ${\mathbf{ncap7}}\ge 8$.
2: $\mathbf{lamda}\left({\mathbf{ncap7}}\right)$Real (Kind=nag_wp) array Input
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 ,\overline{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: $\mathbf{c}\left({\mathbf{ncap7}}\right)$Real (Kind=nag_wp) array Input
On entry: the coefficient ${c}_{\mathit{i}}$ of the B-spline ${N}_{\mathit{i}}\left(x\right)$, for $\mathit{i}=1,2,\dots ,\overline{n}+3$. The remaining elements of the array are not referenced.
4: $\mathbf{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: $\mathbf{left}$Integer Input
On entry: specifies whether left- or right-hand values of the spline and its derivatives are to be computed (see Section 3). Left- or right-hand 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)$, right-hand values are computed.
If $x={\mathbf{lamda}}\left({\mathbf{ncap7}}-3\right)$, left-hand values are formed, regardless of the value of left.
6: $\mathbf{s}\left(4\right)$Real (Kind=nag_wp) array Output
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: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, . If you are unfamiliar with this argument you should refer to Section 4 in the Introduction to the NAG Library FL Interface 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, ${\mathbf{ncap7}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ncap7}}\ge 8$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{lamda}}\left(4\right)=〈\mathit{\text{value}}〉$, ${\mathbf{ncap7}}=〈\mathit{\text{value}}〉$ and ${\mathbf{lamda}}\left({\mathbf{ncap7}}-3\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{lamda}}\left(4\right)<{\mathbf{lamda}}\left({\mathbf{ncap7}}-3\right)$.
On entry, ${\mathbf{x}}=〈\mathit{\text{value}}〉$, ${\mathbf{ncap7}}=〈\mathit{\text{value}}〉$ and ${\mathbf{lamda}}\left({\mathbf{ncap7}}-3\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{x}}\le {\mathbf{lamda}}\left({\mathbf{ncap7}}-3\right)$.
On entry, ${\mathbf{x}}=〈\mathit{\text{value}}〉$ and ${\mathbf{lamda}}\left(4\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{x}}\ge {\mathbf{lamda}}\left(4\right)$.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

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 , 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 . 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.

## 8Parallelism and Performance

e02bcf is not threaded in any implementation.

## 9Further Comments

The time taken is approximately linear in $\mathrm{log}\left(\overline{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 $\overline{n}+7$ instead of $\mathrm{log}\left(\overline{n}+7\right)$. All the conditions are tested in e02baf, however.

## 10Example

Compute, at the $7$ arguments $x=0$, $1$, $2$, $3$, $4$, $5$, $6$, the left- and right-hand 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$ B-spline 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:
 $\overline{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.

### 10.1Program Text

Program Text (e02bcfe.f90)

### 10.2Program Data

Program Data (e02bcfe.d)

### 10.3Program Results

Program Results (e02bcfe.r)