PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_fit_1dspline_deriv_vector (e02bf)
Purpose
nag_fit_1dspline_deriv_vector (e02bf) evaluates a cubic spline and up to its first three derivatives from its B-spline representation at a vector of points.
nag_fit_1dspline_deriv_vector (e02bf) can be used to compute the values and derivatives of cubic spline fits and interpolants produced by reference to
nag_interp_1d_spline (e01ba),
nag_fit_1dspline_knots (e02ba) and
nag_fit_1dspline_auto (e02be).
Syntax
[
s,
ixloc,
iwrk,
ifail] = e02bf(
start,
lamda,
c,
deriv,
xord,
x,
ixloc,
iwrk, 'ncap7',
ncap7, 'nx',
nx, 'liwrk',
liwrk)
[
s,
ixloc,
iwrk,
ifail] = nag_fit_1dspline_deriv_vector(
start,
lamda,
c,
deriv,
xord,
x,
ixloc,
iwrk, 'ncap7',
ncap7, 'nx',
nx, 'liwrk',
liwrk)
Description
nag_fit_1dspline_deriv_vector (e02bf) evaluates the cubic spline
and optionally derivatives up to order
for a vector of points
, for
. It is assumed that
is represented in terms of its B-spline coefficients
, for
, and (augmented) ordered knot set
, for
,
(see
nag_fit_1dspline_knots (e02ba) and
nag_fit_1dspline_auto (e02be)),
i.e.,
Here , is the number of intervals of the spline and denotes the normalized B-spline of degree (order ) defined upon the knots . The knots are the interior knots. The remaining knots, , , , and , , , are the exterior knots. The knots and are the boundaries of the spline.
Only abscissae satisfying,
will be evaluated. At a simple knot
(i.e., one satisfying
), 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
where (exactly)
knots coincide (such a point is termed a knot of multiplicity
), the values of the derivatives of order
, for
, are, in general, discontinuous. (Here
;
is not meaningful.) The maximum order of the derivatives to be evaluated
, and the left- or right-handedness of the computation when an abscissa corresponds exactly to an interior knot, are determined by the value of
deriv.
Each abscissa (point at which the spline is to be evaluated)
contained in
x has an associated enclosing interval number,
either supplied or returned in
ixloc (see argument
start). A simple call to
nag_fit_1dspline_deriv_vector (e02bf) would set
and the contents of
ixloc need never be set nor referenced, and the following description on modes of operation can be ignored. However, where efficiency is an important consideration, the following description will help to choose the appropriate mode of operation.
The interval numbers are used to determine which B-splines must be evaluated for a given abscissa, and are defined as
The algorithm has two modes of vectorization, termed here sorted and unsorted, which are selectable by the argument
start.
Furthermore, if the supplied abscissae are sufficiently ordered, as indicated by the argument
xord, the algorithm will take advantage of significantly faster methods for the determination of both the interval numbers and the subsequent spline evaluations.
The sorted mode has two phases, a sorting phase and an evaluation phase. This mode is recommended if there are many abscissae to evaluate relative to the number of intervals of the spline, or the abscissae are distributed relatively densely over a subsection of the spline. In the first phase,
is determined for each
and a permutation is calculated to sort the
by interval number. The first phase may be either partially or completely by-passed using the argument
start if the enclosing segments and/or the subsequent ordering are already known
a priori, for example if multiple spline coefficients
c are to be evaluated over the same set of knots
lamda.
In the second phase of the sorted mode, spline approximations are evaluated by segment, so that non-abscissa dependent calculations over a segment may be reused in the evaluation for all abscissae belonging to a specific segment. For example, all third derivatives of all abscissae in the same segment will be identical.
In the unsorted mode of vectorization, no
a priori segment sorting is performed, and if the abscissae are not sufficiently ordered, the evaluation at an abscissa will be independent of evaluations at other abscissae; also non-abscissa dependent calculations over a segment will be repeated for each abscissa in a segment. This may be quicker if the number of abscissa is small in comparison to the number of knots in the spline, and they are distributed sparsely throughout the domain of the spline. This is effectively a direct vectorization of
nag_fit_1dspline_eval (e02bb) and
nag_fit_1dspline_deriv (e02bc), although if the enclosing interval numbers
are known, these may again be provided.
If the abscissae are sufficiently ordered, then once the first abscissa in a segment is known, an efficient algorithm will be used to determine the location of the final abscissa in this segment. The spline will subsequently be evaluated in a vectorized manner for all the abscissae indexed between the first and last of the current segment.
If no derivatives are required, the spline evaluation is calculated by taking convex combinations due to
de Boor (1972). Otherwise, the calculation of
and its derivatives is based upon,
(i) |
evaluating the nonzero B-splines of orders , , and by recurrence (see Cox (1972) and Cox (1978)), |
(ii) |
computing all derivatives of the B-splines of order by applying a second recurrence to these computed B-spline values (see de Boor (1972)), |
(iii) |
multiplying the fourth-order B-spline values and their derivative by the appropriate B-spline coefficients, and summing, to yield the values of and its derivatives. |
The method of convex combinations is significantly faster than the recurrence based method. If higher derivatives of order or are not required, as much computation as possible is avoided.
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:
– int64int32nag_int scalar
-
Indicates the completion state of the first phase of the algorithm.
- The enclosing interval numbers for the abscissae contained in x have not been determined, and you wish to use the sorted mode of vectorization.
- The enclosing interval numbers have been determined and are provided in ixloc, however the required permutation and interval related information has not been determined and you wish to use the sorted mode of vectorization.
- You wish to use the sorted mode of vectorization, and the entire first phase has been completed, with the enclosing interval numbers supplied in ixloc, and the required permutation and interval related information provided in iwrk (from a previous call to nag_fit_1dspline_deriv_vector (e02bf)).
- The enclosing interval numbers for the abscissae contained in x have not been determined, and you wish to use the unsorted mode of vectorization.
- The enclosing interval numbers for the abscissae contained in x have been supplied in ixloc, and you wish to use the unsorted mode of vectorization.
Constraint:
, , , or .
Additional: or should be used unless you are sure that the knot set is unchanged between calls.
- 2:
– double array
-
must be set to the value of the th member of the complete set of knots, , for .
Constraint:
the must be in nondecreasing order with
.
- 3:
– double array
-
The coefficient
of the B-spline , for . The remaining elements of the array are not referenced.
- 4:
– int64int32nag_int scalar
-
The order of derivatives required.
If left derivatives are calculated, otherwise right derivatives are calculated.
For abscissae satisfying or only right-handed or left-handed computation will be used respectively. For abscissae which do not coincide exactly with a knot, the handedness of the computation is immaterial.
- No derivatives required.
- Only and its first derivative are required.
- Only and its first and second derivatives are required.
- and its first, second and third derivatives are required.
Note: if is greater than only the derivatives up to and including will be returned.
- 5:
– int64int32nag_int scalar
-
Indicates whether
x is supplied in a sufficiently ordered manner. If
x is sufficiently ordered
nag_fit_1dspline_deriv_vector (e02bf) will complete faster.
- The abscissae in x are ordered at least by ascending interval, in that any two abscissae contained in the same interval are only separated by abscissae in the same interval, and the intervals are arranged in ascending order. For example, , for .
- The abscissae in x are not sufficiently ordered.
- 6:
– double array
-
The abscissae
, for
. If
or
then evaluations will only be performed for these
satisfying
. Otherwise evaluation will be performed unless the corresponding element of
ixloc contains an invalid interval number. Please note that if the
is a valid interval number then no check is made that
actually lies in that interval.
Constraint:
at least one abscissa must fall between and .
- 7:
– int64int32nag_int array
-
If
,
or
, if you wish
to be evaluated,
must be the enclosing interval number
of the abscissae
(see
(1)). If you do not wish
to be evaluated, you may set the interval number to be either less than
or greater than
.
Otherwise,
ixloc need not be set.
Constraint:
if
,
or
, at least one element of
ixloc must be between
and
.
- 8:
– int64int32nag_int array
-
If
,
iwrk must be unchanged from a previous call to
nag_fit_1dspline_deriv_vector (e02bf) with
or
.
Otherwise,
iwrk need not be set.
Optional Input Parameters
- 1:
– int64int32nag_int scalar
-
Default:
the dimension of the arrays
lamda,
c. (An error is raised if these dimensions are not equal.)
, where
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
to
over which the spline is defined). Note that if
nag_fit_1dspline_auto (e02be) was used to generate the knots and spline coefficients then
ncap7 should contain the same value as returned in
n by
nag_fit_1dspline_auto (e02be).
Constraint:
.
- 2:
– int64int32nag_int scalar
-
Default:
the dimension of the array
x and the dimension of the array
ixloc. (An error is raised if these dimensions are not equal.)
, the total number of abscissae contained in
x, including any that will not be evaluated.
Constraint:
.
- 3:
– int64int32nag_int scalar
-
Default:
the dimension of the array
iwrk.
The dimension of the array
iwrk.
Constraint:
if , or , .
Output Parameters
- 1:
– double array
-
The first dimension of the array
s will be
, regardless of the acceptability of the elements of
x.
The second dimension of the array
s will be
.
If
is valid,
will contain the (
)th derivative of
, for
and
. In particular,
will contain the approximation of
for all legal values in
x.
- 2:
– int64int32nag_int array
-
If
,
or
,
ixloc is unchanged on exit.
Otherwise,
, contains the enclosing interval number , for the abscissa supplied in , for . Evaluations will only be performed for abscissae satisfying . If evaluation is not performed is set to if or if .
- 3:
– int64int32nag_int array
-
If
or
,
iwrk is unchanged on exit.
Otherwise,
iwrk contains the required permutation of elements of
x, if any, and information related to the division of the abscissae
between the intervals derived from
lamda.
- 4:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Note: nag_fit_1dspline_deriv_vector (e02bf) may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the function:
-
-
On entry, at least one element of
x has an enclosing interval number in
ixloc outside the set allowed by the provided spline.
-
-
On entry, all elements of
x had enclosing interval numbers in
ixloc outside the domain allowed by the provided spline.
-
-
Constraint: , , , or .
-
-
-
-
Constraint: .
-
-
Constraint: .
-
-
Constraint: .
-
-
-
-
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
Accuracy
The computed value of
has negligible error in most practical situations. Specifically, this value has an absolute error bounded in modulus by
, where
is the largest in modulus of
,
,
and
, and
is an integer such that
. If
,
,
and
are all of the same sign, then the computed value of
has relative error bounded by
. For full details see
Cox (1978).
No complete error analysis is available for the computation of the derivatives of . However, for most practical purposes the absolute errors in the computed derivatives should be small. Note that this is in comparison to the derivatives of the spline, which may or may not be comparable to the derivatives of the function that has been approximated by the spline.
Further Comments
If using the sorted mode of vectorization, the time required for the first phase to determine the enclosing intervals is approximately proportional to
. The time required to then generate the required permutations and interval information is
if
x is ordered sufficiently, or at worst
if
x is not ordered. The time required by the second phase is then proportional to
.
If using the unsorted mode of vectorization, the time required is proportional to if the enclosing interval numbers are not provided, or if they are provided. However, the repeated calculation of various quantities will typically make this slower than the sorted mode when the ratio of abscissae to knots is high, or the abscissae are densely distributed over a relatively small subset of the intervals of the spline.
Note: the function does not test all the conditions on the knots given in the description of
lamda in
Arguments, since to do this would result in a computation time with a linear dependency upon
instead of
. All the conditions are tested in
nag_fit_1dspline_knots (e02ba) and
nag_fit_1dspline_auto (e02be), however.
Example
This example fits a spline through a set of data points using
nag_fit_1dspline_auto (e02be) and then evaluates the spline at a set of supplied abscissae.
Open in the MATLAB editor:
e02bf_example
function e02bf_example
fprintf('e02bf example results\n\n');
npts = 15;
x(1:8) = [ 0; 0.5; 1; 1.5; 2; 2.5; 3; 4];
y(1:8) = [-1.1; -0.372; 0.431; 1.69; 2.11; 3.1; 4.23; 4.35];
x(9:npts) = [4.5; 5; 5.5; 6; 7; 7.5; 8];
y(9:npts) = [4.81; 4.61; 4.79; 5.23; 6.35; 7.19; 7.97];
w(1:npts) = 1;
w(3) = 1.5;
cstart = 'c';
sfac = 0.001;
nest = int64(npts + 4);
lamda = zeros(nest, 1);
wrk = zeros(4*npts + 16*nest + 41, 1);
iwrk1 = zeros(nest, 1, 'int64');
[n, lamda, c, fp, wrk, iwrk1, ifail] = ...
e02be(...
cstart, x, y, w, sfac, nest, lamda, wrk, iwrk1);
nip = 20;
xe = [6.5178; 7.2463; 1.0159; 7.3070; 5.0589; 0.7803; 2.2280; 4.3751; ...
7.6601; 7.7191; 1.2609; 7.7647; 7.6573; 3.8830; 6.4022; 1.1351; ...
3.3741; 7.3259; 6.3377; 7.6759];
xe = sort(xe);
ixloc = zeros(nip, 1, 'int64');
iwrk2 = zeros(3+3*nip, 1, 'int64');
xord = int64(0);
start = int64(0);
deriv = int64(3);
[s, ixloc, iwrk2, ifail] = e02bf(...
start, lamda, c, deriv, xord, xe, ixloc, iwrk2);
fprintf('%8s%7s%10s%10s%10s%10s\n',...
'x','ixloc','s(x)','ds/dx','d2s/dx2','d3s/dx3');
sd2 = min(abs(deriv),3) + 1;
for r = 1:nip
if ixloc(r) >= 4 && ixloc(r) <= n
fprintf('%8.4f%7d%10.4f%10.4f%10.4f%10.4f\n', ...
xe(r), ixloc(r), s(r, 1:sd2));
else
fprintf('%8.4f%7d\n', x(r), ixloc(r));
end
end
e02bf example results
x ixloc s(x) ds/dx d2s/dx2 d3s/dx3
0.7803 4 0.0067 1.6216 2.5007 7.5980
1.0159 5 0.4747 2.4179 3.8175 -22.1715
1.1351 5 0.7838 2.7154 1.1746 -22.1715
1.2609 5 1.1273 2.6878 -1.6146 -22.1715
2.2280 7 2.4751 1.9559 3.0615 -6.6690
3.3741 9 4.4165 -0.1181 -2.0644 10.2964
3.8830 9 4.3152 0.1646 3.1754 10.2964
4.3751 10 4.7199 0.8519 -3.0718 -19.8662
5.0589 12 4.6105 -0.1036 2.9075 -4.4467
6.3377 14 5.5563 0.9931 0.3321 1.3065
6.4022 14 5.6211 1.0172 0.4163 1.3065
6.5178 14 5.7418 1.0741 0.5674 1.3065
7.2463 15 6.7486 1.7074 0.4905 -2.8697
7.3070 15 6.8531 1.7319 0.3163 -2.8697
7.3259 15 6.8859 1.7374 0.2621 -2.8697
7.6573 15 7.4586 1.6667 -0.6889 -2.8697
7.6601 15 7.4633 1.6647 -0.6970 -2.8697
7.6759 15 7.4895 1.6534 -0.7423 -2.8697
7.7191 15 7.5602 1.6186 -0.8663 -2.8697
7.7647 15 7.6330 1.5761 -0.9971 -2.8697
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015