Numerical Methods for Civil Engineers
Lecture 8
Interpolation
- Linear Interpolation - Quadratic Interpolation - Polynomial Interpolation - Piecewise Polynomial Interpolation
By Mongkol JIRAVACHARADET School of Civil Engineering Suranaree University of Technology
Visual Interpolation
40 20 0
60
80 km/h 100 120
Vehicle speed is approximately 49 km/h
BASIC IDEAS From the known data ( xi , yi ), interpolate y = F ( x) for x xi 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 + a2 x + a3 x2 + . . . + an xn-1
Interpolation v.s. Curve Fitting y known data curve fit interpolation
x Curve fitting: fit function & data not exactly agree Interpolation: function passes exactly through known data
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. y
x1
x2
Linear Interpolation Connect two data points with a straight line Using similar triangles: f (x1) f 1(x) f (x0) x0 x x1
f1 ( x ) f ( x0 ) f ( x1 ) f ( x0 ) = x x0 x1 x0
f ( x1 ) f ( x0 ) f1 ( x) = f ( x0 ) + ( x x0 ) x1 x0
Quadratic Interpolation Second-order polynomial interpolation using 3 data points Convenient form:
f 2 ( x) = b0 + b1 ( x x0 ) + b2 ( x x0 )( x x1 )
2
f 2 ( x) = b0 + b1 x b1 x0 + b2 x + b2 x0 x1 b2 xx0 b2 xx1 f 2 ( x) = a0 + a1 x + a2 x 2
where
a0 = b0 b1 x0 + b2 x0 x1 a1 = b1 b2 x0 b2 x1 a2 = b2
Substitute x = x0 into Eq.(1)
b0 = f ( x0 )
Substitute x = x1 into Eq.(1)
f ( x1 ) f ( x0 ) b1 = x1 x0
Substitute x = x2 into Eq.(1)
f ( x2 ) f ( x1 ) f ( x1 ) f ( x0 ) x2 x1 x1 x0 b2 = x2 x0
Example: Quadratic Interpolation at x = 2
x0 = 1 x1 = 4 x2 = 6
Solution:
f ( x0 ) = 0 f ( x1 ) = 1.3863 f ( x2 ) = 1.7918
b0 = 0 1.3863 0 b1 = = 0.4621 4 1 1.7918 1.3863 0.4621 64 = 0.05187 b2 = 6 1
Substitute b0, b1 and b2 into equation f2(x) = 0 + 0.4621(x - 1) - 0.05187(x - 1)(x - 4) which can be evaluated at x = 2 for f2(2) = 0 + 0.4621(2 - 1) - 0.05187(2 - 1)(2 - 4) f2(2) = 0.5658
Polynomial Interpolation Finding Pn-1(x) of degree n-1 that passes through n known data pairs Pn-1(x) = c1xn-1 + c2xn-2 + . . . + cn-1x + cn Vandermonde Systems n pairs of (x, y) n equations n unknowns
Polynomial pass through each of data points
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: 4 2 1 1 2 2
1 c1 2 1 c2 = 1 1 c3 1
Vandermonde Matrix
x12 2 x2 2 x3
x1 x2 x3
1 c1 y1 1 c2 = y2 1 c3 y3
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
Polynomials Wiggle
2nd-order 3rd-order
4th-order 5th-order
x xi yi 1 2 3 4 5 6 7 8 9 10
3.5 3.0 2.5
2.0 1.5 -2.4 -2.8 -3.2 -3.6 -4.0
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)
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
MATLABs Built-in Interpolation Functions
Funnction interp1 interp2 Description 1-D interpolation with piecewise polynomials. 2-D interpolation with nearest neighbor, bilinear, or bicubic interpolants. 3-D interpolation with nearest neighbor, bilinear, or bicubic interpolants. interpft 1-D interpolation of uniformly spaced data using Fourier Series (FFT). n-D extension of methods used by interp3. 1-D interpolation with cubic-splines using not-a-knot or fixed-slope end conditions.
interp3
interpn spline
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.
How to use interp1 ? >> yhat = interp1(y, xhat) >> yhat = interp1(x, y, xhat) >> yhat = interp1(x, y, xhat, method)
where y x xhat = tabulated values to be interpolated. = independent values. If not given x=1:length(y). = values at which interpolant be evaluated.
method = nearest, linear, cubic or spline
Example: Interpolation with piecewise-polynomials
>> >> >> >> >> >> >> >> >> >> or >> 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);