You are on page 1of 33

Chapter 3:

Curve Fitting & Interpolation

Lecture 3:
Spline Interpolation

MAFE208IU-L8 1
Why Spline Interpolation?

Apply lower-order polynomials to subsets of data points. Spline


provides a superior approximation of the behavior of functions that
have local, abrupt changes.

MAFE208IU-L8 2
Why Splines ?

17th Order
Polynomial

Original
Function

5th Order
Polynomial

9th Order
Polynomial

Figure : Higher order polynomial interpolation is a bad idea


MAFE208IU-L8 4
Spline Interpolation
• Polynomials are the most common choice of
interpolants.
• There are cases where polynomials can lead to
erroneous results because of round off error and
overshoot.
• Alternative approach is to apply lower-order
polynomials to subsets of data points. Such
connecting polynomials are called spline functions.

MAFE208IU-L8 5
Spline provides a superior approximation of the behavior
of functions that have local, abrupt changes (d).
MAFE208IU-L8 6
MAFE208IU-L8 8
Linear Spline
The first order splines for a group of ordered data
points can be defined as a set of linear functions:

f ( x) = f ( x0 ) + m0 ( x − x0 ) x0  x  x1
f ( x) = f ( x1 ) + m1 ( x − x1 ) x1  x  x2

f ( x) = f ( xn−1 ) + mn−1 ( x − xn−1 ) xn−1  x  xn

f ( xi +1 ) − f ( xi )
mi =
xi +1 − xi
MAFE208IU-L8 9
Linear spline - Example
Evaluate the function at x = 5 using first order x f(x)
splines:
3.0 2.5
2.5 − 1
m1 = = 0.6 4.5 1.0
7 − 4.5 7.0 2.5
f ( x) = f ( x1 ) + m1 ( x − x1 ) 9.0 0.5

f (5) = f (4.5) + 0.6(5 − 4.5)


= 1.0 + 0.6  0.5
= 1.3

MAFE208IU-L8 10
Linear Spline

• The main disadvantage of linear spline is that they are


not smooth. The data points where 2 splines meets
called (a knot), the changes abruptly.

• The first derivative of the function is discontinuous at


these points.

• Using higher order polynomial splines ensure


smoothness at the knots by equating derivatives at
these points.
MAFE208IU-L8 11
Quadric Splines
• Objective: to derive a second order polynomial for each
interval between data points
fi ( x) = ai x 2 + bi x + ci , x  [ xi −1 , xi ], i = 1, 2,..., n

For n+1 data points:


• i = (0, 1, 2, …n),
• n intervals,
• 3n unknown
constants (a’s, b’s and
c’s)

MAFE208IU-L8 12
Quadric Splines

• The function values of adjacent polynomials must be


equal at the interior knots 2(n-1).

fi ( xi ) = ai xi 2 + bi xi + ci = f ( xi )
fi +1 ( xi ) = ai +1 xi 2 + bi +1 xi + ci +1 = f ( xi ) i = 1, 2, ..., n − 1

• The first and last functions must pass through the end
points (2).
f1 ( x0 ) = a1 x0 2 + b1 x0 + c1 = f ( x0 )
f n ( xn ) = an xn 2 + bn xn + cn = f ( xn )

MAFE208IU-L8 13
Quadric Splines

• The first derivatives at the interior knots must be


equal (n-1).

fi ' ( x) = 2ai x + bi
fi '( xi ) = fi +1 '( xi )  2ai xi + bi = 2ai +1 xi + bi +1 , i = 1, 2,..., n − 1

• Assume that the second derivate is zero at the first


point (1)
a1 = 0
(The first two points will be connected by a straight line)
MAFE208IU-L8 14
Quadric Splines - Example

Fit the following data with quadratic splines. Estimate


the value at x = 5.

x 3.0 4.5 7.0 9.0


f(x) 2.5 1.0 2.5 0.5

Solutions:
There are 3 intervals (n=3), 9 unknowns.

MAFE208IU-L8 15
x 3.0 4.5 7.0 9.0
Example
f(x) 2.5 1.0 2.5 0.5

1. Equal interior points:


➢ For first interior point (4.5, 1.0)
The 1st equation:
f1 ( x1 ) = x12 a1 + x1b1 + c1 = f ( x1 )
(4.5) 2 a1 + 4.5b1 + c1 = f (4.5) 20.25 a1 + 4.5 b1 + c1 = 1.0

The 2nd equation:


f 2 ( x1 ) = x12 a2 + x1b2 + c2 = f ( x1 )
(4.5) 2 a2 + 4.5b2 + c2 = f (4.5) 20.25a2 + 4.5b2 + c2 = 1.0
MAFE208IU-L8 16
x 3.0 4.5 7.0 9.0
Example 2.5 1.0 2.5 0.5
f(x)

➢ For second interior point (7.0, 2.5)


The 3rd equation:
x22 a2 + x2b2 + c2 = f ( x2 )
(7) 2 a2 + 7b2 + c2 = f (7) 49a2 + 7b2 + c2 = 2.5
The 4th equation:

x22 a3 + x2b3 + c3 = f ( x2 )

(7) 2 a3 + 7b3 + c3 = f (7) 49a3 + 7b3 + c3 = 2.5

MAFE208IU-L8 17
x 3.0 4.5 7.0 9.0
Example
f(x) 2.5 1.0 2.5 0.5

➢ First and last functions pass the end points


For the start point (3.0, 2.5)

x a1 + x0 b1 + c1 = f ( x0 )
2
0 9a1 + 3b1 + c1 = 2.5
For the end point (9, 0.5)

x a + x3b3 + c3 = f ( x3 )
2
3 3 81a3 + 9b3 + c3 = 0.5

MAFE208IU-L8 18
x 3.0 4.5 7.0 9.0
Example
f(x) 2.5 1.0 2.5 0.5
➢Equal derivatives at the interior knots.
For first interior point (4.5, 1.0)
f1 '( x1 ) = f 2 '( x1 )
2 x1 a1 + b1 = 2 x1 a2 + b2 9a1 + b1 = 9a2 + b2
For second interior point (7.0, 2.5)
f 2 '( x2 ) = f3 '( x2 )
2 x2 a2 + b2 = 2 x2 a3 + b3 14a2 + b2 = 14a3 + b3
➢Second derivative at the first point is 0:
f '' ( x0 ) = a1 = 0
Example
System of linear equations AX=B
4.5 1 0 0 0 0 0 0  b1  1 
0 0 20.25 4.5 1 0 0 0  c  1 
  1   
0 0 49 7 1 0 0 0  a2  2.5
    
0 0 0 0 0 49 7 1  b2  = 2.5
3 1 0 0 0 0 0 0  c2  2.5
    
0 0 0 0 0 81 9 1  a3  0.5
1 0 − 9 −1 0 0 0 0  b  0 
  3   
0 0 14 1 0 − 14 −1 0  c3  0 
MAFE208IU-L8 20
x 3.0 4.5 7.0 9.0
Example
f(x) 2.5 1.0 2.5 0.5

Solving these 8 equations with 8 unknowns


a1 = 0, b1 = −1, c1 = 5.5
a2 = 0.64, b2 = −6.76, c2 = 18.46
a3 = −1.6, b3 = 24.6, c3 = −91.3
f1 ( x) = − x + 5.5, 3.0  x  4.5
f 2 ( x) = 0.64 x 2 − 6.76 x + 18.46, 4.5  x  7.0

f3 ( x) = −1.6 x 2 + 24.6 x − 91.3, 7.0  x  9.0

f (5) = f 2 (5) = 0.64  52 − 6.76  5 + 18.46 = 0.66


MAFE208IU-L8 21
Quadratic Splines:
A Practical way
Consider each polynomial
starting from 𝑓1
Calculations are simpler
𝑓1 passes through 𝑥0 , 𝑥1 :

b1 x0 + c1 = f ( x0 )
a1 = 0,   b1 , c1
b1 x1 + c1 = f ( x1 )
𝑓𝑖 passes through 𝑥𝑖−1 , 𝑥𝑖 , and 𝑓𝑖′ (𝑥𝑖−1 ) = 𝑓𝑖−1

(𝑥𝑖−1 ):
ai xi −12 + bi xi −1 + ci = f ( xi −1 )
 2
ai xi + bi xi + ci = f ( xi )  ai , bi , ci , i = 2, 3,..., n
 2a x + b = 2a x + b
 i i −1
MAFE208IU-L8
i i −1 i −1 i −1
22
Exercise

Fit the following data with quadratic splines.


Estimate the value at x = 3 and x=6.
x 2.0 4 7.0 8.0
f(x) 0.5 2.0 4 2.5

MAFE208IU-L8 23
Cubic Splines
Objective: Derive a third order polynomial for each interval between data points

fi ( x) = ai x 3 + bi x 2 + ci x + d i , x  [ xi −1 , xi ], i = 1, 2,..., n
For n+1 data points:
• i = (0, 1, 2, …n),
• n intervals,
• 4n unknown constants
• (a’s, b’s ,c’s and d’s)

MAFE208IU-
x0 xi-1 xi xn 24
L8
Function values coincide at the interior knots 2(n-1):
𝑓𝑖+1 𝑥𝑖 = 𝑓 𝑥𝑖 , 𝑓𝑖 𝑥𝑖 = 𝑓 𝑥𝑖 , 𝑖 = 1,2, … , 𝑛 − 1
First and last functions pass through the endpoints (2):
𝑓1 𝑥0 = 𝑓 𝑥0 , 𝑓𝑛 𝑥𝑛 = 𝑓 𝑥𝑛

First derivatives at the interior knots coincide (n-1): 𝑓𝑖+1 𝑥𝑖 = 𝑓𝑖′ 𝑥𝑖

′′ ′′
Second derivatives at the interior knots coincide (n-1): 𝑓𝑖+1 𝑥𝑖 = 𝑓𝑖 𝑥𝑖

Natural splines: Second derivatives at the endpoints are zero (2):


𝑓1′′ 𝑥0 = 𝑓𝑛′′ 𝑥𝑛 = 0

MAFE208IU-
x0 xi-1 xi xn 25
L8
Alternative technique to get Cubic Splines
The second derivative within each interval [xi-1, xi ] is a straight line,
represented by first order Lagrange interpolating polynomials:

x − xi x − xi −1
fi ( x) = f ( xi −1 )
'' ''
+ f ( xi )
''

xi −1 − xi xi − xi −1

MAFE208IU-
MAFE208IU-L8
x0 xi-1 xi xn 2626
L8
Cubic Splines

• The last equation can be integrated twice


2 unknown constants of integration can be evaluated by
applying the boundary conditions:
1. fi(x) = f (xi-1) at xi-1
2. fi(x) = f (xi) at xi
Unknowns:
f "( xi −1 ) f "( xi )
fi ( x) = ( i )
− + ( i −1 )

3 3
x x x x
6 ( xi − xi −1 ) 6 ( xi − xi −1 ) f ' ' ( xi )
 f ( xi −1 ) f "( xi −1 ) ( xi − xi −1 )  i = 0, 1,…, n
+ −  ( xi − x )
 xi − xi −1 6 
 f ( xi ) f "( xi ) ( xi − xi −1 ) 
+ −  ( x − xi −1 )
MAFE208IU-L8
 xi − xi −1 6  27
f i−' 1 ( xi ) = f i ( xi )
'
For each interior point xi :
This equation result with n-1 unknown second derivatives

( xi − xi −1 ) f '' ( xi −1 ) + 2( xi +1 − xi −1 ) f '' ( xi ) + ( xi +1 − xi ) f '' ( xi +1 )


6 6
=  f ( xi +1 ) − f ( xi ) +  f ( xi −1 ) − f ( xi )
xi +1 − xi xi − xi −1

For boundary points: f "( x0 ) = f "( xn ) = 0


Set ui = f "( xi ), fi = f ( xi ), hi = xi − xi −1
This yields n-1 linear equations
 fi +1 − fi f i − f i −1 
hi ui −1 + 2(hi + hi +1 )ui + hi +1ui +1 = 6  −  , i = 1, 2,..., n − 1
 hi +1 hi 
MAFE208IU-L8 28
Matrix form of n − 1 linear equations: Au = b

 2(h1 + h2 ) h2 0 0 0 
 h 2(h2 + h3 ) h3 0 0 
 2 
 0 h3 2(h3 + h4 ) 
A= 
 0 
 0 0 0 2(hn − 2 + hn −1 ) hn −1 
 
 0 0 0 hn −1 2(hn −1 + hn ) 
 f 2 − f1 f1 − f 0 
 −  ui −1 u
h h fi ( x) = ( xi − x ) + i ( x − xi −1 )
3 3
 f1"   2 1

 f3 − f 2 f 2 − f1  6hi 6hi
 " −
u=  f2  
, b = 6  f u h
 
h3 h2  +  i −1 − i −1 i  ( xi − x )
 "     hi 6 
 f n −1    f uh
 f n − f n −1 − f n −1 − f n − 2  +  i − i i  ( x − xi −1 ) , xi −1  x  xi
MAFE208IU-L8  hn hn −1   hi 6  29
Special case: Equally-spaced data: ℎ𝑖 = ℎ, 𝑖 = 1,2, … , 𝑛

 4h h 0 0 0
 h 4h h 0 
 0  f 2 − 2 f1 + f 0   f1" 
 0 h 4h     "
6  f3 − 2 f 2 + f1 
A=  b= u=  f2 
 0 h   
   " 
0 0 0 4h h   n
f − 2 f + f n−2 
 
n −1
 f n −1 
0 0 0 h 4h 

ui −1 ui
fi ( x) = ( xi − x ) + ( x − xi −1 )
3 3

6h 6h
 fi −1 ui −1h 
𝐴𝑢 = 𝑏 + −  ( xi − x )
h 6 
f uh 
+  i − i  ( x − xi −1 ) , xi −1  x  xi
h 6 
MAFE208IU-L8 30
Example

Fit the following data with cubic splines


Use the results to estimate the value at x=5.

x 3.0 4.5 7.0 9.0


f(x) 2.5 1.0 2.5 0.5

Solution:
➢Natural Spline:

f '' ( x0 ) = f '' (3) = 0, f '' ( x3 ) = f '' (9) = 0

MAFE208IU-L8 31
x 3.0 4.5 7.0 9.0  f 2 − f1 f1 − f 0 
Solution  h2

h1 
f(x) 2.5 1.0 2.5 0.5  
 f3 − f 2 f 2 − f1 

b = 6  h3 h2 

2(1.5 + 2.5) 2.5 8 2.5 



A= =  f n − f n −1 − f n −1 − f n − 2 
2.5 2(2.5 + 2) 2.5 9  hn hn −1 

2.5−1 1−2.5 1.6791


− 48/5 u=
2.5 1.5 Au=b
b=6 = −1.5331
0.5−2.5

2.5−1 −48/5
2 2.5

f1 ( x) = 0.186566 ( x − 3)3 + 1.6667 (4.5 − x) + 0.24689( x − 3)

f 2 ( x) = 0.111939(7 − x)3 − 0.102205 ( x − 4.5)3 − 0.29962(7 − x) + 1.638783 ( x − 4.5)

f 3 ( x) = −0.127757(9 − x)3 + 1.761027 (9 − x) + 0.25 ( x − 7)

f (5) = f 2 (5) = 1.102886


MAFE208IU-L8 32
Exercise
−𝑥 2
Reconstruct the function 𝑓 𝑥 = 𝑒 in [0, 3/2] using
the values of f(x) at x=0, 1/2, 1, and 3/2 by
a) linear splines, and use it to find f(3/4) and error
b) quadratic splines, and use it to find f(3/4) and error
c) cubic splines, and use it to find f(3/4) and error

MAFE208IU-L8 33
Exercise
Reconstruct the function 𝑓 𝑥 = ln(𝑥 2 + 1) in [0,1]
using the values of f(x) at x=0, ¼, 1/2, 3/4 and 1 by a
cubic spline, and use it to find f(2/3) and error

MAFE208IU-L8 34
Exercise
The oxygen level O (mg/L) at different temperature T of the
sea is given in the following table
a) Fit the data with linear splines. Use the results to
estimate the oxygen level value at T=20.
b) Fit the data with quadratic splines. Use the results to
estimate the oxygen level value at T=20.
c) Fit the data with cubic splines. Use the results to
estimate the oxygen level value at T=20.
x=T 8 16 24 32
f(x) 11.843 9.87 8.418 7.305

You might also like