MAFE208IU-L8 - Spline Interpolation
MAFE208IU-L8 - Spline Interpolation
MAFE208IU-L8 - Spline Interpolation
Lecture 3:
Spline Interpolation
MAFE208IU-L8 1
Why Spline Interpolation?
MAFE208IU-L8 2
Why Splines ?
17th Order
Polynomial
Original
Function
5th Order
Polynomial
9th Order
Polynomial
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
MAFE208IU-L8 10
Linear Spline
MAFE208IU-L8 12
Quadric Splines
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
fi ' ( x) = 2ai x + bi
fi '( xi ) = fi +1 '( xi ) 2ai xi + bi = 2ai +1 xi + bi +1 , i = 1, 2,..., n − 1
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
x22 a3 + x2b3 + c3 = f ( x2 )
MAFE208IU-L8 17
x 3.0 4.5 7.0 9.0
Example
f(x) 2.5 1.0 2.5 0.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
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
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 𝑥𝑖 = 𝑓𝑖 𝑥𝑖
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
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
Solution:
➢Natural Spline:
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
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