Transcript
7/30/2019 L08 Interpolation
1/20
Interpolation
- Linear Interpolation
- Quadratic Interpolation
- Polynomial Interpolation
- Piecewise Polynomial Interpolation
Numerical Methods forNumerical Methods forCivilCivilEngineersEngineers
Lecture 8
ByBy MongkolMongkol JIRAVACHARADETJIRAVACHARADET
School of Civil EngineeringSchool of Civil EngineeringSuranareeSuranaree University of TechnologyUniversity of Technology
7/30/2019 L08 Interpolation
2/20
Visual Interpolation
Vehicle speed is approximately 49 km/h
0
20
4060
80
100
120
km/h
7/30/2019 L08 Interpolation
3/20
BASIC IDEAS
From the known data ( xi , yi ), interpolate ( ) for iy F x x x=
Determining coefficient a1, a2, . . . , an of basis function (x)
F(x) = a11(x) + a22(x) + . . . + ann(x)
Polynomials are often used as the basis functions.
F(x) = a1 + a2x + a3x2 + . . . + an x
n-1
7/30/2019 L08 Interpolation
4/20
Interpolation v.s. Curve Fitting
known datay
x
interpolation
curve fit
Curve fitting: fit function & data not exactly agree
Interpolation: function passes exactly through known data
7/30/2019 L08 Interpolation
5/20
Interpolation & Extrapolation
Interpolation approximate within the range of independent variable
of the given data set.
Extrapolation approximate outside the range of independent variable
of the given data set.
xx1 x2
y
7/30/2019 L08 Interpolation
6/20
Linear Interpolation
Connect two data points with a straight line
1 0 1 0
0 1 0
( ) ( ) ( ) ( )x f x f x f x
x x x x
=
Using similar triangles:
x0 x1
f(x0)
f(x1)
x
f1(x)
1 01 0 0
1 0
( ) ( )( ) ( ) ( )
f x f xf x f x x x
x x
= +
7/30/2019 L08 Interpolation
7/20
Quadratic Interpolation
Second-order polynomial interpolation using 3 data points
Convenient form:
2 0 1 0 2 0 1( ) ( ) ( )( )f x b b x x b x x x x= + + 1
2
2 0 1 1 0 2 2 0 1 2 0 2 1( )f x b b x b x b x b x x b xx b xx= + + +
2
2 0 1 2( )f x a a x a x= + +
where 0 0 1 0 2 0 1
1 1 2 0 2 1
2 2
a b b x b x xa b b x b x
a b
= +=
=
7/30/2019 L08 Interpolation
8/20
7/30/2019 L08 Interpolation
9/20
Example: Quadratic Interpolation atx= 2
0
1
2
1
4
6
x
x
x
==
=
0
1
2
( ) 0
( ) 1.3863
( ) 1.7918
f x
f x
f x
==
=
Solution:
0
1
2
0
1.3863 00.4621
4 1
1.7918 1.38630.46216 4 0.05187
6 1
b
b
b
=
= =
= =
7/30/2019 L08 Interpolation
10/20
Substitute b0, b1 and b2 into equation
f2(x) = 0 + 0.4621(x - 1) - 0.05187(x - 1)(x - 4)
which can be evaluated atx= 2 for
f2(2) = 0 + 0.4621(2 - 1) - 0.05187(2 - 1)(2 - 4)
f2(2) = 0.5658
7/30/2019 L08 Interpolation
11/20
Polynomial Interpolation
FindingPn-1(x) of degree n-1 that passes through n known data pairs
Pn-1(x) = c1xn-1 + c2x
n-2 + . . . + cn-1x + cn
Vandermonde Systems n pairs of (x, y)
n equations
n unknowns
Polynomial pass through each of data points
7/30/2019 L08 Interpolation
12/20
Example: Construct a quadratic interpolating function
y = c1x2 + c2x + c3
that pass through (x, y) support points (-2, -2), (-1, 1), and (2, -1)
Substitute known points into equation:
- 2 = c1 (-2)2 + c2 (-2) + c3
1 = c1 (-1)2 + c2 (-1) + c3
- 1 = c1 (2)2 + c2 (2) + c3
Rewritten in matrix form:
1
2
3
4 2 1 2
1 1 1 1
2 2 1 1
c
c
c
=
7/30/2019 L08 Interpolation
13/20
Vandermonde Matrix
2
1 1 1 1
2
2 2 2 2
2
3 3 3 3
1
1
1
x x c y
x x c y
x x c y
=
MATLAB statements:
>> 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
7/30/2019 L08 Interpolation
14/20
Polynomials Wiggle
2nd-order
3rd-order
4th-order
5th-order
y
x
xi 1 2 3 4 5 6 7 8 9 10
yi 3.5 3.0 2.5 2.0 1.5 -2.4 -2.8 -3.2 -3.6 -4.0
7/30/2019 L08 Interpolation
15/20
MATLABs Command Lines to Demonstrate Polynomials Wiggle
>> 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)
7/30/2019 L08 Interpolation
16/20
Piecewise Polynomial Interpolation
Using a set of lower degree interpolants on subinterval of
the whole domain= breakpoint or knot
y
x
Piecewise-linear interpolation
Piecewise-quadratic interpolation
Piecewise-cubic interpolation
f(x) and f(x) continuous at breakpoint = cubic spline
7/30/2019 L08 Interpolation
17/20
MATLABs Built-in Interpolation Functions
Funnction Description
interp1 1-D interpolation with piecewise polynomials.
interp2 2-D interpolation with nearest neighbor, bilinear,
or bicubic interpolants.
interp3 3-D interpolation with nearest neighbor, bilinear,
or bicubic interpolants.
interpft 1-D interpolation of uniformly spaced data using
Fourier Series (FFT).
interpn n-D extension of methods used by interp3.
spline 1-D interpolation with cubic-splines using
not-a-knot or fixed-slope end conditions.
7/30/2019 L08 Interpolation
18/20
interp1 Built-in Function
1-D interpolation with one of the following 4 methods:
1. Nearest-neighbor uses piecewise-constant function.
Interpolant discontinue at midpoint between knots
2. Linear interpolation uses piecewise-linear polynomials.
3. Cubic interpolation uses piecewise-cubic polynomials.
Interpolant and f(x) are continuous.
4. Spline interpolation uses cubic splines. This option performs
the same interpolation as built-in function spline.
7/30/2019 L08 Interpolation
19/20
How to use interp1 ?
>> yhat = interp1(y, xhat)
>> yhat = interp1(x, y, xhat)>> yhat = interp1(x, y, xhat, method)
where y = tabulated values to be interpolated.
x = independent values. If not given x=1:length(y).
xhat = values at which interpolant be evaluated.
method = nearest, linear, cubic orspline
7/30/2019 L08 Interpolation
20/20
>> 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);
or >> ys = spline(x, y, xhat);>> plot(x, y, o, xhat, ys);
Example: Interpolation with piecewise-polynomials
top related