Lect 6 C
Lect 6 C
1
1.1
A spline function is a function that consists of polynomial pieces joined together with
certain smoothness conditions. For example, the polygonal function is a spline of degree
1 which consists of linear polynomials joined together to achieve continuity. The points
t0 ,t1 , . . . ,tn are called knots.
S0
S5
S1
S2
S4
S6
S3
S7
a=t0
t1
t2 t3
t4
t5
t6 t7
S0 (x)
x [t0 ,t1 ]
S1 (x)
x [t1 ,t2 ]
S(x) = .
..
S (x)
x [t ,t ]
n1 n
n1
t8=b
Definition.
A function S is called a spline of degree 1 if:
1. The domain of S is an interval [a, b].
2. S is continuous on [a, b].
3. There is a partitioning of the interval a = t0 < t1 < . . . < tn = b such that S is a linear
polynomial on each subinterval [ti ,ti+1 ].
x [1, 0]
x
S(x) = 1 x
x (0, 1)
2x 2
x [1, 2]
is not a spline because it is discontinuous at x = 0.
2
1.5
S(x)
1
0.5
0
0.5
1
1
0.5
0.5
x
1.5
xs+
xs
In other words, this means that the values of f must converge to the same limiting value
f (s) (which is the value of the function at s) from both above and below the point s.
Spline functions of degree 1 can be used for interpolation. Suppose that we have the
following table of values:
x t0
y y0
t1
y1
. . . tn
. . . yn
This can be represented by n + 1 points in the xy-plane, and we can draw a polygonal line
(a spline of degree 1) through the points.
The equation for the line segment on the interval [ti ,ti+1 ] is given by
Si (x) = yi + mi (x ti ) = yi +
yi+1 yi
(x ti )
ti+1 ti
1.2
Modulus of continuity
xa
bx
[ f (x) f (b)] +
[ f (x) f (a)]
ba
ba
Then we have
xa
bx
| f (x) p(x)|
| f (x) f (b)| +
| f (x) f (a)|
ba
ba
xa
bx
( f ; h) +
( f ; h)
ba
ba
xa
bx
=
+
( f ; h)
ba
ba
= ( f ; h)
This tells us that if more knots are inserted in such a way that the maximum spacing h
goes to zero, then the corresponding first-degree spline will converge uniformly to f .
Note that this type of a result does not exist in the polynomial interpolation theory! There
increasing the number of nodes may even lead to larger errors!
1.3
Second-degree splines
x0
x
Q(x) = x2
0x1
1 2x
x1
The function is obviously piecewise quadratic. We can determine whether Q and Q0 are
continuous by considering all the knot points separately:
lim Q(x) = lim x2 = 0
x0
x0
x0+
x1
x1
x1+
x0
x0+
0
x1
x1+
x0
x0+
x0+
x1
x1+
x1+
1
0.5
0
Q(x)
0.5
1
1.5
2
2.5
3
1
0.5
0.5
x
1.5
1.4
Quadratic splines are not used in applications as often as natural cubic splines, but understanding the simpler second-degree spline theory will help in grasping the more common
third-degree splines.
Consider an interpolation problem with the following table of values:
x t0
y y0
t1
y1
. . . tn
. . . yn
Think of the nodes of the interpolation problem as being also the knots for the spline
function to be constructed. A quadratic spline consists of n separate quadratic functions,
Qi (x) = ai x2 + bi x + ci . Thus we have 3n coefficients to determine.
The following conditions are imposed in order to determine the unknown coefficients:
(i) On each subinterval [ti ,ti+1 ], the quadratic spline function Qi must satisfy the interpolation conditions: Qi (ti ) = yi and Qi (ti+1 ) = yi+1 . This imposes 2n conditions.
(ii) The continuity of Q does not impose any further conditions. But the continuity of Q0
at each of the interior points gives n 1 conditions.
(iii) We are now one condition short, since we have a total of 3n 1 conditions from (i)
and (ii). The last condition can be applied in different ways; e.g. Q0 (t0 ) = 0 or Q000 = 0.
Derivation of the quadratic interpolating spline
We seek for a piecewise quadratic function
Q0 (x)
Q1 (x)
Q(x) = .
..
Q (x)
x [t0 ,t1 ]
x [t1 ,t2 ]
x [tn1 ,tn ]
n1
which is continuously differentiable on the entire interval [t0 ,tn ] and which interpolates
the table:
0in
Q(ti ) = yi
zi+1 zi
(x ti )2 + zi (x ti ) + yi
2(ti+1 ti )
You can verify that: Qi (ti ) = yi , Q0i (ti ) = zi and Q0i (ti+1 ) = zi+1 .
In order for Q to be continuous and to interpolate the data table, it is necessary and sufficient that Qi (ti+1 ) = yi+1 for i = 0, 1, . . . , n 1. This gives us the following:
yi+1 yi
(0 i n 1)
zi+1 = zi + 2
ti+1 ti
This equation can be used to obtain the vector [z0 , z1 , . . . , zn ]T , starting with an arbitrary
value for z0 = Q0 (t0 ).
Algorithm.
(i.) Select z0 arbitrarily and compute z1 , z2 , . . . , zn using the recursion formula above.
(ii.) The quadratic spline interpolating function Q can now be constructed using the table
values for yi and ti together with the obtained values for zi .
2.1
First- and second-degree splines are not so useful for actual applications, because their
low-order derivatives are discontinuous.
For first-degree splines, the slope of the spline may change abruptly at the knots.
For second-degree splines, the discontinuity is in the second derivative which means
that the curvature of the quadratic spline changes abruptly at each node.
First-degree spline
Second-degree spline
1
0.5
1.5
Q(x)
S(x)
0.5
1
1.5
0.5
2
2.5
0.5
0.5
x
1.5
0.5
0.5
x
1.5
Higher-degree splines are useful whenever more smoothness is needed in the approximating function.
Definition.
A function S is called a spline of degree k if:
1. The domain of S is an interval [a, b].
2. S, S0 , S00 , . . . ,S(k1) are all continuous on [a, b].
3. There are points ti (the knots of S) such that a = t0 < t1 < . . . < tn = b and such that
S is a polynomial of degree at most k on each subinterval [ti ,ti+1 ].
2.2
Splines of degree 3 are called cubic splines. In this case, the spline function has two
continuous derivatives which makes the graph of the function appear smooth.
We next turn to interpolating a table of given values using a cubic spline whose knots
coincide with the x values in the table.
x t0
y y0
. . . tn
. . . yn
t1
y1
The ti s are the knots and are assumed to be arranged in ascending order. The function S,
which we are constructing, consists of n cubic polynomial pieces:
S0 (x)
x [t0 ,t1 ]
S1 (x)
x [t1 ,t2 ]
S(x) = .
..
S (x)
x [t ,t ]
n1 n
n1
Here Si denotes the cubic polynomial that will be used on the subinterval [ti ,ti+1 ].
The interpolation conditions are
Si (ti ) = yi
(0 i n)
The continuity conditions are imposed only at the interior knots t1 ,t2 , . . . ,tn1 :
lim S(k) (ti ) = lim S(k) (ti ) (k = 0, 1, 2)
xti
xti+
Two extra conditions are needed in order to use all the degrees of freedom available. We
can choose:
S00 (t0 ) = S00 (tn ) = 0
The resulting spline function is termed a natural cubic spline.
Example.
Determine the parameters a, b, c, d, e, f , g and h so that S(x) is a natural cubic spline, where
(
ax3 + bx2 + cx + d x [1, 0]
S(x) =
ex3 + f x2 + gx + h x [0, 1]
with interpolation conditions S(1) = 1, S(0) = 2 and S(1) = 1.
Solution.
Let the two cubic polynomials be S0 (x) and S1 (x).
From the interpolation conditions we have
S0 (0) = d = 2
S1 (0) = h = 2
S0 (1) = a + b c = 1
S1 (1) = e + f + g = 3
The first derivative of S(x) is
(
3ax2 + 2bx + c
S0 (x) =
3ex2 + 2 f x + g
From the continuity condition of S0 we get
S0 (0) = c = g
The second derivative is given by
(
6ax + 2b
S00 (x) =
6ex + 2 f
From the continuity of S00 we get
S00 (0) = b = f
Two extra conditions are
S00 (1) = 6a + 2b = 0
and
S00 (1) = 6e + 2 f = 0
From all these equations, we obtain
a = 1, b = 3, c = 1, d = 2, e = 1, f = 3 and h = 2.
10
2.5
2
1.5
S(x)
1
0.5
0
0.5
1
1
2.3
0.5
0
x
0.5
We will now develop a systematic procedure for determining the formula for a natural
cubic spline, when the table of interpolation values is given.
From the continuity of S00 , we can unambiguously define the following numbers
zi S00 (ti ) (0 i n)
From the two extra conditions, we have z0 = zn = 0. The other values zi are not yet known.
We know that, on each subinterval [ti ,ti+1 ], S00 is a linear polynomial that takes the values
zi and zi+1 at the endpoints. Thus
Si00 (x) =
zi
zi+1
(x ti ) + (ti+1 x)
hi
hi
zi+1
zi
(x ti )3 +
(ti+1 x)3 + cx + d
6hi
6hi
zi+1
zi
(x ti )3 +
(ti+1 x)3 +Ci (x ti ) + Di (ti+1 x)
6hi
6hi
Si (x) =
11
When the values z0 , z1 , . . . , zn have been determined, the spline function S(x) is obtained
piece by piece from this equation.
We now determine the zi s. We use the remaining condition - namely the continuity of S0 .
0 (t ) = S0 (t ). We have
At the interior knots ti for 1 i n 1, we must have Si1
i
i i
Si0 (x) =
zi+1
zi
yi+1 hi
yi hi
(x ti )2
(ti+1 x)2 +
zi+1 + zi
2hi
2hi
hi
6
hi 6
This gives
hi
hi
Si0 (ti ) = zi+1 zi + bi ,
6
3
where
bi =
1
(yi+1 yi )
hi
and
hi = ti+1 ti .
Analogously, we have
0
Si1
(ti ) =
hi1
hi1
zi1
zi + bi1
6
3
z0
hi1 zi1 + ui zi + hi zi+1
zn
=0
= vi
=0
(1 i n 1)
z0
0
1 0
z1 v1
h0 u1 h1
z2 v2
h1 u2 h2
.. = ..
... ... ...
. .
zn1 vn1
0 1
zn
0
12
u1 h1
z1
h1 u2 h2
z2
..
... ...
...
v1
v2
..
.
=
vn2
vn1
h2i1
ui ui ui1
v v hi1 vi1 (i = 2, 3, . . . , n 1)
i
i
ui1
The back substitution yields
(
v
zni un1
n1
vi hi zi+1
zi
ui
(i = n 2, n 3, . . . , 1)
13
Algorithm
Given the interpolation points (ti , yi ) for i = 0, 1, . . . , n:
1. Compute for i = 0, 1, . . . , n 1
(
hi = ti+1 ti
bi = h1i (yi+1 yi )
2. Set
(
u1 = 2(h0 + h1 )
v1 = 6(b1 b0 )
h2
3. Set
(
zn = 0
z0 = 0
vi hi zi+1
ui
This algorithm could potentially fail because of divisions by zero in steps 2 and 3. Therefore, let us prove that ui 6= 0 for all i.
It is clear that u1 > h1 > 0. If ui1 > hi1 , then ui > hi because
ui = 2(hi + hi1 )
h2i1
> 2(hi + hi1 ) hi1 > hi
ui1
14
Evaluation of Si (x)
The following form for the cubic polynomial Si
zi
zi+1
(x ti )3 +
(ti+1 x)3
6hi
6hi
yi+1 hi
yi hi
+
zi+1 (x ti ) +
zi (ti+1 x)
hi
6
hi 6
Si (x) =
Bi = Si0 (ti )
1
Ci = Si00 (ti )
2
1
Di = Si000 (ti )
6
15
Implementation
The following procedure solves the (n + 1) (n + 1) tridiagonal system. The result is
stored in the array (zi ).
void Spline3_Coeff(int n,double *t,double *y,double *z)
{
int i;
double *h, *b, *u, *v;
h
b
u
v
=
=
=
=
(double
(double
(double
(double
*)
*)
*)
*)
malloc((size_t)
malloc((size_t)
malloc((size_t)
malloc((size_t)
(n*sizeof(double)));
(n*sizeof(double)));
(n*sizeof(double)));
(n*sizeof(double)));
16
The following procedure evaluates the natural cubic spline S(x) for a given value of x.
The nested form is used in the evaluation of the Si s.
double Spline3_Eval(int n, double *t,
double *y, double *z, double x)
{
int i;
double h, tmp, result;
for(i=n-1; i>=0; i--) {
if(x-t[i]>=0)
break;
}
h = t[i+1]-t[i];
tmp = 0.5*z[i] + (x-t[i])*(z[i+1]-z[i])/(6.0*h);
tmp = -(h/6.0)*(z[i+1]+2.0*z[i])+(y[i+1]-y[i])/h + (x-t[i])*tmp;
result = y[i] + (x-t[i])*tmp;
return(result);
}
17
Example
As an example, the natural cubic spline routines were implemented in a program which
determines the natural cubic spline interpolant for sin x at ten equidistant knots in the
interval [0, 1.6875]. The spline function is evaluated at 37 equally spaced points in the
same interval. The figure below shows the spline function and the ten equidistant knots.
knots
spline
1
0.8
0.6
0.4
0.2
0
0
0.5
1.5
The figure below shows the error |S(x)sin(x)|, and for comparison, also the error |p(x)
sin(x)| obtained for the Newton form of the interpolating polynomial (Chapter 3). We note
that in this case the spline interpolant is not as accurate as the polynomial!
4
x 10
spline
polynomial
16
14
error
12
10
8
6
4
2
0
0
0.5
1
x
18
1.5
Example 2
Here is another example which illustrates the differences between polynomial interpolation and cubic spline interpolation. Consider the serpentine curve given by
y=
x
1/4 + x2
In order to have non-uniformly spaced knots, we write the curve in parametric form:
(
x = 12 tan
y = sin 2
and take = i(/12), where i = 5, . . . , 5.
If we use the parametric representation to generate the knots {x(), y()}, the order of the
knots must be rearranged so that the array (ti ) runs from the smallest value to the largest.
The values in array (yi ) are also rearranged to correspond to the ordering of (ti ). After
this, we can use the procedures Spline3_Coeff and Spline3_Eval to determine
the cubic spline interpolant of the serpentine curve.
The figure below shows the 13 knots, the polynomial interpolant and the cubic spline interpolant of the serpentine curve. Notice how the polynomial becomes wildly oscillatory,
whereas the spline is an excellent fit.
6
knots
polynomial
spline
4
2
0
2
4
6
19
2.4
Smoothness property
The previous example illustrates why spline functions are better for data fitting than ordinary polynomials. Interpolation by high-degree polynomials is often unsatisfactory because polynomials exhibit oscillations.
Wild oscillations in a function can be attributed to its derivatives being very large. For
example, for the curve in the figure below, f 0 (x) is first large and positive and soon after
large and negative. Consequently, there is a point where f 00 (x) is large (since the value of
f 0 changes rapidly).
1500
1000
f(x) > 0
f(x) < 0
500
0
1
1.2
1.4
1.6
1.8
2.2
Spline functions do not exhibit such oscillatory behavior. In fact, from a certain point of
view, spline functions are the optimal functions for curve fitting.
00
[S (x)] dx
Z b
[ f 00 (x)]2 dx
This theorem states that the average value of [S00 (x)]2 on the interval [a, b] is never larger
than the average value of [ f 00 (x)]2 on the same interval. Since [ f 00 (x)]2 is related to the curvature of f , we know that the spline interpolant will not oscillate more than the function
f itself does.
20
B splines
B splines are special spline functions that are well suited for numerical tasks. B splines
are often used in software packages for approximating data, and therefore, familiarity
with these functions is useful for anyone using such library codes. The name of B splines
comes from the fact that they form a basis for the set of all splines.
Here we assume that an infinite set of knots {ti } has been prescribed in such a way that
(
. . . < t2 < t1 < t0 < t1 < t2 < . . .
limi ti = = limi ti
The B splines depend on this set of knots, although the notation does not show the dependence. The B splines of degree 0 are defined by
(
1 ti x < ti+1
0
Bi (x) =
0 otherwise
t(i)
t(i+1)
Obviously B0i is discontinuous, but it is continuous from the right, even at the points where
the jump occurs:
lim B0i (x) = 1 = B0i (ti ) and
xti+
+
xti+1
The support of a function f is defined to be the set of points where f (x) 6= 0. Thus the
support of B0i is the half-open interval [ti ,ti+1 ). Two further observations can be made:
(
B0i (x)
0 for all x and for all i
0
if ti x < ti+1
S=
(i = 0, 1, 2, . . .)
bi B0i (x)
i=
21
x ti+2
x ti
t
ti
i+1
ti+2 x
ti+2 ti+1
or x ti
ti x < ti+1
ti+1 x < ti+2
t(i)
t(i+1)
t(i+2)
The splines B1i are sometimes called hat functions or chapeau functions due to their triangular shape. In general, the functions Bki (x) are called B splines of degree k. Since these
functions are defined recursively, the degree of the functions increases by 1 at each step.
Therefore, B1i (x) is piecewise linear, B2i (x) is piecewise quadratic, and so on.
It can be shown that
i.
Bki (x) = 0
x
/ [ti ,ti+k+1 )
ii.
x (ti ,ti+k+1 )
and that
The principal use of the B splines Bki is as the basis for the set of all kth degree splines
that have the same knot sequence. Our first task is to develop an efficient method for
evaluating a function of the form
f (x) =
i=
x ti
ti+k+1 x
k1
k1
k
Bi+1 (x)
Bi (x) +
f (x) = Ci
ti+k ti
ti+k+1 ti+1
i=
x ti
ti+k x
k
k
= Ci
+Ci1
Bik1 (x)
t
t
t
t
i
i
i+k
i+k
i=
(x)
Cik1 Bk1
i
i=
where Cik1 is defined to be the appropriate coefficient from the second line of the equation
above.
This algebraic manipulation shows how a linear combination of Bki (x) can be expressed
(x). Repeating the process k 1 times, gives
as a linear combination of Bk1
i
f (x) =
i=
j1
f (x) =
i=
i=mk
k
k
The coefficients Cmk ,Cm1
, . . . ,Cmk
can be calculated from the equation (*) by forming
the following triangular array:
Cmk
Cm0
Cmk1
...
...
k1
k
Cm1
Cm1
...
1
Cm1
..
.
...
k
Cmk
f (x) =
Bki (x) = 1
i=
We already know this for k = 0. For k > 0, we set Cik = 1 for all i. We can show (by
induction) that all the subsequent coefficients Cik ,Cik1 , . . . ,Ci0 are also equal to 1. Thus,
at the end,
f (x) =
i=
B0i (x) = 1
i=
d
k
ci Bi (x) = di Bk1
(x)
i
dx i=
i=
where
ci ci1
di = k
ti+k ti
B splines are also recommended for numerical integration. Here is a basic result:
Z x
ti+k+1 ti+1 k+1
k
Bi (s)ds =
B j (x)
k+1
j=1
This can be verified by differentiating both sides with respect to x.
This leads to the following useful formula
Z x
i=
ci Bki (s)ds =
ei Bik+1 (x)
i=
where
ei =
1
k+1
c j (t j+k+1 t j )
j=
This formula gives an indefinite integral of any function expressed as a linear combination
of B splines. Any definite integral can be obtained by selecting a specific value for x.
For example, if x is a knot, say x = tm , then
Z tm
i=
ci Bki (s)ds =
ei Bik+1 (tm ) =
i=
ei Bik+1 (tm )
i=mk1
Matlab has a Spline toolbox which can be used for many tasks involving splines. For
example, there are routines for interpolating data by splines and routines for least-squares
fits to data. There are also many demonstrations. Use the Matlab help command to learn
more about the properties.
24
S(x) =
Ai Bkik (x)
i=
. . . tn
. . . yn
t1
y1
(0 i n)
B splines of degree 0
We begin with the simplest splines, corresponding to k = 0:
(
1
i= j
B0i (t j ) = i j =
0
i 6= j
Here the solution is obvious: we set Ai = yi for 0 i n. All other coefficients are
arbitrary.
Thus the zero-degree B spline
n
0
x ti+2 or x ti
xt
ti < x < ti+1
= ti+1 iti
ti+2 x
ti+1 x < ti+2
ti+2 ti+1
25
B1
t1
B0
t0
B1
t1
t2
B2
t3
t4
S(x) =
Ai B2i2 (x)
i=
S(t j ) =
i=
Ai B2i2 (t j ) =
1
[A j (t j+1 t j ) + A j+1 (t j t j1 )]
t j+1 t j1
Imposing the interpolation conditions S(t j ) = y j , we obtain the following system of equations:
A j (t j+1 t j ) + A j+1 (t j t j1 ) = y j (t j+1 t j1 )
(0 j n)
This is a system of n + 1 linear equations in n + 2 unknowns A0 , A1 , . . . , An+1 . This gives
us the necessary and sufficient conditions for the coefficients.
26
One way to solve the system of equations is to assign any value to A0 and then compute
A1 , A2 , . . . , An+1 recursively. For this purpose, the equations could be rewritten in the
following form
A j+1 = j + j A j (0 j n)
where (for 0 j n)
j+1 t j1
t j t j1
= yj
t t
= t j t j+1
t
j1
In order to keep the coefficients small in magnitude, A0 should be selected such that the
following expression will be a minimum:
n+1
A2i
i=0
(0 j n)
where the coefficients j and j are obtained recursively by the following algorithm (for
1 j n):
(
0 = 0
0 = 0
j = j + j j1 j = j j1
27
S(x) =
Ai B2i2 (x)
i=
at a given point x which lies in between the nodes t j1 and t j , then we can calculate S(x)
using the following equations
S(x) =
1
[d(x t j1 ) + e(t j x)]
t j t j1
1
[A j+1 (x t j1 ) + A j (t j+1 x)]
t j+1 t j1
and
e=
1
[A j (x t j2 ) + A j1 (t j x)]
t j t j2
We are now ready to construct a program which calculates a quadratic spline interpolant
for a given set of data points.
28
Implementation
Two new routines are needed for the implementation of a program that determines a
quadratic B spline interpolant for a given set of data points.
First, the following procedure, BSpline2_Coef, computes the coefficients A0 , A1 , . . . , An+1
in the manner described before. ti and yi are the prescribed data points, ai is the coefficient
array, and hi contains the subinterval lengths hi = ti ti1 .
void BSpline2_Coeff(int n, double *t, double *y,
double *a, double *h)
{
int i;
double delta, gamma, p, q, r;
for(i=1; i<=n; i++){
h[i] = t[i]-t[i-1];
}
h[0] = h[1];
h[n+1] = h[n];
/* Determine A0 */
delta = -1.0;
gamma = 2.0 * y[0];
p = delta*gamma;
q = 2.0;
for(i=1; i<=n; i++){
r = h[i+1]/h[i];
delta = -r*delta;
gamma = -r*gamma + (r+1.0)*y[i];
p = p + gamma*delta;
q = q + delta*delta;
}
a[0]=-p/q;
/* Determine other coefficients Ai */
for(i=1; i<=n+1; i++){
a[i] = ((h[i-1]+h[i])*y[i-1]-h[i]*a[i-1])/h[i-1];
}
}
29
The following procedure, BSpline2_Eval, computes the values of the quadratic spline.
Before calling this procedure, the arrays ai and hi must be determined using the previous
procedure. The input variable x is a single real number that should lie between t0 and tn .
double BSpline2_Eval(int n, double *t, double *a,
double *h, double x)
{
int i;
double d, e, result;
/* Check in which interval x lies */
for(i=n-1; i>=0; i--) {
if(x-t[i]>=0)
break;
}
/* Evaluate S(x) */
i = i+1;
d = (a[i+1]*(x-t[i-1])+a[i]*(t[i]-x+h[i+1]))/(h[i]+h[i+1]);
e = (a[i]*(x-t[i-1]+h[i-1])+a[i-1]*(t[i-1]-x+h[i]))/(h[i-1]+h[i]);
result = (d*(x-t[i-1])+e*(t[i]-x))/h[i];
return(result);
}
30
Output
Applied to a set 20 data points (for a randomly drawn free-hand curve), we obtain the
following graph for the quadratic B spline.
2
1.5
knots
B spline
1
0.5
0
0.5
1
1.5
0
The figure below shows a comparison of the quadratic B spline and a cubic spline.
2.5
2
knots
B spline
cubic spline
1.5
1
0.5
0
0.5
1
1.5
0
31