L08 Interpolation
L08 Interpolation
Lecture 8
Interpolation
Linear Interpolation
Quadratic Interpolation
Polynomial Interpolation
Piecewise Polynomial Interpolation
Mongkol JIRAVACHARADET
SURANAREE
INSTITUTE OF ENGINEERING
UNIVERSITY OF TECHNOLOGY
Visual Interpolation
40
20
0
60
80
km/h
100
120
known data
What is the
corresponding value
of y for this x ?
BASIC IDEAS
From the known data ( xi , yi ), interpolate y = F ( x ) for x x i
x1
y1
M
M
xi
y i interpolate
x i +1 y i +1
M
M
xn
yn
x y
F(x)
known data
curve fit
interpolation
x
Curve fitting: fit function & data not exactly agree
Interpolation: function passes exactly through known data
x1
x2
Linear Interpolation
The most common way to estimate a data point between 2 points.
The function is estimated by a straight line drawn between them.
y
Interpolated Point
2 points
3 points
f(x) = a0 + a1x
4 points
Cubic = 3rd order
f(x) = a0 + a1x+ a2x2 + a3x3
Linear Interpolation
Connect two data points with a straight line
y1
y y 0
y y0
= 1
x x0
x1 x 0
F ( x ) = y
y0
x0
x1
F ( x ) = y = y 0 +
y1 y 0
( x x0 )
x1 x0
Quadratic Interpolation
Second-order polynomial interpolation using 3 data points
Convenient form:
(x1, y1)
f2 ( x ) = b0 + b1( x x0 ) + b2 ( x x0 )( x x1 )
(x2, y2)
f2 ( x ) = a0 + a1x + a2 x 2
where
a1 = b1 + b2 x0 + b2 x1
(x0, y0)
Substitute f2 ( x0 ) = y 0 b0 = y 0
Substitute f2 ( x1 ) = y 1 b1 =
Substitute f2 ( x 2 ) = y 2
a0 = b0 + b1x0 + b2 x 0 x1
a2 = b2
y1 y 0
x1 x 0
y 3 y 2 y 2 y1
x3 x 2 x 2 x1
b2 =
x3 x1
ln(1)
ln(2)
ln(4)
ln(6)
f1(2) = 0 +
=
=
=
=
0
0.6932
1.3863
1.7918
1.7918 0
( 2 1) = 0.3584 t = 48 .3%
6 1
f1(2) = 0 +
1.3863 0
( 2 1) = 0.4621 t = 33 .3%
4 1
b0 = 0 ,
b1 =
1.3863 0
= 0.4621
4 1
1.7918 1.3863
0.4621
6
4
b3 =
= 0.05187
6 1
MATLAB :
>> x = 1:0.01:7;
>> y = log(x);
>> y2= -0.05187*x.^2+...
0.7251*x-0.6696;
>> plot(x,y,x,y2)
Polynomial Interpolation
Finding Pn-1(x) of degree n-1 that passes through n known data pairs
Linear
= 1 : 2 pt.
Quadratic = 2 : 3 pt.
Cubic
= 3 : 4 pt.
n pairs of (x, y)
n equations
n unknowns
2
2
1 c1 2
1 c2 = 1
1 c3 1
Vandermonde Matrix
x12
2
x2
x32
x1
x2
x3
1 c1 y1
1 c2 = y2
1 c3 y3
MATLAB:
>> x = [-2 -1 2];
>> A = [x.^2 x ones(size(x))];
or use the built-in vander function
>> A = vander([-2 -1 2]);
>> y = [-2 1 -1];
>> c = A\y
c =
-0.9167
0.2500
2.1667
2.1667
-2
-1
Polynomials Wiggle
2nd-order
3rd-order
4th-order
5th-order
x
xi
yi
10
x = [1 2 3 4 5 6 7 8 9 10];
y = [3.5 3.0 2.5 2.0 1.5 -2.4 -2.8 -3.2 -3.6 -4.0];
x0 = 1:0.1:10;
y2 = polyval(polyfit(x(4:6), y(4:6), 2), x0);
y3 = polyval(polyfit(x(4:7), y(4:7), 3), x0);
y4 = polyval(polyfit(x(3:7), y(3:7), 4), x0);
y5 = polyval(polyfit(x(3:8), y(3:8), 5), x0);
axis([0 10 -5 5])
plot(x, y, o)
hold on
plot(x0, y2)
plot(x0, y3)
plot(x0, y4)
plot(x0, y5)
x
Piecewise-linear interpolation
Piecewise-quadratic interpolation
Piecewise-cubic interpolation
f(x) and f(x) continuous at breakpoint = cubic spline
Description
interp1
interp2
interp3
interpft
interpn
spline
xhat
x = [1 2 3 4 5 6 7 8 9 10];
y = [3.5 3.0 2.5 2.0 1.5 -2.4 -2.8 -3.2 -3.6 -4.0];
xhat = 1:0.1:10;
% eval interpolant at xhat
yn = interp1(x, y, xhat, nearest);
plot(x, y, o, xhat, yn); pause;
yl = interp1(x, y, xhat, linear);
plot(x, y, o, xhat, yl); pause;
yc = interp1(x, y, xhat, cubic);
plot(x, y, o, xhat, yc); pause;
ys = interp1(x, y, xhat, spline);
>> ys = spline(x, y, xhat);
plot(x, y, o, xhat, ys);
15
10
>> x = 0:5;
15
10
ans =
4
Plot Graph:
>> xhat = 0:0.2:5;
0
0