0% found this document useful (0 votes)
57 views

L08 Interpolation

This document discusses various interpolation methods including linear, quadratic, and polynomial interpolation. Linear interpolation uses a straight line between two known data points to estimate unknown points. Quadratic interpolation uses a second-order polynomial to interpolate three known data points. Polynomial interpolation finds a polynomial of degree n-1 that passes through n known data pairs. Piecewise polynomial interpolation uses separate polynomial functions over different intervals of the domain. MATLAB functions like interp1, polyfit, and spline can perform various interpolation methods.

Uploaded by

Ace
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

L08 Interpolation

This document discusses various interpolation methods including linear, quadratic, and polynomial interpolation. Linear interpolation uses a straight line between two known data points to estimate unknown points. Quadratic interpolation uses a second-order polynomial to interpolate three known data points. Polynomial interpolation finds a polynomial of degree n-1 that passes through n known data pairs. Piecewise polynomial interpolation uses separate polynomial functions over different intervals of the domain. MATLAB functions like interp1, polyfit, and spline can perform various interpolation methods.

Uploaded by

Ace
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Numerical Methods for Civil Engineers

Lecture 8

Interpolation

Linear Interpolation
Quadratic Interpolation
Polynomial Interpolation
Piecewise Polynomial Interpolation
Mongkol JIRAVACHARADET

SURANAREE

INSTITUTE OF ENGINEERING

UNIVERSITY OF TECHNOLOGY

SCHOOL OF CIVIL ENGINEERING

Visual Interpolation

40
20
0

60

80
km/h

100
120

Vehicle speed is approximately 49 km/h

Interpolation between data points


Consider a set of xy data collected during an experiment.
We use interpolation technique to estimate y at x where theres no data.

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)

Determining coefficient a1, a2, . . . , an of basis function F(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
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

Linear, Quadratic, and Cubic Interpolations

2 points

3 points

Linear = 1st order

Quadratic = 2nd order

f(x) = a0 + a1x

f(x) = a0 + a1x + a2x2

4 points
Cubic = 3rd order
f(x) = a0 + a1x+ a2x2 + a3x3

General formula for an (n 1)th-order polynomial:


f(x) = a0 + a1x+ a2x2 ++ anxn-1

Linear Interpolation
Connect two data points with a straight line

y1

Using similar triangles:

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)

EXAMPLE : Linear & Quadratic Interpolation


Estimate ln(2) by using linear & quadratic interpolation
Linear interpolation from x1 = 1 to x2 = 6

f1(2) = 0 +

=
=
=
=

0
0.6932
1.3863
1.7918

1.7918 0
( 2 1) = 0.3584 t = 48 .3%
6 1

Linear interpolation from x1 = 1 to x2 = 4

f1(2) = 0 +

1.3863 0
( 2 1) = 0.4621 t = 33 .3%
4 1

Quadratic interpolation from x1 = 1 to x2 = 4 and x3 = 6

b0 = 0 ,

b1 =

1.3863 0
= 0.4621
4 1

1.7918 1.3863
0.4621
6
4

b3 =
= 0.05187
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 t = 18.4%
f2(x) = 0.05187x2 + 0.7251x 0.6696
2

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

Pn-1(x) = c1xn-1 + c2xn-2 + . . . + cn-1x + cn


Vandermonde Systems

Linear
= 1 : 2 pt.
Quadratic = 2 : 3 pt.
Cubic
= 3 : 4 pt.

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
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

MATLAB : polyfit and polyval Functions


Find y = c1x2 + c2x + c3 that pass through (x, y) support points
(-2, -2), (-1, 1), and (2, -1).
>> x = [-2 -1 2];
>> y = [-2 1 -1];
>> c = polyfit(x,y,2)
c =
-0.9167
0.2500

2.1667

We can then use the polyval function to perform an interpolation as in


Ex. To interpolate y at x = 1
>> polyval(c,1)
ans =
1.5000

Also, we can use the polyval function to plot the result as in


>> xhat = -3:0.1:3;
>> yhat = polyval(c,xhat);
>> plot(xhat,yhat,x,y,'o')
4
2
0
-2
-4
-6
-8
-3

-2

-1

Polynomials Wiggle
2nd-order
3rd-order

4th-order
5th-order

x
xi

yi

3.5 3.0 2.5

10

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

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.

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

= tabulated values to be interpolated.

= independent values. If not given x=1:length(y).

xhat

= 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);

EXAMPLE : Linear interpolation by interp1 function


Estimate the value of y when x is equal to 3.5
x

15

10

>> x = 0:5;

15

>> y = [15 10 9 6 2 0];


>> interp1(x,y,3.5)

10

ans =
4

Plot Graph:
>> xhat = 0:0.2:5;

0
0

>> yhat = interp1(x,y,xhat);


>> plot(x,y,'bo-',xhat,yhat,'rx')

You might also like