You are on page 1of 14

# 1

## Chapter 4. Common Splines

A spline function is a piecewise polynomial function joined together with certain
continuity conditions satisfied.

## 4.1. Linear Spline

For a set of data points usually termed knots: ( xi , y i , i 1, 2, , n) , the linear
spline is
si ( x) ai x bi , for x xi , xi 1 , i 1, 2, , n 1

(4.1.1)

## The C0 conditions, s i ( xi ) y i and si ( xi 1 ) y i 1 yield 2(n 2) equations at interior

points. These along with one condition at each end point give a total of 2n 2 equations
to match the 2n 2 unknowns: ai , bi , i 1, 2, , n 1 . By applying these, we get
s i ( x) y i

x xi 1
x xi
y yi
y i 1
y i i 1
( x xi ), x xi , xi 1
xi xi 1
xi 1 xi
xi 1 xi

(4.1.2)

## which results in straight lines joining neighboring points.

Clearly, s i (x ) is the Lagrange interpolation formula for the data set consisting of
the following two points: ( xi , y i ) and ( xi 1 , y i 1 ) . Hence it is the solution
approximation for linear finite elements in one dimension.
Example 4.1.1. Find the linear splines for the following data set
i 1 2 3 4 5
x 0 5 7 8 10
y 0 2 -1 -2 20
Linear spline fit
s1 ( x ) 0

x5
x0
2
2.5 x ,
05
50

s 2 ( x) 2

x7
x5
1
1.5 x 9.5 ,
57
75

s 3 ( x) 1

x [0, 5]

x 8
x7
2
x 6,
7 8
87

x [5, 7]

x [7, 8]

s 4 ( x) 2

x 10
x 8
20
11x 90 ,
8 10
10 8

x [8, 10]

20

data points
linear spline
interp1: linear

## 15 interp1: MATLAB function.

Type "help interp1" in MATLAB
window for its usage and options.

10

-5

5
x

10

## A MATLAB code linear_spline_examples.m is used to generate Figure 4.1.1.

Here is a brief algorithm for the code
(1)
(2)

Input the data points x(i) ,y(i), i = 1,n; Plot them as symbols using plot;
For i = 1, n-1, do
A = linspace(x(i),x(i+1),nint) % Divide each data interval into nint points
Call c1=linear_spline % Call the linear_spline.m function
Call c2=interp1 % Call the MATLAB function (default option: linear)
Plot c1
Plot c2
Enddo

(1)

## Input x where the interpolation value y is needed;

(2)
(3)

Check to see if x is out of range; if yes, abort the program; if no, proceed;
Find the data interval in which x resides;
Calculate y according to Equation 4.1.2.

Example 4.1.2 Finding the linear splines for the following data set
i 1 2 3 4 5 6
x 0 1 2 3 4 5
y 1 1 1 -1 -1 -1
We will use the same MATLAB code for this example. The only change needed in
linear_spline_examples.m is to replace the data set x and y with that in the table above.
The splines are plotted in Figure 4.1.2.
Linear Spline Interpolation
data points
linear spline
interp1: linear

1
0.8
0.6
0.4

0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0

0.5

1.5

2.5
x

3.5

4.5

## Figure 4.1.2 Linear Splines for data set 2.

Clearly the splines in both examples are just straight lines. We will use the two
data point sets above throughout this chapter and compare the splines due to different
methods.

si ( x) ai x 2 bi x ci ,

for x xi , xi 1 , i 1, 2, , n 1 (4.2.1)

## which is constrained to satisfy the C0 and C1 conditions.

For the C0 condition, we get
si ( xi ) y i ,

si ( xi 1 ) y i 1 ,

i 1,2,...n 1

## For the C1 condition, we get

si ( xi 1 ) si1 ( xi 1 ), i 1, 2, , n 2

(4.2.2)

(4.2.3)

From the above three requirements, there are 3n-4 constraint conditions. But the
splines require a total of 3n-3 conditions, so we are one condition short and the problem
is not completely defined. Usually, s1(x1) = 0 is used for the additional condition. This
results in the so-called natural quadratic spline. Certainly other conditions can be used,
such as s1(x1) = sn-1(xn ).
To find the formula of the splines, first denote di = si (xi ), then since s(x) is a
linear spline for the data set (xi, di, i = 1,2, , n), we have
si ( x) d i 1

x xi
x x (d i 1 d i ) x d i 1 xi d i xi 1
d i i 1

xi 1 xi
xi 1 xi
xi 1 xi
d di
i 1
( x xi ) d i
xi 1 xi

## which is of course is the linear equation for the slope.

Integrating over x, we get
d di
s i ( x) i 1
( x xi ) 2 d i ( x xi ) y i
2( xi 1 xi )

(4.2.4)

after solving for the constant of integration using the C0 boundary condition: s i ( xi ) y i .
So the quadratic spline is defined once we have the d i s.
Letting x = xi+1, we get
d di
y i 1 i 1
( xi 1 xi ) 2 d i ( xi 1 xi ) y i
2( xi 1 xi )

5
d i 1 2

y i 1 y i
d i , i 1, 2, , n 1
xi 1 xi

(4.2.5)

which is a recursive scheme. If d1 is known, then di can be derived from the above
equation.
Various conditions can be imposed to obtain d1:
For the natural spline, d1 0 .
If d1 = d2, then d1 can be calculated by
y y1
y y1
d1 d 2 2 2
d1 d1 2
(4.2.6)
x 2 x1
x 2 x1
If d1 = dn, then d1 can be calculated by

n 1

y yi
d 1 d n 2 (1) n i 1 i 1
xi 1 xi
i 2

n 1

n i 1 y i 1 y i
d 1 (1)

xi 1 xi
i 2
(1) n d 1

no solution,

, n is even

n is odd
(4.2.7)

## This condition can not be applied when n is an odd number.

The algorithm of the MATLAB code quadratic_spline_examples.m is as
follows
(1)
Input the data points x(i) ,y(i), i = 1,n; Plot them as symbols using plot;
(2)
For i = 1, n-1, do
A = linspace(x(i),x(i+1),nint) % Divide each data interval into nint points
Call c2=quadratic_spline with d1 = d2;
Plot c1
Plot c2
Enddo
The quadratic_spline.m function is built according to the following algorithm
(1) Input x where the interpolation value y is needed;
Check to see if x is out of range; if yes, abort the program; if no, proceed;
(2) Find the data interval where x belongs;
(3) Calculate d1; Calculate di (Equation 4.2.5);
(4) Calculate y according to Equation 4.2.4.
Example 4.2.1. Given the same data points in Example 4.1.1, derive the quadratic splines.
Using the natural quadratic splines, where d1 = 0, then we have
d2 2

20
0 0.8,
50

d3 2

1 2
0. 8 3. 8
75

d4 2

2 1
20 2
3.8 1.8, d 5 2
1.8 20.2
87
10 8

s1 ( x)

0 .8 0
( x 0) 2 0( x 0) 0 0.08 x 2 ,
2(5 0)

s 2 ( x)

3.8 0.8
( x 5) 2 0.8( x 5) 2 1.15 x 2 12.3 x 289.5,
2(7 5)

s3 ( x)

1.8 3.8
( x 7) 2 3.8( x 7) 1 2.8 x 2 9.4 x 162.8,
2(8 7)

s 4 ( x)

20.2 1.8
( x 8) 2 1.8( x 8) 2 4.6 x 2 71.8 x 273.6,
2(10 8)

x [0, 5]

x [5, 7]

x [7, 8]

x [8, 10]

The results and the splines due to d1 = d2, are plotted in Figure 4.2.1. Near x = 0,
the natural spline has a flatter curve due to zero slope at x = 0. The other spline has a
linear slope in the first interval. Throughout all the data intervals, these two splines are
different, although the differences are small for the most part.

20

data points

15

10

-5

5
x

## Figure 4.2.1. Quadratic Splines for data set 1.

10

Example 4.2.2. Given the same data points in Example 4.1.2, obtain the quadratic splines.
The only change needed in quadratic_spline_examples.m is to replace the data
set x and y with the table in Example 4.1.2. The splines are plotted in Figure 4.2.2. Note
that the natural splines and splines due to d1 = d2 are exactly the same. This is caused by
the fact that both options produce the same result: d1 = 0.
Also, the splines in the 4th and 5th intervals have large oscillations. We would like
a smooth transition in the 3rd interval ideally and the splines do have this feature. But
because of the under-shoot at interval 4, there are oscillations in the 4th and 5th intervals.
Generally, when the order of polynomial splines goes up, this problem can become very
severe.

1.5

data points

1
0.5

0
-0.5
-1
-1.5
-2

0.5

1.5

2.5
x

3.5

4.5

## 4.3. Natural Cubic Splines

Generally, a function s is called a spline of degree k on x1 x 2 x n if
(1) s x1 , x n ;

8
(2) s ( j ) , j 0, 1, 2, , k 1 are all continuous functions on x1 , x n where s(j) is the
jth derivative;
(3) s is a polynomial of degree k on each interval xi , xi 1 .
If k 3 , the spline is called a cubic spline. Then the curvatures are linear in an
interval and denoting values at the points x i as ei s i( xi ), i 1, 2, , n , they
comprise a linear spline for the data set ( xi , ei , i 1, 2, , n) . Therefore
x xi
x x
si( x) ei 1
ei i 1
, i 1, 2, , n 1
xi 1 xi
xi 1 xi
(4.3.1)
Integrating over x twice, we get
si ( x)

ei 1
e
( x xi ) 3 i ( xi 1 x) 3 c( x xi ) d ( xi 1 x)
6hi
6hi

(4.3.2)

## where hi = xi+1 - xi and c and d are constants of integration.

Applying the C0 conditions, we get
c

y i 1 ei 1 hi
y eh

; d i i i
hi
6
hi
6

(4.3.3)

si ( x)

ei 1
e
y
e h
y
eh
( x xi ) 3 i ( xi 1 x) 3 ( i 1 i 1 i )( x xi ) ( i i i )( xi 1 x)
6hi
6hi
hi
6
hi
6

(4.3.4)
So the cubic spline is defined once we have the eis.
1

## The values of ei can be derived from the C continuity condition. Differentiating

the above equation, we get
si ( x)

and

ei 1
e
y
e h
y
eh
( x xi ) 2 i ( xi 1 x ) 2 ( i 1 i 1 i ) ( i i i )
2hi
2hi
hi
6
hi
6

9
si ( xi )

ei 2
y
e h
y
eh
e h eh
hi ( i 1 i 1 i ) ( i i i ) i 1 i i i bi
2hi
hi
6
hi
6
6
3

where
bi

y i 1 y i
, i 1, 2, , n 1
hi

(4.3.5)

Also
s i1 ( xi )

ei
y
eh
y
e h
e h
eh
hi21 ( i i i 1 ) ( i 1 i 1 i 1 ) i 1 i 1 i i 1 bi 1
2hi 1
hi 1
6
hi 1
6
6
3

## By setting si( xi ) si1 ( xi ) at all interior points, we have

hi 1ei 1 2( hi 1 hi )ei hi ei 1 6(bi bi 1 ),

i 2, 3, , n 1

(4.3.6)

## which is a tridiagonal system of equations in terms of the unknowns ei s. There are n

unknowns and n 2 equations. Two additional conditions are needed to determine the
cubic splines. When they are e1 en 0 , the resulting splines are called the natural cubic
splines. These equations can be rearranged as
e1 0
hi 1ei 1 u i ei hi ei 1 vi

i 2, 3, , n 1

(4.3.7)

en 0

where
u i 2(hi 1 hi )
i 2, 3, , n 1
vi 6(bi bi 1 )

(4.3.8)

## The algorithm of the code cubic_spline_examples.m is very similar to

linear_spline_examples.m, so only the algorithm for the function cubic_spline.m will
be presented here.
(1)
(2)
(3)
(4)
(5)
(6)
(7)

## Input x where the interpolation value y is needed; % s = y here.

Check to see if x is out of range; if yes, abort the program; if no, proceed;
Calculate hi and bi (Equation 4.3.5);
Calculate ui and vi (Equations 4.3.8);
Assign e1 = 0, then calculate ei (Equation 4.3.7)
Find the data interval in which x resides;
Calculate y according to Equation 4.3.4.

10
Example 4.3.1. Given the data points in Example 4.1.1, obtain the natural cubic splines.
To calculate the 2nd derivatives, first determine
h1 = 5, h2 = 2, h3 = 1, h4 = 2
b1 = 0.4, b2 = -1.5, b3 = -1, b4 = 1
Using above we can calculate uis and vis as
u2 = 14, u3 = 6, u4= 6
v2 = -11.4, v3 = 3, v4 = 12
Then from equation 4.3.7, we get
14 2 0

2 6 1
0 1 6

e2 11.4

e3 3
e 12
4

The values of e2, e3, and e4 can be obtained by using the tridiagonal solver. These
values along with e1= e5 = 0 can then be used to determine the splines.
The natural cubic splines are plotted in Figure 4.3.1. MATLAB functions spline
and interp1 with option set to spline produce exactly the same splines. The additional
two constraints built into these two MATLAB functions are as follows
s1( 3) ( x 2 ) s 2( 3) ( x 2 ) ,

## and s n( 3)2 ( x n 1 ) s n( 3)1 ( x n 1 )

11
Cubic Spline Interpolation

20

data points
natural cubic spline
interp1:spline
spline

15

10

-5

5
x

10

## Figure 4.3.1. Cubic Splines for data set 1

Example 4.3.2. Given the data points in Example 4.1.2, obtain the cubic splines.
The splines are plotted in Figure 4.3.2. All of them did better than the quadratic
splines. The oscillations are smaller. The natural splines here did even better than the
ones due to spline and interp1: spline in the 1st and 5th intervals.

12
Cubic Spline Interpolation

1.5

data points
natural cubic spline
interp1:spline
spline

0.5

-0.5

-1

-1.5

0.5

1.5

2.5
x

3.5

4.5

## Figure 4.3.2. Cubic Splines for data set 2.

4.4. Conclusion
Splines of increasing order are obtained by increasing the order of continuity in
the matching conditions across intervals at interior points: linear splines match the data,
quadratic splines match first derivatives, cubic splines match second derivatives, etc. We
stop there because the concept is set and because higher order splines are rarely used in
applications. But is an increase in order worth the extra mathematical effort required to
generate it?
Observation of the graphs reveals that splines of order greater one oscillate
through the data. A comparison of splines is shown in Figures 4.4.1 and 4.4.2. We
discover that the quadratic spline, in general, oscillates more than the cubic about the
linear spline which we use as a reference. So increasing the order does not always
increase the oscillations. So which spline do we choose?
Selection of an appropriate spline depends on the application. If we want to
interpolate the data itself, perhaps the linear is the best choice. If we have data that we
know is cubic in nature, then a cubic spline may be a better choice. If we want derivatives
of data, a linear spline creates jumps at data points, hence a degree of smoothness may be
necessary. So again, one must make a choice based upon the application and carefully
weigh the results. In design, splines allow local smoothing through data and the cubic
spline is the usual choice.

13
How do splines compare to Hermite interpolations which also treat derivatives? It
is very important to understand the following concept: splines match derivative
conditions at intersections of splines, not derivatives in the data itself. In other words,
splines are piecewise functions which approximate the data by filling in the voids
between data: their matching conditions insure smooth connections between themselves,
not the data. On the other hand, Hermite interpolations require derivatives of data, that is,
the derivatives are part of the data set, e.g., position, velocity, acceleration. The only
relation between spline and Hermite interpolation is that they are both polynomials.
Spline Comparison

20

data points
natural cubic spline
linear spline

15

10

-5

5
x

## Figure 4.4.1 Spline comparison for dataset 1

10

14

Spline Comparison

1.5

data points
natural cubic spline
linear spline

1
0.5

0
-0.5
-1
-1.5
-2

0.5

1.5

2.5
x

3.5

4.5