Spline Method of Interpolation - Matlab
Spline Method of Interpolation - Matlab
http://numericalmethods.eng.usf.edu
Transforming Numerical Methods Education for STEM
Undergraduates
http://numericalmethods.eng.usf.edu 1
Spline Method of
Interpolation
http://numericalmethods.eng.usf.edu
What is Interpolation ?
Given (x0,y0), (x1,y1), …… (xn,yn), find the
value of ‘y’ at a value of ‘x’ that is not given.
3 http://numericalmethods.eng.usf.edu
Interpolants
Polynomials are the most common
choice of interpolants because they
are easy to:
Evaluate
Differentiate, and
Integrate.
4 http://numericalmethods.eng.usf.edu
Why Splines ?
1
f ( x) =
1 + 25 x 2
Table : Six e quidistantly s paced points in [-1, 1]
1
x y=
1 + 25 x 2
-1.0 0.038461
-0.6 0.1
-0.2 0.5
0.2 0.5
0.6 0.1
5 http://numericalmethods.eng.usf.edu
Why Splines ?
1.2
0.8
0.4
y
0
-1 -0.5 0 0.5 1
-0.4
-0.8
x
19th Order Polynomial f (x) 5th Order Polynomial
7 http://numericalmethods.eng.usf.edu
Linear Interpolation (contd)
f ( x1 ) − f ( x 0 )
f ( x ) = f ( x0 ) + ( x − x 0 ), x 0 ≤ x ≤ x1
x1 − x 0
f ( x 2 ) − f ( x1 )
= f ( x1 ) + ( x − x1 ), x1 ≤ x ≤ x 2
x2 − x1
.
.
.
f ( x n ) − f ( x n −1 )
= f ( x n −1 ) + ( x − x n −1 ), x n −1 ≤ x ≤ x n
x n − x n −1
v(t ) − v (t 0 )
500
v (t ) = v(t 0 ) + 1 (t − t 0 )
t1 − t 0 ys
f ( range)
450
517.35 − 362.78 f(x )
= 362.78 + (t − 15) desired
20 − 15
v (t ) = 362.78 + 30.913( t − 15)
400
= 393.7 m/s
10 http://numericalmethods.eng.usf.edu
Quadratic Interpolation
Given ( x0 , y0 ), ( x1 , y1 ),......, (x n −1 , y n −1 ), ( x n , y n ) , fit quadratic splines through the data. The splines
are given by
f ( x ) = a1 x 2 + b1 x + c1 , x 0 ≤ x ≤ x1
= a 2 x 2 + b2 x + c2 , x1 ≤ x ≤ x 2
.
.
.
= a n x 2 + bn x + cn , x n −1 ≤ x ≤ x n
Find a i , bi , ci , i = 1, 2, …, n
11 http://numericalmethods.eng.usf.edu
Quadratic Interpolation (contd)
Each quadratic spline goes through two consecutive data points
a1 x 0 + b1 x 0 + c1 = f ( x0 )
2
a1 x1 + b1 x1 + c1 = f ( x1 )
2
.
.
.
a i xi −1 + bi xi −1 + ci = f ( xi −1 )
2
a i xi + bi xi + c i = f ( xi )
2
.
.
.
a n x n −1 + bn x n −1 + c n = f ( xn −1 )
2
a n x n + bn xn + cn = f ( x n )
2
2 a1 x1 + b1 = 2a 2 x1 + b2
2 a1 x1 + b1 − 2a 2 x1 − b2 = 0
13 http://numericalmethods.eng.usf.edu
Quadratic Splines (contd)
Similarly at the other interior points,
2a 2 x 2 + b2 − 2a3 x 2 − b3 = 0
.
.
.
2ai xi + bi − 2ai +1 xi − bi +1 = 0
.
.
.
2a n −1 x n −1 + bn −1 − 2a n x n−1 − bn = 0
We have (n-1) such equations. The total number of equations is (2n) + (n − 1) = (3n − 1) .
We can assume that the first spline is linear, that is a1 = 0
14 http://numericalmethods.eng.usf.edu
Quadratic Splines (contd)
This gives us ‘3n’ equations and ‘3n’ unknowns. Once we find the ‘3n’ constants,
we can find the function at any value of ‘x’ using the splines,
f ( x) = a1 x 2 + b1 x + c1 , x0 ≤ x ≤ x1
= a 2 x 2 + b2 x + c 2 , x1 ≤ x ≤ x 2
.
.
.
= a n x 2 + bn x + c n , x n −1 ≤ x ≤ x n
15 http://numericalmethods.eng.usf.edu
Quadratic Spline Example
The upward velocity of a rocket is given as a function of time.
Using quadratic splines
a) Find the velocity at t=16 seconds
b) Find the acceleration at t=16 seconds
Table Velocity as a
function of time
16 http://numericalmethods.eng.usf.edu
Solution
v(t ) = a1t + b1t + c1 , 0 ≤ t ≤ 10
2
= a 2 t + b2 t + c 2 , 10 ≤ t ≤ 15
2
= a3t + b3t + c3 , 15 ≤ t ≤ 20
2
= a 4 t + b4 t + c 4 , 20 ≤ t ≤ 22.5
2
= a5 t + b5 t + c5 ,
2
22.5 ≤ t ≤ 30
Let us set up the equations
17 http://numericalmethods.eng.usf.edu
Each Spline Goes Through
Two Consecutive Data Points
v(t ) = a1t + b1t + c1 , 0 ≤ t ≤ 10
2
a1 (0) + b1 (0) + c1 = 0
2
18 http://numericalmethods.eng.usf.edu
Each Spline Goes Through
Two Consecutive Data Points
a 2 (10) 2 + b2 (10) + c 2 = 227.04
t v(t)
a 2 (15) 2 + b2 (15) + c 2 = 362.78
s m/s
0 0 a3 (15) + b3 (15) + c3 = 362.78
2
15 362.78
a4 (20) + b4 (20) + c4 = 517.35
2
20 517.35
a4 (22.5) 2 + b4 (22.5) + c4 = 602.97
22.5 602.97
30 901.67 a5 (22.5) + b5 (22.5) + c5 = 602.97
2
= a 2 t + b2 t + c 2 ,10 ≤ t ≤ 15
2
d
dt
(a1t + b1t + c1
2
) =
d
dt
(a2t + b2t + c2
2
)
t =10 t =10
a1 = 0
22 http://numericalmethods.eng.usf.edu
Final Set of Equations
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 a1 0
100 10 1 0 0 0 0 0 0 0 0 0 0 0 0 b1 227.04
0 0 0 100 10 1 0 0 0 0 0 0 0 0
227.04
0 c1
0 0 0 225 15 1 0 0 0 0 0 0 0 0 0 a 2 362.78
0 0 0 0 0 0 225 15 1 0 0 0 0 0 0 b2 362.78
0 0 0 0 0 0 400 20 1 0 0 0 0 0 0 c2 517.35
0 0 0 0 0 0 0 0 0 400 20 1 0 0 0 a3 517.35
0 0 0 0 0 0 0 0 0 506.25 22.5 1 0 0 0 b3 = 602.97
0 0 0 0 0 0 0 0 0 0 0 0 506.25 22.5 1 c3 602.97
0 0 0 0 0 0 0 0 0 0 0 0 900 30 1 a 4 901.67
20 1 0 − 20 − 1 0 0 0 0 0 0 0 0 0 0 b4 0
0 0 0 30 1 0 − 30 − 1 0 0 0 0 0 0 0 c4 0
− 40 −1
0 0 0 0 0 0 40 1 0 0 0 0
0 a5 0
0 0 0 0 0 0 0 0 0 45 1 0 − 45 − 1 0 b5 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 c5 0
23 http://numericalmethods.eng.usf.edu
Coefficients of Spline
i ai bi ci
1 0 22.704 0
2 0.8888 4.928 88.88
3 −0.1356 35.66 −141.61
4 1.6048 −33.956 554.55
5 0.20889 28.86 −152.13
24 http://numericalmethods.eng.usf.edu
Quadratic Spline Interpolation
Part 2 of 2
http://numericalmethods.eng.usf.edu
25 http://numericalmethods.eng.usf.edu
Final Solution
v(t ) = 22.704t , 0 ≤ t ≤ 10
= 0.8888t 2 + 4.928t + 88.88, 10 ≤ t ≤ 15
= −0.1356t 2 + 35.66t − 141.61, 15 ≤ t ≤ 20
= 1.6048t 2 − 33.956t + 554.55, 20 ≤ t ≤ 22.5
= 0.20889t 2 + 28.86t − 152.13, 22.5 ≤ t ≤ 30
26 http://numericalmethods.eng.usf.edu
Velocity at a Particular Point
a) Velocity at t=16
v(t ) = 22.704t , 0 ≤ t ≤ 10
= 0.8888t 2 + 4.928t + 88.88, 10 ≤ t ≤ 15
= −0.1356t 2 + 35.66t − 141.61, 15 ≤ t ≤ 20
= 1.6048t 2 − 33.956t + 554.55, 20 ≤ t ≤ 22.5
= 0.20889t 2 + 28.86t − 152.13, 22.5 ≤ t ≤ 30
= 394.24 m/s
27 http://numericalmethods.eng.usf.edu
Acceleration from Velocity Profile
b) The quadratic spline valid at t=16 is
given by
d
a (16) = v(t ) t =16
dt
v(t ) = −0.1356t 2 + 35.66t − 141.61, 15 ≤ t ≤ 20
d
a (t ) = ( −0.1356t + 35.66t − 141.61)
2
dt
= −0.2712t + 35.66, 15 ≤ t ≤ 20
a (16) = −0.2712(16) + 35.66 = 31.321m/s
2
28 http://numericalmethods.eng.usf.edu
Distance from Velocity Profile
c) Find the distance covered by the rocket from t=11s to
t=16s.
16
S (16) − S (11) = ∫ v (t )dt
11
( ) ( )
15 16
= ∫ 0.8888t 2 + 4.928t + 88.88 dt + ∫ − 0.1356t 2 + 35.66t − 141.61 dt
11 15
= 1595.9 m
29 http://numericalmethods.eng.usf.edu
Additional Resources
For all resources on this topic such as digital audiovisual
lectures, primers, textbook chapters, multiple-choice
tests, worksheets in MATLAB, MATHEMATICA, MathCad
and MAPLE, blogs, related physical problems, please
visit
http://numericalmethods.eng.usf.edu/topics/spline_met
hod.html
THE END
http://numericalmethods.eng.usf.edu