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_deriv (e02bc)

## Purpose

nag_fit_1dspline_deriv (e02bc) evaluates a cubic spline and its first three derivatives from its B-spline representation.

## Syntax

[s, ifail] = e02bc(lamda, c, x, left, 'ncap7', ncap7)
[s, ifail] = nag_fit_1dspline_deriv(lamda, c, x, left, 'ncap7', ncap7)

## Description

nag_fit_1dspline_deriv (e02bc) evaluates the cubic spline s(x)$s\left(x\right)$ and its first three derivatives at a prescribed argument x$x$. It is assumed that s(x)$s\left(x\right)$ is represented in terms of its B-spline coefficients ci${c}_{\mathit{i}}$, for i = 1,2,,n + 3$\mathit{i}=1,2,\dots ,\stackrel{-}{n}+3$ and (augmented) ordered knot set λi${\lambda }_{\mathit{i}}$, for i = 1,2,,n + 7$\mathit{i}=1,2,\dots ,\stackrel{-}{n}+7$, (see nag_fit_1dspline_knots (e02ba)), i.e.,
 q s(x) = ∑ ciNi(x). i = 1
$s(x) = ∑ i=1q ci Ni(x) .$
Here q = n + 3$q=\stackrel{-}{n}+3$, n$\stackrel{-}{n}$ is the number of intervals of the spline and Ni(x)${N}_{i}\left(x\right)$ denotes the normalized B-spline of degree 3$3$ (order 4$4$) defined upon the knots λi,λi + 1,,λi + 4${\lambda }_{i},{\lambda }_{i+1},\dots ,{\lambda }_{i+4}$. The prescribed argument x$x$ must satisfy
 λ4 ≤ x ≤ λn + 4 . $λ4 ≤ x ≤ λ n-+4 .$
At a simple knot λi${\lambda }_{i}$ (i.e., one satisfying λi1 < λi < λi + 1${\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$x=u$ where (exactly) r$r$ knots coincide (such a point is termed a knot of multiplicity r$r$), the values of the derivatives of order 4j$4-\mathit{j}$, for j = 1,2,,r$\mathit{j}=1,2,\dots ,r$, are in general discontinuous. (Here 1r4$1\le r\le 4$; r > 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:
 (i) carrying out a binary search for the knot interval containing the argument x$x$ (see Cox (1978)), (ii) evaluating the nonzero B-splines of orders 1$1$, 2$2$, 3$3$ and 4$4$ by recurrence (see Cox (1972) and Cox (1978)), (iii) computing all derivatives of the B-splines of order 4$4$ by applying a second recurrence to these computed B-spline values (see de Boor (1972)), (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(x)$s\left(x\right)$ and its derivatives.
nag_fit_1dspline_deriv (e02bc) can be used to compute the values and derivatives of cubic spline fits and interpolants produced by nag_fit_1dspline_knots (e02ba).
If only values and not derivatives are required, nag_fit_1dspline_eval (e02bb) may be used instead of nag_fit_1dspline_deriv (e02bc), which takes about 50%$50%$ longer than nag_fit_1dspline_eval (e02bb).

## References

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

## Parameters

### Compulsory Input Parameters

1:     lamda(ncap7) – double array
ncap7, the dimension of the array, must satisfy the constraint ncap78${\mathbf{ncap7}}\ge 8$.
lamda(j)${\mathbf{lamda}}\left(\mathit{j}\right)$ must be set to the value of the j$\mathit{j}$th member of the complete set of knots, λj${\lambda }_{\mathit{j}}$, for j = 1,2,,n + 7$\mathit{j}=1,2,\dots ,\stackrel{-}{n}+7$.
Constraint: the lamda(j)${\mathbf{lamda}}\left(j\right)$ must be in nondecreasing order with
lamda(ncap73) > lamda(4)${\mathbf{lamda}}\left({\mathbf{ncap7}}-3\right)>{\mathbf{lamda}}\left(4\right)$.
2:     c(ncap7) – double array
ncap7, the dimension of the array, must satisfy the constraint ncap78${\mathbf{ncap7}}\ge 8$.
The coefficient ci${c}_{\mathit{i}}$ of the B-spline Ni(x)${N}_{\mathit{i}}\left(x\right)$, for i = 1,2,,n + 3$\mathit{i}=1,2,\dots ,\stackrel{-}{n}+3$. The remaining elements of the array are not referenced.
3:     x – double scalar
The argument x$x$ at which the cubic spline and its derivatives are to be evaluated.
Constraint: lamda(4)xlamda(ncap73)${\mathbf{lamda}}\left(4\right)\le {\mathbf{x}}\le {\mathbf{lamda}}\left({\mathbf{ncap7}}-3\right)$.
4:     left – int64int32nag_int scalar
Specifies whether left- or right-hand values of the spline and its derivatives are to be computed (see Section [Description]). Left- or right-hand values are formed according to whether left is equal or not equal to 1$1$.
If x$x$ does not coincide with a knot, the value of left is immaterial.
If x = lamda(4)$x={\mathbf{lamda}}\left(4\right)$, right-hand values are computed.
If x = lamda(ncap73)$x={\mathbf{lamda}}\left({\mathbf{ncap7}}-3\right)$, left-hand values are formed, regardless of the value of left.

### Optional Input Parameters

1:     ncap7 – int64int32nag_int scalar
Default: The dimension of the arrays lamda, c. (An error is raised if these dimensions are not equal.)
n + 7$\stackrel{-}{n}+7$, where n$\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 λ4${\lambda }_{4}$ to λn + 4${\lambda }_{\stackrel{-}{n}+4}$ over which the spline is defined).
Constraint: ncap78${\mathbf{ncap7}}\ge 8$.

None.

### Output Parameters

1:     s(4$4$) – double array
s(j)${\mathbf{s}}\left(\mathit{j}\right)$ contains the value of the (j1)$\left(\mathit{j}-1\right)$th derivative of the spline at the argument x$x$, for j = 1,2,3,4$\mathit{j}=1,2,3,4$. Note that s(1)${\mathbf{s}}\left(1\right)$ contains the value of the spline.
2:     ifail – int64int32nag_int scalar
${\mathrm{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:
ifail = 1${\mathbf{ifail}}=1$
ncap7 < 8${\mathbf{ncap7}}<8$, i.e., the number of intervals is not positive.
ifail = 2${\mathbf{ifail}}=2$
Either lamda(4)lamda(ncap73)${\mathbf{lamda}}\left(4\right)\ge {\mathbf{lamda}}\left({\mathbf{ncap7}}-3\right)$, i.e., the range over which s(x)$s\left(x\right)$ is defined is null or negative in length, or x is an invalid argument, i.e., x < lamda(4)${\mathbf{x}}<{\mathbf{lamda}}\left(4\right)$ or x > lamda(ncap73)${\mathbf{x}}>{\mathbf{lamda}}\left({\mathbf{ncap7}}-3\right)$.

## Accuracy

The computed value of s(x)$s\left(x\right)$ has negligible error in most practical situations. Specifically, this value has an absolute error bounded in modulus by 18 × cmax × machine precision, where cmax${c}_{\mathrm{max}}$ is the largest in modulus of cj,cj + 1,cj + 2${c}_{j},{c}_{j+1},{c}_{j+2}$ and cj + 3${c}_{j+3}$, and j$j$ is an integer such that λj + 3xλj + 4${\lambda }_{j+3}\le x\le {\lambda }_{j+4}$. If cj,cj + 1,cj + 2${c}_{j},{c}_{j+1},{c}_{j+2}$ and cj + 3${c}_{j+3}$ are all of the same sign, then the computed value of s(x)$s\left(x\right)$ has relative error bounded by 20 × machine precision. For full details see Cox (1978).
No complete error analysis is available for the computation of the derivatives of s(x)$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 log(n + 7)$\mathrm{log}\left(\stackrel{-}{n}+7\right)$.
Note:  the function does not test all the conditions on the knots given in the description of lamda in Section [Parameters], since to do this would result in a computation time approximately linear in n + 7$\stackrel{-}{n}+7$ instead of log(n + 7)$\mathrm{log}\left(\stackrel{-}{n}+7\right)$. All the conditions are tested in nag_fit_1dspline_knots (e02ba), however.

## Example

```function nag_fit_1dspline_deriv_example
lamda = [0;
0;
0;
0;
1;
3;
3;
3;
4;
4;
6;
6;
6;
6];
c = [10;
12;
13;
15;
22;
26;
24;
18;
14;
12;
0;
0;
0;
0];
x = 0;
left = int64(1);
[s, ifail] = nag_fit_1dspline_deriv(lamda, c, x, left)
```
```

s =

10.0000
6.0000
-10.0000
10.6667

ifail =

0

```
```function e02bc_example
lamda = [0;
0;
0;
0;
1;
3;
3;
3;
4;
4;
6;
6;
6;
6];
c = [10;
12;
13;
15;
22;
26;
24;
18;
14;
12;
0;
0;
0;
0];
x = 0;
left = int64(1);
[s, ifail] = e02bc(lamda, c, x, left)
```
```

s =

10.0000
6.0000
-10.0000
10.6667

ifail =

0

```