Proiecte Functii Spline
Proiecte Functii Spline
Purpose 3aptknt
Acceptable knot sequence
Description Assume that tau has at least k entries, is nondecreasing, and satisfies
tau(i)<tau(i+k-1) for all i. Then the knot sequence knots returned by the
command aptknt(tau,k) is acceptable for interpolation to data at the sites tau
in the sense that there is exactly one spline of order k with knot sequence knots
that matches given values at those sites. This is so because the sequence knots
returned satisfies the Schoenberg-Whitney conditions
knots(i) < tau(i) < knots(i+k), i=1:length(tau)
with equality only at the extreme knots, each of which occurs with exact
multiplicity k.
If tau has fewer than k entries, then k is reduced to the value length(tau); the
k used is optionally returned. An error results if tau fails to be nondecreasing
and/or tau(i) equals tau(i+k-1) for some i.
Examples If tau is equally spaced, e.g., equal to linspace(a,b,n) for some n>=4, and y is
a sequence of the same size as tau, then sp = spapi(aptknt(tau,4),tau,y)
gives the cubic spline interpolant with the not-a-knot end condition. This is the
same cubic spline as produced by the command spline(tau,y), but in B-form
rather than ppform.
Cautionary If tau is very nonuniform, then use of the resulting knot sequence for
Note interpolation to data at the sites tau may lead to unsatisfactory results.
3-7
augknt
Purpose 3augknt
Augment a knot sequence
Examples If you want to construct a cubic spline on the interval [a..b], with two
continuous derivatives, and with the interior break sequence xi, then
augknt([a,b,xi],4) is the knot sequence you should use.
If you want to use Hermite cubics instead, i.e., a cubic spline with only one
continuous derivative, then the appropriate knot sequence is
augknt([a,xi,b],4,2).
3-8
aveknt
Purpose 3aveknt
Provide knot averages
Description aveknt(t,k) returns the averages of successive k-1 knots, i.e., the sites
ti : = ( t i + 1 + + t i + k 1 ) ( k 1 ) , i = 1 :n
With k and the strictly increasing sequence breaks given, the statements
t = augknt(breaks,k); x = aveknt(t);
sp = spapi(t,x,sin(x));
For sp the B-form of a scalar-valued univariate spline function, and with tstar
and a computed as
tstar = aveknt(fnbrk(sp,'knots'),fnbrk(sp,'order'));
a = fnbrk(sp,'coefs');
the points ( tstar(i), a(i) ) constitute the control points of the spline, i.e., the
vertices of the splines control polygon.
3-9
bkbrk
Purpose 3bkbrk
Part(s) of an almost block-diagonal matrix
This utility program is not likely to be of interest to the casual user. It is used
in slvblk to decode the information, provided by spcol, about a spline
collocation matrix in an almost block diagonal form especially suited for
splines. But bkbrk can also decode the almost block-diagonal form used in [1].
bkbrk(blokmat) returns nothing, but the details are printed out. This is of use
when trying to understand what went wrong with such a matrix.
References [1] C. de Boor and R. Weiss, SOLVEBLOK: A package for solving almost block
diagonal linear systems, ACM Trans. Mathem. Software 6 (1980), 80 87.
3-10
brk2knt
Purpose 3brk2knt
Breaks with multiplicities into knots
Description The sequence knots is the sequence breaks but with breaks(i) occurring
mults(i) times, all i. In particular, breaks(i) will not appear unless
mults(i)>0. If, as one would expect, breaks is a strictly increasing sequence,
then knots contains each breaks(i) exactly mults(i) times.
If mults does not have exactly as many entries as does breaks, then all
mults(i) are set equal to mults(1).
If, as one would expect, breaks is strictly increasing and all multiplicities are
positive, then, for each i, index(i) is the first place in knots at which
breaks(i) appears.
3-11
bspligui
Purpose 3bspligui
Experiment with a B-spline as a function of its knots
Syntax bspligui
Description bspligui starts a graphical user interface (GUI) for exploring how a B-spline
depends on its knots. As you add, move, or delete knots, you see the B-spline
and its first three derivatives change accordingly.
You observe the following basic facts about the B-spline with knot sequence
t0 tk :
The B-spline is positive on the open interval ( t 0 ..t k ) . It is zero at the end
knots, t 0 and t k , unless they are knots of multiplicity k. The B-spline is also
zero outside the closed interval [ t 0 ..t k ] , but that part of the B-spline is not
shown in the GUI.
Even at its maximum, the B-spline is never bigger than 1. It reaches the
value 1 inside the interval ( t 0 ..t k ) only at a knot of multiplicity at least
k 1 . On the other hand, that maximum cannot be arbitrarily small; it
seems smallest when there are no interior knots.
The B-spline is piecewise polynomial of order k , i.e., its polynomial pieces
all are of degree < k . For k = 1:4 , you can even observe that all its
nonzero polynomial pieces are of exact degree k-1, by looking at the first
three derivatives of the B-spline. This means that the degree goes up/down
by 1 every time you add/delete a knot.
Each knot t j is a break for the B-spline, but it is permissible for several
knots to coincide. Therefore, the number of nontrivial polynomial pieces is
maximally k (when all the knots are different) and minimally 1 (when there
are no interior knots), and any number between 1 and k is possible.
The smoothness of the B-spline across a break depends on the multiplicity of
the corresponding knot. If the break occurs in the knot sequence m times,
then the ( k m ) th derivative of the B-spline has a jump across that break,
while all derivatives of order lower than ( k m ) are continuous across that
break. Thus, by varying the multiplicity of a knot, you can control the
smoothness of the B-spline across that knot.
As one knot approaches another, the highest derivative that is continuous
across both develops a jump and the higher derivatives become unbounded.
But nothing dramatic happens in any of the lower-order derivatives.
3-12
bspligui
The B-spline is bell-shaped in the following sense: if the first derivative is not
identically zero, then it has exactly one sign change in the interval ( t 0 ..t k ) ,
hence the B-spline itself is unimodal, meaning that it has exactly one
maximum. Further, if the second derivative is not identically zero, then it
has exactly two sign changes in that interval. Finally, if the third derivative
is not identically zero, then it has exactly three sign changes in that interval.
This illustrates the fact that, for j = 0: k 1 , if the j th derivative is not
identically zero, then it has exactly j sign changes in the interval ( t 0 ..t k ) ;
it is this property that is meant by the term bell-shaped. For this claim to
be strictly true, one has to be careful with the meaning of sign change in
case there are knots with multiplicities. For example, the ( k 1 ) st
derivative is piecewise constant, hence it cannot have k 1 sign changes in
the straightforward sense unless there are k polynomial pieces, i.e., unless
all the knots are simple.
3-13
bspline
Purpose 3bspline
Display a B-spline and its polynomial pieces
Syntax bspline(t)
bspline(t,window)
pp = bspline(t)
Description bspline(t) plots B(|t) , i.e., the B-spline with knot sequence t, as well as
the polynomial pieces of which it is composed.
3-14
chbpnt
Purpose 3chbpnt
Good data sites, the Chebyshev-Demko points
Description tau = chbpnt(t,k) are the extreme sites of the Chebyshev spline of order k
with knot sequence t. These are particularly good sites at which to interpolate
data by splines of order k with knot sequence t because the resulting
interpolant is often quite close to the best uniform approximation from that
spline space to the function whose values at tau are being interpolated.
then a good approximation to the square-root function from that specific spline
space is given by
x = chbpnt(t,k); sp = spapi(t,x,sqrt(x));
Algorithm The Chebyshev spline for the given knot sequence and order is constructed
iteratively, using the Remez algorithm, using as initial guess the spline that
takes alternately the values 1 and -1 at the sequence aveknt(t,k). The demo
chebdem gives a detailed discussion of one version of the process as applied to
a particular example.
3-15
csape
Purpose 3csape
Cubic spline interpolation with end conditions
Syntax pp = csape(x,y)
pp = csape(x,y,conds)
Description pp = csape(x,y) is the ppform of a cubic spline s with knot sequence x that
satisfies s(x(j)) = y(:,j) for all j, as well as an additional end condition at
the ends (meaning the leftmost and at the rightmost data site), namely the
default condition listed below. The data values y(:,j) may be scalars, vectors,
matrices, even ND-arrays. Data values at the same data site are averaged.
3-16
csape
Here, e is a ( e is b ), i.e., the left (right) end, in case j is 1 (j is 2), and (in the
Lagrange condition) p is the cubic polynomial that interpolates to the given
data at e and the three sites nearest e .
If conds(j) is not specified or is different from 0, 1, or 2, then it is taken to be
1 and the corresponding endcondvals(:,j) is taken to be the corresponding
default value.
The default value for endcondvals(:,j) is the derivative of the cubic interpolant
at the nearest four sites in case conds(j) is 1, and is 0 otherwise.
It is also possible to handle gridded data, by having x be a cell array containing
m univariate meshes and, correspondingly, having y be an m -dimensional
array (or an m + r -dimensional array if the function is to be r -valued).
Correspondingly, conds is a cell array with m entries, and end condition
values may be correspondingly supplied in each of the m variables. This, as the
last example below, of bicubic spline interpolation, makes clear, may require
you to supply end conditions for end conditions.
This command calls on a much expanded version of the Fortran routine CUBSPL
in PGS.
3-17
csape
Examples csape(x,y) provides the cubic spline interpolant with the Lagrange end
conditions, while csape(x,y,[2 2]) provides the variational, or natural cubic
spline interpolant, as does csape(x,y,'v').
csape([-1 1],[3 -1 1 6],[1 2]) provides the cubic polynomial p for which
2 3
Dp( 1) = 3 , p( 1) = 1 , p(1) = 1 , D p(1) = 6 , i.e., p(x) = x .
Finally, csape([-1 1],[-1 1]) provides the straight line p for which
p(1) = 1, i.e., p ( x ) = x .
End conditions other than the ones listed earlier can be handled along the
following lines. Suppose that we want to enforce the condition
2
( s ) := aDs ( e ) + bD s ( e ) = c
for given scalars a , b , and c , and with e equal to x(1). Then one could compute
the cubic spline interpolant s 1 to the given data using the default end condition
as well as the cubic spline interpolant s 0 to zero data and some (nontrivial) end
condition at e , and then obtain the desired interpolant in the form
s = s1 + ( ( c ) ( s1 ) ) ( s0 ) s0
Here are the (not inconsiderable) details (in which the first polynomial piece of
s 1 and s 0 is pulled out to avoid differentiating all of s 1 and s 0 ):
pp1 = csape(x,y);
dp1 = fnder(fnbrk(pp1,1));
pp0 = csape(x,[1,zeros(1,length(y)),0],[1,0]);
dp0 = fnder(fnbrk(pp0,1));
e = x(1);
lam1 = a*fnval(dp1,e) + b*fnval(fnder(dp1),e);
lam0 = a*fnval(dp0,e) + b*fnval(fnder(dp0),e);
pp = fncmb(pp0,(c-lam1)/lam0,pp1);
3-18
csape
sph = csape({x,y},v,{'clamped','periodic'});
values = fnval(sph,{0:.1:4,-2:.1:2});
surf(squeeze(values(1,:,:)),squeeze(values(2,:,:)),...
squeeze(values(3,:,:))); axis equal, axis off
The lines involving fnval and surf could have been replaced by the simple
command: fnplt(sph). Note that v is a 3-dimensional array, with v(:,i+1,j)
the 3-vector to be matched at (x(i),y(j)), i=1:5, j=1:5. Note further that,
in accordance with conds{1} being 'clamped', size(v,2) is 7 (and not 5), with
the first and last entry of v(r,:,j) specifying the end slopes to be matched.
Here is a bivariate example that shows the need for supplying end conditions
of end conditions when supplying end conditions in both variables. We
reproduce the bicubic polynomial g(x,y) = x^3y^3 by complete bicubic
interpolation. We derive the needed data, including end condition values,
directly from g in order to make it easier for you to see just how the end
condition values must be placed. We also check the result.
sites = {[0 1],[0 2]}; coefs = zeros(4,4); coefs(1,1) = 1;
g = ppmak({bx,by},coefs);
Dxg = fnval(fnder(g,[1 0]),sites);
Dyg = fnval(fnder(g,[0 1]),sites);
Dxyg = fnval(fnder(g,[1 1]),sites);
f = csape(sites,[Dxyg(1,1), Dxg(1,:), Dxyg(1,2); ...
Dyg(:,1), fnval(g,sites), Dyg(:,2) ; ...
Dxyg(2,1), Dxg(2,:), Dxyg(2,2)], ...
{'complete','complete'});
if any(squeeze(fnbrk(f,'c'))-coefs), 'this is wrong', end
Algorithm The relevant tridiagonal linear system is constructed and solved using the
sparse matrix capabilities of MATLAB.
Cautionary csape recognizes that you supplied explicit end condition values by the fact
Note that you supplied exactly two more data values than data sites. In particular,
even when using different end conditions at the two ends, if you wish to supply
an end condition value at one end, you must also supply one for the other end.
3-19
csapi
Purpose 3csapi
Cubic spline interpolation
Syntax pp = csapi(x,y)
values = csapi(x,y,xx)
Description pp=csapi(x,y) returns the ppform of a cubic spline s with knot sequence x that
takes the value y(:,j) at x(j) for j=1:length(x). The values y(:,j) can be
scalars, vectors, matrices, even ND-arrays. Data points with the same data site
are averaged and then sorted by their sites. With x the resulting sorted data
sites, the spline s satisfies the not-a-knot end conditions, namely
3 3 3
jump x(2) D s = 0 = jump x(end 1) D s (with D s the third derivative of s ).
If x is a cell array, containing sequences x1, ..., xm, of lengths n1, ..., nm
respectively, then y is expected to be an array, of size [n1,...,nm] (or of size
[d,n1,...,nm] if the interpolant is to be d-valued). In that case, pp is the
ppform of an m-cubic spline interpolant s to such data. In particular, now
s(x1(i 1), , xm(i m)) equals y(:, i 1, , i m) for i 1 = 1: n1, , i m = 1: nm .
You can use the structure pp, in fnval, fnder, fnplt, etc, to evaluate,
differentiate, plot, etc, this interpolating cubic spline.
except that the description of the spline obtained this second way will use no
break at x(2) and x(n-1).
3-20
csapi
Note the reversal of x and y in the call to meshgrid, needed since MATLAB
likes to think of the entry z(i,j) as the value at (x(j),y(i)) while this toolbox
follows the Approximation Theory standard of thinking of z(i,j) as the value
at (x(i),y(j)). Similar caution has to be exerted when values of such a
bivariate spline are to be plotted with the aid of the MATLAB mesh function, as
is shown here (note the use of the transpose of the matrix of values obtained
from fnval).
xf = linspace(x(1),x(end),41); yf = linspace(y(1),y(end),41);
mesh(xf, yf, fnval( bcs, {xf, yf}).')
Algorithm The relevant tridiagonal linear system is constructed and solved, using the
MATLAB sparse matrix capability.
The not-a-knot end condition is used, thus forcing the first and second
polynomial piece of the interpolant to coincide, as well as the second-to-last and
the last polynomial piece.
3-21
csaps
Purpose 3csaps
Cubic smoothing spline
Description pp=csaps(x,y) returns the ppform of a cubic smoothing spline f to the given
data x,y, with the value of f at the data site x(j) approximating the data
value y(:,j), for j=1:length(x). The values may be scalars, vectors, matrices,
even ND-arrays. Data points with the same site are replaced by their
(weighted) average, with its weight the sum of the corresponding weights.
This smoothing spline f minimizes
n
2
w ( j ) y ( :, j ) f ( x ( j ) )
2 2
p + (1 p) (t) D f( t) dt
j=1
2
Here, z stands for the sum of the squares of all the entries of z , n is the
number of entries of x, and the integral is over the smallest interval containing
all the entries of x. The default value for the weight vector w in the error
measure is ones(size(x)). The default value for the piecewise constant weight
2
function in the roughness measure is the constant function 1. Further, D f
denotes the second derivative of the function f . The default value for the
smoothing parameter, p, is chosen in dependence on the given data sites x.
3-22
csaps
3
p = 1 ( 1 + h 0.6 ) . You can input a p > 1, but this leads to a smoothing
spline even rougher than the variational cubic spline interpolant.
If the input p is negative or empty, then the default value for p is used. Whether
or not you specify the smoothing parameter, you can always obtain the value of
p actually used, as the optional second output argument. This is important for
experimentation which you might start with [pp,p]=csaps(x,y) in order to
obtain a reasonable first guess for p.
If you have difficulty choosing p but have some feeling for the size of the noise
in y, consider using instead spaps(x,y,tol) which, in effect, chooses p in such
a way that the roughness measure
2
(t) D
2
s ( t ) dt
w j y ( :, j ) s ( x ( j ) )
2
does not exceed the specified tol . This usually means that the error measure
equals the specified tol .
The weight function in the roughness measure can, optionally, be specified
as a (non-negative) piecewise constant function, with breaks at the data sites
x , by inputing for p a vector whose ith entry provides the value of on the
interval (x(i-1) .. x(i)) for i=2:length(x). The first entry of the input vector
p continues to be used as the desired value of the smoothness parameter p. In
this way, it is possible to insist that the resulting smoothing spline be smoother
(by making the weight function larger) or closer to the data (by making the
weight functions smaller) in some parts of the interval than in others.
3-23
csaps
Examples Example 1.
x = linspace(0,2*pi,21); y = sin(x)+(rand(1,21)-.5)*.1;
pp = csaps(x,y, .4, [], [ones(1,10), repmat(5,1,10), 0] );
returns a smooth fit to the (noisy) data that is much closer to the data in the
right half, because of the much larger error weight there, except for the last
data point, for which the weight is zero.
pp1 = csaps(x,y, [.4,ones(1,10),repmat(.2,1,10)], [], ...
[ones(1,10), repmat(5,1,10), 0]);
uses the same data, smoothing parameter, and error weight but chooses the
roughness weight to be only .2 in the right half of the interval and gives,
correspondingly, a rougher but better fit there, -- except for the last data point
which is ignored.
A plot showing both examples for comparison can now be obtained by
fnplt(pp); hold on, fnplt(pp1,'r'), plot(x,y,'ok'), hold off
title(['cubic smoothing spline, with right half treated ',...
'differently:'])
xlabel(['blue: larger error weights; ', ...
'red: also smaller roughness weights'])
3-24
csaps
0.5
0.5
1.5
0 1 2 3 4 5 6 7
blue: larger error weights; red: also smaller roughness weights
Note the need to transpose the array smooth. For a somewhat smoother
approximation, use a slightly smaller value of p than the one, .9998889, used
above by csaps. The final plot is obtained by the following:
smoother = csaps(x,noisy,.996,x);
figure, surf(x{1},x{2},smoother.'), axis off
3-25
csaps
3-26
cscvn
Purpose 3cscvn
Natural or periodic interpolating cubic spline curve
points ( : ,i + 1 ) points ( : ,i ) 2
i<j
If the first and last point coincide (and there are no other repeated points), then
a periodic cubic spline curve is constructed. However, double points result in
corners.
Examples The following provides the plot of a questionable curve through some points
(marked as circles):
points=[0 1 1 0 -1 -1 0 0; 0 0 1 2 1 0 -1 -2];
fnplt(cscvn(points)); hold on,
plot(points(1,:),points(2,:),'o'), hold off
Here is a closed curve, good for 14 February, with one double point:
c=fnplt(cscvn([0 .82 .92 0 0 -.92 -.82 0; .66 .9 0 ...
-.83 -.83 0 .9 .66])); fill(c(1,:),c(2,:),'r'), axis equal
3-27
fn2fm
Purpose 3fn2fm
Convert to specified form
Syntax g = fn2fm(f,form)
sp = fn2fm(pp,'B-',sconds)
g = fn2fm(f)
3-28
fn2fm
ith interior break. If the function in f is a tensor product, then sconds, if given,
must be a cell array.
fn2fm(f) converts a possibly old version of a form into its present version.
gives p1 identical to p0 (up to round-off in the coefficients) since the spline has
no discontinuity in any derivative across the additional breaks introduced by
fnrfn, hence conversion to B-form ignores these additional breaks, and
conversion to ppform does not retain any knot multiplicities (like the knot
multiplicities introduced, by conversion to B-form, at the endpoints of the
spline's basic interval).
Cautionary When going from B-form to ppform, any jump discontinuity at the first and last
Note knot, t(1) or t(end), will be lost since the ppform considers f to be defined
outside its basic interval by extension of the first, respectively, the last
polynomial piece. For example, while sp=spmak([0 1],1) gives the
characteristic function of the interval [0..1], pp=fn2fm(spmak([0 1],1),'pp')
is the constant polynomial, x 1.
3-29
fnbrk
Purpose 3fnbrk
Name and part(s) of a form
You only need to specify the beginning character(s) of the revelant string.
Regardless of what particular form f is in, parti can be one of the following.
3-30
fnbrk
3-31
fnbrk
m -variate function, then j must be a cell array of length m . In that case, each
entry of j must be a positive integer or else an interval, to single out a
particular polynomial piece or else to specify the basic interval in that
dimension.
fnbrk(f) returns nothing, but a description of the various parts of the form is
printed at the command line instead.
Examples If p1 and p2 contain the B-form of two splines of the same order, with the same
knot sequence, and the same target dimension, then
p1plusp2 = spmak(fnbrk(p1,'k'),fnbrk(p1,'c')+fnbrk(p2,'c'));
3-32
fnchg
Purpose 3fnchg
Change part(s) of a form
Syntax f = fnchg(f,part,value)
Description fnchg(f,part,value) returns the given function description f but with the
specified part changed to the specified value.
The string part can be (the beginning character(s) of)
The specified value for part is not checked for consistency with the rest of the
description in f in case the string part terminates with the letter z.
3-33
fncmb
Purpose 3fncmb
Arithmetic with function(s)
Syntax fn = fncmb(function,operation)
fn = fncmb(function,function)
fn = fncmb(function,matrix,function)
fn = fncmb(function,matrix,function,matrix)
fn = fncmb(function,op,function)
Description The intent is to make it easy to carry out the standard linear operations of
scaling and adding within a spline space without having to deal explicitly with
the relevant parts of the function(s) involved.
The remaining options only work for univariate functions. See Limitations for
more information.
3-34
fncmb
fncmb(f,3,g,-4) returns the linear combination, with weights 3 and -4, of the
function in f and the function in g.
fncmb(f,3,g) adds 3 times the function in f to the function in g.
3-35
fncmb
Assuming that sp describes the B-form of a spline of order <k, the output of
fn2fm(fncmb(sp,'+',ppmak(fnbrk(sp,'interv'),zeros(1,k))),'B-')
describes the B-form of the same spline, but with its order raised to k.
Algorithm The coefficients are extracted (via fnbrk) and operated on by the specified
matrix or operation (and, possibly, added), then recombined with the rest of the
function description (via ppmak, spmak,rpmak,rsmak,stmak). To be sure, when
the function is rational, the matrix is only applied to the coefficients of the
numerator. Again, if we are to translate the function values by a given vector
and the function is in ppform, then only the coefficients corresponding to
constant terms are so translated.
If there are two functions input, then they must be of the same type (see
Limitations, below) except for the following.
fncmb(f1,op,f2) returns the ppform of the function
x f1(x) op f2(x)
with op one of '+', '-', '*', and f1, f2 of arbitrary polynomial form. If, in
addition, f2 is a scalar or vector, it is taken to be the function that is constantly
equal to that scalar or vector.
Limitations fncmb only works for univariate functions, except for the case
fncmb(function,operation), i.e., when there is just one function in the input.
Further, if two functions are involved, then they must be of the same type. This
means that they must either both be in B-form or both be in ppform, and,
moreover, have the same knots or breaks, the same order, and the same target.
The only exception to this is the command of the form
fncmb(function,op,function).
3-36
fnder
Purpose 3fnder
Differentiate a function
Examples If f is in ppform, or in B-form with its last knot of sufficiently high multiplicity,
then, up to rounding errors, f and fnder(fnint(f)) are the same.
If f is in ppform and fa is the value of the function in f at the left end of its
basic interval, then, up to rounding errors, f and fnint(fnder(f),fa) are the
same, unless the function described by f has jump discontinuities.
If f contains the B-form of f , and t 1 is its left-most knot, then, up to rounding
errors, fnint(fnder(f)) contains the B-form of f-f ( t 1 ) . However, its
left-most knot will have lost one multiplicity (if it had multiplicity > 1 to begin
with). Also, its rightmost knot will have full multiplicity even if the rightmost
knot for the B-form of f in f doesnt.
Here is an illustration of this last fact. The spline in sp = spmak([0 0 1], 1)
is, on its basic interval [0..1], the straight line that is 1 at 0 and 0 at 1. Now
integrate its derivative: spdi = fnint(fnder(sp)). As you can check, the
spline in spdi has the same basic interval, but, on that interval, it agrees with
the straight line that is 0 at 0 and -1 at 1.
See the demos spalldem and ppalldem for examples.
Algorithm For differentiation of either polynomial form, the derivatives are found in the
piecewise-polynomial sense. This means that, in effect, each polynomial piece
3-37
fnder
3-38
fndir
Purpose 3fndir
Directional derivative of a function
Syntax df = fndir(f,y)
Since fndir relies on the ppform of the function in f, it does not work for
rational functions nor for functions in stform.
is the Jacobian of that function at that point (which, for this particular
scalar-valued function, is its gradient, and it is zero at the origin).
As a related example, the next statements plot the gradients of (a good
approximation to) the Franke function at a regular mesh:
xx = linspace(-.1,1.1,13); yy = linspace(0,1,11);
[x,y] = ndgrid(xx,yy); z = franke(x,y);
pp2dir = fndir(csapi({xx,yy},z),eye(2));
grads = reshape(fnval(pp2dir,[x(:) y(:)].'),...
[2,length(xx),length(yy)]);
quiver(x,y,squeeze(grads(1,:,:)),squeeze(grads(2,:,:)))
3-39
fndir
1.2
0.8
0.6
0.4
0.2
0.2
0.2 0 0.2 0.4 0.6 0.8 1 1.2
Algorithm The function in f is converted to ppform, and the directional derivative of its
polynomial pieces is computed formally and in one vector operation, and put
together again to form the ppform of the directional derivative of the function
in f.
3-40
fnint
Purpose 3fnint
Integrate a function
Algorithm For the B-form, the formula [PGS; (X.22)] for integration is used.
3-41
fnint
3-42
fnjmp
returns instead the vector [0 -24 24 -24 0], consistent with the fact that, in
contrast to the B-form, a spline in ppform does not have a discontinuity in any
of its derivatives at the endpoints of its basic interval. Note that
fnjmp(fnder(spmak(x,1),3),-x) returns the vector [12,0,0,0,12] since -x,
though theoretically equal to x, differs from x by roundoff, hence the third
derivative of the B-spline provided by spmak(x,1) does not have a jump across
-x(2),-x(3), and -x(4).
3-43
fnmin
Purpose 3fnmin
Minimum of a function in a given interval
Syntax fnmin(f)
fnmin(f,interv)
[minval,minsite] = fnmin(f,...)
Description fnmin(f) returns the minimum value of the scalar-valued univariate spline in
f on its basic interval.
Examples Example 1. We construct and plot a spline f with many local extrema, then
compute its maximum as the negative of the minimum of f . We indicate this
maximum value by adding a horizontal line to the plot at the height of the
computed maximum.
rand('seed',21);
f = spmak(1:21,rand(1,15)-.5);
fnplt(f)
maxval = -fnmin(fncmb(f,-1));
hold on, plot(fnbrk(f,'interv'),maxval([1 1])), hold off
0.3
0.2
0.1
0.1
0.2
0.3
0.4
0 5 10 15 20 25
3-44
fnmin
Algorithm fnmin first changes the basic interval of the function to the given interval, if
any. On the interval, fnmin then finds all local extrema of the function as left
and right limits at a jump and as zeros of the functions first derivative. It then
evaluates the function at these extrema and at the endpoints of the interval,
and determines the minimum over all these values.
3-45
fnplt
Purpose 3fnplt
Plot a function
Syntax fnplt(f)
fnplt(f,arg1,arg2,arg3,arg4)
points = fnplt(f)
A string that specifies a plotting symbol, such as '-.' or '*'; the default
is '-'.
A scalar to specify the linewidth; the default value is 1.
A string that starts with the letter 'j' to indicate that any jump in the
univariate function being plotted should actually appear as a jump. The
default is to fill in any jump by a (near-)vertical line.
A vector of the form [a,b], to indicate the interval over which to plot the
univariate function in f. If the function in f is m -variate, then this optional
3-46
fnplt
argument must be a cell array whose ith entry specifies the interval over
which the ith argument is to vary. In effect, for this arg, the command
fnplt(f,arg,...) has the same effect as the command
fnplt(fnbrk(f,arg),...). The default is the basic interval of f.
An empty matrix or string, to indicate use of default(s). You will find this
option handy when your particular choice depends on some other variables.
Cautionary The basic interval for f in B-form is the interval containing all the knots. This
Note means that, e.g., f is sure to vanish at the endpoints of the basic interval unless
the first and the last knot are both of full multiplicity k , with k the order of
the spline f . Failure to have such full multiplicity is particularly annoying
when f is a spline curve, since the plot of that curve as produced by fnplt is
then bound to start and finish at the origin, regardless of what the curve might
otherwise do.
Further, since B-splines are zero outside their support, any function in B-form
is zero outside the basic interval of its form. This is very much in contrast to a
function in ppform whose values outside the basic interval of the form are given
by the extension of its leftmost, respectively rightmost, polynomial piece.
3-47
fnrfn
Purpose 3fnrfn
Insert additional points into the partition of a form
Syntax g = fnrfn(f,addpts)
Description g = fnrfn(f,addpts) describes the same function as does f, but uses more
terms to do it. This is of use when the sum of two or more functions of different
forms is wanted or when the number of degrees of freedom in the form is to be
increased to make fine local changes possible. The precise action depends on
the form in f.
If the form in f is a B-form or BBform, then the entries of addpts are inserted
into the existing knot sequence, subject to the following restriction: The
multiplicity of no knot exceed the order of the spline. The equivalent B-form
with this refined knot sequence for the function given by f is returned.
If the form in f is a ppform, then the entries of addpts are inserted into the
existing break sequence, subject to the following restriction: The break
sequence be strictly increasing. The equivalent ppform with this refined break
sequence for the function in f is returned.
fnrfn does not work for functions in stform.
Examples See fncmb for the use of fnrfn to refine the knot or break sequences of two
splines to a common refinement before forming their sum.
Algorithm The standard knot insertion algorithm is used for the calculation of the B-form
coefficients for the refined knot sequence, while Horners method is used for the
calculation of the local polynomial coefficients at the additional breaks in the
refined break sequence.
3-48
fntlr
Purpose 3fntlr
Taylor coefficients or polynomial
dorder-1
T ( f, dorder, x ) := [f ( x ) ;Df ( x ) ; ;D f(x) ]
If, more generally, the function in f is d-valued with d>1 or even prod(d)>1
and/or is m-variate for some m>1, then dorder is expected to be an m-vector of
positive integers, x is expected to be a matrix with m rows, and, in that case,
the output is of size [prod(d)*prod(dorder),size(x,2)], with its j-th column
containing
i1-1 im-1
T ( f, dorder, x(:,j) ) ( i1, , im ) = D 1 D m f ( x(:,j) )
for i1=1:dorder(1), ..., im=1:dorder(m). Here, D i f is the partial derivative
of f with respect to its i th argument.
3-49
fntlr
We plot ci along with the points v(1:2,:), to verify that these are, indeed,
points on the unit circle.
fnplt(ci), hold on, plot(v(1,:),v(2,:),'o')
Next, to verify that v(3:4,j) is a vector tangent to the circle at the point
v(1:2,j), we use the MATLAB quiver command to add the corresponding
arrows to our plot:
quiver(v(1,:),v(2,:),v(3,:),v(4,:))
Finally, what about v(5:6,:)? These are second derivatives, and we add the
corresponding arrows by the following quiver command, thus finishing the
figure.
quiver(v(1,:),v(2,:),v(5,:),v(6,:)), axis equal, hold off
0.8
0.6
0.4
0.2
0.2
0.4
0.6
0.8
1 0.5 0 0.5 1
Figure 3-1: First and Second Derivative of a Rational Spline Giving a Circle
Now, our curve being a circle, you might have expected the 2nd derivative
arrows to point straight to the center of that circle, and that would have been
indeed the case if the function in ci had been using arclength as its
independent variable. Since the parameter used is not arclength, we use the
formula, given in Example: A Spline Curve on page 2-31, to compute the
curvature of the curve given by ci at these selected points. For ease of
comparison, we switch over to the variables used there and then simply use the
commands from there.
3-50
fntlr
The numerical answer is reassuring: at all the points tested, the curvature is 1
to within roundoff.
Next, we make up the coefficient array for the numerator, 1, using exactly the
same size, and put the two together into a rational spline:
wcoefs = fnbrk(w,'coef');
scoefs = zeros(size(wcoefs)); scoefs(end)=1;
runge2 = rpmak(fnbrk(w,'breaks'),[scoefs;wcoefs]);
Then we enlarge the basic interval for this rational spline, plot it and plot, on
top of it, its Taylor polynomial at (0,0) of order [3,3].
3-51
fntlr
Since we shaded the function but not the Taylor polynomial, we can easily
distinguish the two in the previous figure. We can also see that, in contrast to
the function, the Taylor polynomial fails to be rotationally symmetric. This is
due to the fact that it is a polynomial of order [3,3] rather than a polynomial of
total order 3.
To obtain the Taylor polynomial of order 3, we get the Taylor polynomial of
order [3,3], but with (0,0) the left point of its basic interval, set all its
coefficients of total order bigger than 3 equal to zero, and then reconstruct the
polynomial, and plot it, choosing a different view in order to show off the Taylor
polynomial better. Here are the commands and the resulting figure.
taylor = fntlr(runge2,[3 3],[0;0],[0 1;0 1]);
tcoef = fnbrk(taylor,'coe'); tcoef([1 2 4]) = 0;
taylor2 = fnbrk(ppmak(fnbrk(taylor,'br'),tcoef),{[-1 1],[-1 1]});
fnplt(fnbrk(runge2,{[-2 2],[-2 2]})); shading interp, hold on
fnplt(taylor2), view(-28,-26), axis off, hold off
Figure 3-3: The Function 1/(1+x^2+y^2) and Its Taylor Polynomial of Order 3
at the Origin
3-52
fnval
Purpose 3fnval
Evaluate a function
Syntax v = fnval(f,x)
v = fnval(x,f)
v = fnval(...,'l')
Description v = fnval(f,x) and v = fnval(x,f) both provide the value f ( x ) at the points
in x of the function f whose description is contained in f.
Roughly speaking, the output v is obtained by replacing each entry of x by the
value of f at that entry. This is literally true in case the function in f is
scalar-valued and univariate, and is the intent in all other cases, except that,
for a d-valued m-variate function, this means replacing m-vectors by d-vectors.
The full details are as follows.
For a univariate f :
If x is an array, of size [n1,...,ns] say, then n1 must equal m, and v has size
[d1,...,dr, n2,...,ns], with v(:,...,:, j2,...,js) the value of f at
x(:,j2,...,js), except that
(1) d1, ..., dr is ignored in case f is scalar-valued, i.e., both r and n1 are 1;
(2) MATLAB ignores any trailing singleton dimensions of x.
3-53
fnval
If f has a jump discontinuity at x, then the value f(x +) , i.e., the limit from the
right, is returned, except when x equals the right end of the basic interval of
the form; for such x, the value f(x ) , i.e., the limit from the left, is returned.
Examples The statement fnval(csapi(x,y),xx) has the same effect as the statement
csapi(x,y,xx).
Algorithm For each entry of x, the relevant break- or knot-interval is determined and the
relevant information assembled. Depending on whether f is in ppform or in
B-form, nested multiplication or the B-spline recurrence (see, e.g.,
[PGS; X.(3)]) is then used vector-fashion for the simultaneous evaluation at all
entries of x. Evaluation of a multivariate polynomial spline function takes full
advantage of the tensor product structure.
Evaluation of a rational spline follows up evaluation of the corresponding
vector-valued spline by division of all but its last component by its last
component.
Evaluation of a function in stform makes essential use of stcol, and tries to
keep the matrices involved to reasonable size.
3-54
fnzeros
Purpose 3fnzeros
Find zeros of a function in a given interval
Syntax z = fnzeros(f)
z = fnzeros(f,[a b])
Description z = fnzeros(f) provides the 2-rowed matrix z that is an ordered list of the
zeros of the continuous univariate spline in f in its basic interval.
If the endpoints agree, then the function in f is relatively small at that point.
If the endpoints agree to many significant digits, then the function changes
sign across the interval, and the interval contains a zero of the function
provided the function is continuous there.
If the endpoints are not close, then the function is zero on the entire interval.
Examples Example 1. We construct and plot a piecewise linear spline that has each of the
three kinds of zeros, use fnzeros to compute all its zeros, and then mark the
results on that graph.
sp = spmak(augknt(1:7,2),[1,0,1,-1,0,0,1]);
fnplt(sp)
z = fnzeros(sp)
nz = size(z,2);
hold on
plot(z(1,:),zeros(1,nz),'>',z(2,:),zeros(1,nz),'<'), hold off
In this simple example, even for the second kind of zero, the two endpoints
agree to all places.
3-55
fnzeros
0.8
0.6
0.4
0.2
0.2
0.4
0.6
0.8
1
1 2 3 4 5 6 7
Example 2. We generate a spline function with many extrema and locate all
that are in a certain interval by computing the zeros of the spline functions
first derivative there.
rand('seed',23)
sp = spmak(1:21,rand(1,16)-.5);
fnplt(sp)
z = mean(fnzeros(fnder(sp),[7,14]));
zy = fnval(sp,z);
hold on, plot(z,zy,'o'), hold off
0.3
0.2
0.1
0.1
0.2
0.3
0.4
0 5 10 15 20 25
3-56
fnzeros
Notice the resulting zero interval [2..2.7], due to the fact that, by definition, a
spline in B-form is identically zero outside its basic interval.
Algorithm fnzeros first converts the function to B-form. It locates zero intervals by the
corresponding sequence of consecutive zero B-spline coefficients. It locates the
sign changes in the B-spline coefficients for the function, isolates them from
each other by suitable knot insertion, and then uses the Modified Regula falsi
to locate the corresponding sign changes in the function, if any.
3-57
getcurve
Purpose 3getcurve
Interactive creation of a cubic spline curve
Description getcurve displays a gridded window and asks you for input. As you click on
points in the gridded window, the broken line connecting these points is
displayed. To indicate that you are done, click outside the gridded window.
Then a cubic spline curve, spcv, through the point sequence, xy, is computed
(via cscvn) and drawn. The point sequence and, optionally, the spline curve are
output.
If you want a closed curve, place the last point close to the initial point.
If you would like the curve to have a corner at some point, click on that point
twice (or more times) in succession.
3-58
knt2brk, knt2mlt
Syntax knt2brk(knots)
[breaks,mults] = knt2brk(knots)
m = knt2mlt(t)
[m,sortedt] = knt2mlt(t)
Description The commands extract the distinct elements from a sequence, as well as their
multiplicities in that sequence, with multiplicity taken in two slightly different
senses.
Neither knt2brk nor knt2mlt is likely to be used by the casual user of this
toolbox.
3-59
knt2brk, knt2mlt
3-60
newknt
Purpose 3newknt
New break distribution
Description newknt(f,newl) returns the knot sequence whose interior knots cut the basic
interval of f into newl pieces, in such a way that a certain piecewise linear
monotone function related to the high derivative of f is equidistributed.
newknt(f) uses for newl its default value, namely the number of polynomial
pieces in f.
Examples If the error in the least-squares approximation sp to some data x,y by a spline
of order k seems uneven, you might try for a more equitable distribution of
knots by using
spap2(newknt(sp),k,x,y);
For another example, see the last part of the demo difeqdem.
Algorithm This is the Fortran routine NEWNOT in PGS. With k the order of the
k
piecewise-polynomial function f in pp, the function D f is approximated by
a piecewise constant function obtained by local, discrete, differentiation of the
k-1
variation of D f . The new break sequence is chosen to subdivide the basic
interval of the piecewise-polynomial f in such a way that
newknots ( i + 1 ) k 1k
newknots ( i ) D f = const , i = k:k + newl 1
3-61
optknt
Purpose 3optknt
Knot distribution optimal for interpolation
Here, f : = sup tau ( 1 ) < x < tau ( n ) f ( x ) . Then we may look for the optimal
recovery scheme as the scheme R for which const R is as small as possible.
Micchelli/Rivlin/Winograd have shown this to be interpolation from S k, t ,
with t uniquely determined by the following conditions:
Examples See the last part of the demo spapidem for an illustration. For the following
highly nonuniform knot sequence
t = [0, .0012+[0, 1, 2+[0,.1], 4]*1e-5, .002, 1];
3-62
optknt
3-63
ppmak
Purpose 3ppmak
Put together a spline in ppform
Syntax ppmak(breaks,coefs)
ppmak
pp = ppmak(breaks,coefs,d)
pp = ppmak(breaks,coefs,sizec)
Description The command ppmak(...) puts together a spline in ppform from minimal
information, with the rest inferred from that information. fnbrk provides any
or all of the parts of the completed description. In this way, the actual data
structure used for the storage of the ppform is easily modified without any
effect on the various fn... commands that use this construct. However, the
casual user is not likely to use ppmak explicitly, relying instead on the various
spline construction commands in the toolbox to construct particular splines.
3-64
ppmak
m
( k( ) r( ) )
( x( ) breaks{ } ( i( ) ) )
=1
3-65
ppmak
have exactly the same ppform (2-vector-valued, of order 2). But the second
command provides the coefficients in the arrangement used internally.
ppmak([0:2],[1:6]) constructs a piecewise-polynomial function with basic
interval [0..2] and consisting of two pieces of order 3, with the sole interior
break 1. The resulting function is scalar, i.e., the dimension d of its target is 1.
The function happens to be continuous at that break since the first piece is
2 2
x x + 2x + 3 , while the second piece is x 4(x 1) + 5 ( x 1 ) + 6 .
When the function is univariate and the dimension d is not explicitly specified,
then it is taken to be the row number of coefs. The column number should be
an integer multiple of the number l of pieces specified by breaks. For example,
the statement ppmak([0:2],[1:3;4:6]) leads to an error, since the break
sequence [0:2] indicates two polynomial pieces, hence an even number of
columns are expected in the coefficient matrix. The modified statement
ppmak([0:1],[1:3;4:6]) specifies the parabolic curve
2
x ( 1, 4 )x + ( 2, 5 )x + ( 3, 6 ) . In particular, the dimension d of its target is 2.
The differently modified statement ppmak([0:2],[1:4;5:8]) also specifies a
3-66
ppmak
planar curve (i.e., d is 2), but this one is piecewise linear; its first polynomial
piece is x ( 1, 5 )x + ( 2, 6 ) .
Explicit specification of the dimension d leads, in the univariate case, to a
different interpretation of the entries of coefs. Now the column number
indicates the polynomial order of the pieces, and the row number should equal
d times the number of pieces. Thus, the statement ppmak([0:2],[1:4;5:8],2)
is in error, while the statement ppmak([0:2],[1:4;5:8],1) specifies a scalar
3 2
piecewise cubic whose first piece is x x + 2x + 3x + 4 .
If you wanted to make up the constant polynomial, with basic interval [0..1]
say, whose value is the matrix eye(2), then you would have to use the full
optional third argument, i.e., use the command
pp = ppmak(0:1,eye(2),[2,2,1,1]);
3-67
rpmak, rsmak
Syntax rp = rpmak(breaks,coefs)
rp = rpmak(breaks,coefs,d)
rs = rsmak(knots,coefs)
rs = rsmak(shape,parameters)
Description Both rpmak and rsmak put together a rational spline from minimal information.
rsmak is also equipped to provide rational splines that describe standard
geometric shapes. A rational spline must be scalar- or vector-valued.
3-68
rpmak, rsmak
a rBform. See spmak for how the input arrays knots and coefs are being
interpreted, hence how they are to be specified in order to produce a particular
piecewise-polynomial.
rsmak('circle',radius,center)
rsmak('cone',radius,halfheight)
rsmak('cylinder',radius,height)
rsmak('southcap',radius,center)
From these, one may generate related shapes by affine transformations, with
the help of fncmb.
All fn... commands except fnint, fnder, fndir can handle rational splines.
3-69
rpmak, rsmak
For further, illustrated examples, see NURBS and Other Rational Splines on
page 2-38.
3-70
slvblk
Purpose 3slvblk
Solve an almost block-diagonal linear system
Syntax x = slvblk(blokmat,b)
x = slvblk(blokmat,b,w)
Description slvblk(blokmat,b) returns the solution (if any) of the linear system Ax = b,
with the matrix A stored in blokmat in the spline almost block-diagonal form.
At present, only the command spcol provides such a description, of the matrix
whose typical entry is the value of some derivative (including the 0th
derivative, i.e., the value) of a B-spline at some site. If the linear system is
overdetermined (i.e., has more equations than unknowns but is of full rank),
then the least-squares solution is returned.
The right side b may contain several columns, and is expected to contain as
many rows as there are rows in the matrix described by blokmat.
Algorithm The command bkbrk is used to obtain the essential parts of the coefficient
matrix described by blokmat (in one of two available forms).
A QR factorization is made of each diagonal block, after it was augmented by
the equations not dealt with when factoring the preceding block. The resulting
factorization is then used to solve the linear system by backsubstitution.
3-71
sorted
Purpose 3sorted
Locate sites with respect to mesh sites
Description Various commands in this toolbox need to determine the index j for which a
given x lies in the interval [ t j ..t j + 1 ] , with ( t i ) a given nondecreasing
sequence, e.g., a knot sequence. This job is done by sorted in the following
fashion.
3-72
spap2
Purpose 3spap2
Least-squares spline approximation
Syntax sp = spap2(knots,k,x,y)
sp = spap2(l,k,x,y)
sp = spap2(knots,k,x,y,w)
Description spap2(knots,k,x,y) returns the B-form of the spline f of order k with the
given knot sequence knots for which
(*) y(:,j) = f(x(j)), all j
spap2(...,x,y,w) lets you specify the weights w in the error measure (given
above). w must be a vector of the same size as x, with nonnegative entries. All
the weights corresponding to data points with the same site are summed when
those data points are replaced by their average.
3-73
spap2
Examples sp = spap2(augknt([a,xi,b],4),4,x,y)
If the resulting approximation is not satisfactory, try using a larger l. Else use
sp = spap2(newknt(sp),4,x,y);
for a possibly better distribution of the knot sequence. In fact, if that helps,
repeating it may help even more.
As another example, spap2(1, 2, x, y); provides the least-squares
straight-line fit to data x,y, while
w = ones(size(x)); w([1 end]) = 100; spap2(1,2, x,y,w);
forces that fit to come very close to the first data point and to the last.
3-74
spap2
3-75
spapi
Purpose 3spapi
Spline interpolation
k = length(knots) - length(x)
If some of the entries of x are the same, then this is taken in the osculatory
m(j)
sense, i.e., in the sense that D f ( x(j) ) = y(:,j) , with m(j) := #{ i<j : x(i)
m
= x(j) }, and D f the m -th derivative of f . Thus r -fold repetition of a site z
in x corresponds to the prescribing of value and the first r 1 derivatives of f
at z .
The data values, y(:,j), may be scalars, vectors, matrices, or even ND-arrays.
3-76
spapi
2
f(0+) = 2 , f ( 1 ) = 0 , Df(1) = 1 , D f(1) = 2 , f(2) = 1
These include 3-fold matching at 1, i.e., matching there to prescribed values of
the function and its first two derivatives.
Here is an example of osculatory interpolation, to values y and slopes s at the
sites x by a quintic spline:
sp = spapi(augknt(x,6,2),[x,x,min(x),max(x))],[y,s,ddy0,ddy1]);
with ddy0 and ddy1 values for the second derivative at the endpoints.
As a related example, if the function sin(x) is to be interpolated at the distinct
data sites x by a cubic spline, and its slope is also to be matched at a
subsequence x(s), then this can be accomplished by the command
sp = spapi(4,[x x(s)], [sin(x) cos(x(s))]);
in which a suitable knot sequence is supplied with the aid of aptknt. In fact, if
you wanted to interpolate the same data by quintic splines, simply change the
4 to 6.
3-77
spapi
Limitations The given (univariate) knots and sites must satisfy the Schoenberg-Whitney
conditions for the interpolant to be defined. Assuming the site sequence x to be
nondecreasing, this means that we must have
3-78
spaps
Purpose 3spaps
Smoothing spline
Syntax sp = spaps(x,y,tol)
[sp,values] = spaps(x,y,tol)
[sp,values,rho] = spaps(x,y,tol)
[...] = spaps(x,y,tol,arg1,arg2,...)
[...] = spaps({x1,...,xr},y,tol,...)
Description spaps(x,y,tol) returns the B-form of the smoothest function f that lies
within the given tolerance tol of the given data points (x(j), y(:,j)),
j=1:length(x). The data values y(:,j) may be scalars, vectors, matrices,
even ND-arrays. Data points with the same data site are replaced by their
weighted average, with its weight the sum of the corresponding weights, and
the tolerance tol is reduced accordingly.
Here, the distance of the function f from the given data is measured by
n
w ( j ) ( y ( :, j ) f ( x ( j ) ) )
2
E(f) =
j=1
with the default choice for the weights w making E ( f ) the composite
max(x) 2 2
trapezoidal rule approximation to y f , and z denoting the sum
min(x)
of squares of the entries of z .
Further, smoothest means that the following roughness measure is minimized:
max(x)
m m 2
F(D f ) = ( t ) D f ( t ) dt
min(x)
m
where D f denotes the mth derivative of f . The default value for m is 2, the
default value for the roughness measure weight is the constant 1, and this
makes f a cubic smoothing spline.
When tol is nonnegative, then the spline f is determined as the unique
m
minimizer of the expression E ( f ) + F ( D f ) , with the smoothing parameter
(optionally returned) so chosen that E ( f ) equals tol. Hence, when m is 2,
3-79
spaps
then, after conversion to ppform, the result should be the same (up to roundoff)
as obtained by csaps(x, y, ( + 1 )) . Further, when tol is zero, then the
natural or variational spline interpolant of order 2m is returned. For large
enough tol, the least-squares approximation to the data by polynomials of
degree <m is returned.
When tol is negative, then is taken to be -tol.
The default value for the weight function in the roughness measure is the
constant function 1. But you may choose it to be, more generally, a piecewise
constant function, with breaks only at the data sites. Assuming the vector x to
be strictly increasing, you specify such a piecewise constant by inputting tol
as a vector of the same size as x. In that case, tol(i) is taken as the constant
value of on the interval (x(i-1) .. x(i)), i=2:length(x), while tol(1)
continues to be used as the specified tolerance.
3-80
spaps
give a quintic smoothing spline approximation to the given data that close to
interpolates the first and last datum, while being within about 1.e-2 of the
rest.
x = -2:.2:2; y=-1:.25:1; [xx,yy] = ndgrid(x,y); rand('seed',39);
z = exp(-(xx.^2+yy.^2)) + (rand(size(xx))-.5)/30;
sp = spaps({x,y},z,8/(60^2)); fnplt(sp), axis off
produces the figure below, showing a smooth approximant to noisy data from a
smooth bivariate function. Note the use of ndgrid here; use of meshgrid would
have led to an error.
Algorithm Reinschs approach [1] is used (including his clever way of choosing the
equation for the optimal smoothing parameter in such a way that a good initial
guess is available and Newtons method is guaranteed to converge and to
converge fast).
References [1] C. Reinsch, Smoothing by spline functions, Numer. Math. 10 (1967), 177
183.
3-81