# NAG Library Function Documentnag_1d_spline_evaluate (e02bbc)

## 1  Purpose

nag_1d_spline_evaluate (e02bbc) evaluates a cubic spline from its B-spline representation.

## 2  Specification

#include <nag.h>
#include <nage02.h>

void  nag_1d_spline_evaluate (double x, double *s, Nag_Spline *spline,  NagError *fail)

## 3  Description

nag_1d_spline_evaluate (e02bbc) evaluates the cubic spline $s\left(x\right)$ at a prescribed argument $x$ from its augmented knot set ${\lambda }_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\stackrel{-}{n}+7$, (see nag_1d_spline_fit_knots (e02bac)) and from the coefficients ${c}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,q$, in its B-spline representation

$s x = ∑ i=1 q c i N i x$
Here $q=\stackrel{-}{n}+3$, where $\stackrel{-}{n}$ is the number of intervals of the spline, and ${N}_{i}\left(x\right)$ denotes the normalized B-spline of degree 3 defined upon the knots ${\lambda }_{i},{\lambda }_{i+1},\dots ,{\lambda }_{i+4}$. The prescribed argument $x$ must satisfy ${\lambda }_{4}\le x\le {\lambda }_{\stackrel{-}{n}+4}$.
It is assumed that ${\lambda }_{\mathit{j}}\ge {\lambda }_{\mathit{j}-1}$, for $\mathit{j}=2,3,\dots ,\stackrel{-}{n}+7$, and ${\lambda }_{\stackrel{-}{n}+4}>{\lambda }_{4}$.
The method employed is that of evaluation by taking convex combinations due to de Boor (1972). For further details of the algorithm and its use see Cox (1972) and Cox (1978).
It is expected that a common use of nag_1d_spline_evaluate (e02bbc) will be the evaluation of the cubic spline approximations produced by nag_1d_spline_fit_knots (e02bac). A generalization of nag_1d_spline_evaluate (e02bbc) which also forms the derivative of $s\left(x\right)$ is nag_1d_spline_deriv (e02bcc). nag_1d_spline_deriv (e02bcc) takes about 50% longer than nag_1d_spline_evaluate (e02bbc).

## 4  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
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

## 5  Arguments

1:     x – doubleInput
On entry: the argument $x$ at which the cubic spline is to be evaluated.
Constraint: $\mathbf{spline}\mathbf{\to }\mathbf{lamda}\left[3\right]\le {\mathbf{x}}\le \mathbf{spline}\mathbf{\to }\mathbf{lamda}\left[\mathbf{spline}\mathbf{\to }\mathbf{n}-4\right]$.
2:     s – double *Output
On exit: the value of the spline, $s\left(x\right)$.
3:     spline – Nag_Spline *
Pointer to structure of type Nag_Spline with the following members:
n – IntegerInput
On entry: $\stackrel{-}{n}+7$, where $\stackrel{-}{n}$ is the number of intervals (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{spline}\mathbf{\to }\mathbf{n}\ge 8$.
lamda – doubleInput
On entry: a pointer to which memory of size $\mathbf{spline}\mathbf{\to }\mathbf{n}$ must be allocated. $\mathbf{spline}\mathbf{\to }\mathbf{lamda}\left[j-1\right]$ must be set to the value of the $j$th member of the complete set of knots, ${\lambda }_{j}$ for $j=1,2,\dots ,\stackrel{-}{n}+7$.
Constraint: the ${\lambda }_{j}$ must be in non-decreasing order with $\mathbf{spline}\mathbf{\to }\mathbf{lamda}\left[\mathbf{spline}\mathbf{\to }\mathbf{n}-4\right]>\mathbf{spline}\mathbf{\to }\mathbf{lamda}\left[3\right]$.
c – doubleInput
On entry: a pointer to which memory of size $\mathbf{spline}\mathbf{\to }\mathbf{n}-4$ must be allocated. $\mathbf{spline}\mathbf{\to }\mathbf{c}$ holds the coefficient ${c}_{\mathit{i}}$ of the B-spline ${N}_{\mathit{i}}\left(x\right)$, for $\mathit{i}=1,2,\dots ,\stackrel{-}{n}+3$.
Under normal usage, the call to nag_1d_spline_evaluate (e02bbc) will follow a call to nag_1d_spline_fit_knots (e02bac), nag_1d_spline_interpolant (e01bac) or nag_1d_spline_fit (e02bec). In that case, the structure spline will have been set up correctly for input to nag_1d_spline_evaluate (e02bbc).
4:     fail – NagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_ABSCI_OUTSIDE_KNOT_INTVL
On entry, x must satisfy $\mathbf{spline}\mathbf{\to }\mathbf{lamda}\left[3\right]\le {\mathbf{x}}\le \mathbf{spline}\mathbf{\to }\mathbf{lamda}\left[\mathbf{spline}\mathbf{\to }\mathbf{n}-4\right]$:
$\mathbf{spline}\mathbf{\to }\mathbf{lamda}\left[3\right]=〈\mathit{\text{value}}〉$, ${\mathbf{x}}=〈\mathit{\text{value}}〉$, $\mathbf{spline}\mathbf{\to }\mathbf{lamda}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$.
In this case s is set arbitrarily to zero.
NE_INT_ARG_LT
On entry, $\mathbf{spline}\mathbf{\to }\mathbf{n}$ must not be less than 8: $\mathbf{spline}\mathbf{\to }\mathbf{n}=〈\mathit{\text{value}}〉$.

## 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×{c}_{\mathrm{max}}×$ machine precision, 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 a relative error not exceeding $20×$ machine precision in modulus. For further details see Cox (1978).

## 8  Further Comments

The time taken by nag_1d_spline_evaluate (e02bbc) is approximately C $×\left(1+0.1×\mathrm{log}\left(\stackrel{-}{n}+7\right)\right)$ seconds, where C is a machine-dependent constant.
Note: the function does not test all the conditions on the knots given in the description of $\mathbf{spline}\mathbf{\to }\mathbf{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 nag_1d_spline_fit_knots (e02bac), however, and the knots returned by nag_1d_spline_interpolant (e01bac) or nag_1d_spline_fit (e02bec) will satisfy the conditions.

## 9  Example

Evaluate at 9 equally-spaced points in the interval $1.0\le x\le 9.0$ the cubic spline with (augmented) knots 1.0, 1.0, 1.0, 1.0, 3.0, 6.0, 8.0, 9.0, 9.0, 9.0, 9.0 and normalized cubic B-spline coefficients 1.0, 2.0, 4.0, 7.0, 6.0, 4.0, 3.0.
The example program is written in a general form that will enable a cubic spline with $\stackrel{-}{n}$ intervals, in its normalized cubic B-spline form, to be evaluated at $m$ equally-spaced points in the interval $\mathbf{spline}\mathbf{\to }\mathbf{lamda}\left[3\right]\le x\le \mathbf{spline}\mathbf{\to }\mathbf{lamda}\left[\stackrel{-}{n}+3\right]$. The program is self-starting in that any number of datasets may be supplied.

### 9.1  Program Text

Program Text (e02bbce.c)

### 9.2  Program Data

Program Data (e02bbce.d)

### 9.3  Program Results

Program Results (e02bbce.r)
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 62, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.7081049680709839, "perplexity_flag": "middle"}