Chapter 14Chapter 14Curve Fitting : Polynomial Curve Fitting : Polynomial
Interpolation Interpolation
Gab Byung ChaeGab Byung Chae
You’ve got a problemYou’ve got a problemEstimation of intermediate values between Estimation of intermediate values between data pointsdata points
14.1 Introduction to 14.1 Introduction to Interpolation Interpolation
(2) MATLAB Function : polyfit and polyval(2) MATLAB Function : polyfit and polyval
Only one straight line that connects two points.Only one straight line that connects two points.
Only one parabola connects a set of three points.Only one parabola connects a set of three points.
14.1.1 Determining polynomial 14.1.1 Determining polynomial coefficientscoefficients
Interpolating polynomial ofInterpolating polynomial of
solvesolve
Ill-conditioned systemIll-conditioned system
(Vandermonde matrices)(Vandermonde matrices)
),(),,(),,( 332211 yxyxyx
322
1)( pxpxpxp
33322
313
23222
212
13122
111
)(
)(
)(
ypxpxpxp
ypxpxpxp
ypxpxpxp
3
2
1
3
2
1
32
3
22
2
12
1
1
1
1
y
y
y
p
p
p
xx
xx
xx
4.1.2 MATLAB Functions 4.1.2 MATLAB Functions
>> format long >> format long
>> T = [300 400 500];>> T = [300 400 500];
>> density = [0.616 0.525 0.457]; >> density = [0.616 0.525 0.457];
>> p = polyfit(T, density,2)>> p = polyfit(T, density,2)
>> d = polyval(p,350)>> d = polyval(p,350)
14.2 Newton Interpolating 14.2 Newton Interpolating polynomialpolynomial
Graphical depiction of linear interpolation. The shaded areas indicate the similar triangles used to derive the Newton linear-interpolation formula [Eq. (14.5)].
Figure 14.2
FDD = finite divided difference
The second FDD
Two linear interpolations to estimate ln 2. Note how the smaller interval provides a better estimate.
Figure 14.3
Example 14.3Example 14.3Problem : f(x) = ln xProblem : f(x) = ln x– xx11 = 1 f(x = 1 f(x11) =0 ) =0
– xx22 = 4 f(x = 4 f(x22) = 1.386294 ) = 1.386294
– xx33 = 6 f(x = 6 f(x33) = 1.791759 ) = 1.791759
Solution : Solution : – bb1 1 = 0 = 0
– bb2 2 = (1.386294 – 0) /(4-1) = 0.4620981= (1.386294 – 0) /(4-1) = 0.4620981
– bb3 3 = =
ff22(x) = 0 + 0.4620981(x-1) – 0.0518731(x-1)(x-4)(x) = 0 + 0.4620981(x-1) – 0.0518731(x-1)(x-4)
0518731.016
4620981.046
386294.1791759.1
The use of quadratic interpolation to estimate ln 2. The linear interpolation from x = 1 to 4 is also included for comparison.
Figure 14.4
Newton InterpolationNewton Interpolation Divided-difference table :Divided-difference table :
Adding (3,14) and (4,22)Adding (3,14) and (4,22)
82
302
28
1)2(2
)1(320
1)2(0
42
42
23
23
13
12
12
12
3
1
2
1
1
1
xx
yy
xx
dd
xx
yy
xx
ddddddd
xx
dddd
xx
yydyx
ii
iii
ii
iii
ii
iiiii
<- a1
<- a2
<- a3
Graphical depiction of the recursive nature of finite divided differences. This representation is referred to as a
divided difference table.
Figure 14.5
Example 14.4 Example 14.4 Problem : f(x) = ln xProblem : f(x) = ln x
– xx11 = 1 f(x = 1 f(x11) =0 ) =0
– xx22 = 4 f(x = 4 f(x22) = 1.386294 ) = 1.386294
– xx33 = 6 f(x = 6 f(x33) = 1.791759 ) = 1.791759
– xx44 = 5 f(x = 5 f(x44) = 1.609438) = 1.609438
ff33(x) = b1 + b2(x-x1)+ (x) = b1 + b2(x-x1)+
b3(x-x1)(x-x2)+b4(x-x1)(x- x2)(x-x3)b3(x-x1)(x-x2)+b4(x-x1)(x- x2)(x-x3)
Solution : Solution :
– bb1 1 = f(x= f(x11) = 0 ) = 0
– bb2 2 = f[x= f[x2 2 , x, x11] = (1.386294 – 0) /(4-1) = 0.4620981] = (1.386294 – 0) /(4-1) = 0.4620981
– f[xf[x33,x,x22] = (1.791759 – 1.386294) /(6-4) = 0.2027326] = (1.791759 – 1.386294) /(6-4) = 0.2027326
– f[xf[x44,x,x33] = (1.609438 – 1.791759) /(5-6) = 0.1823216] = (1.609438 – 1.791759) /(5-6) = 0.1823216
– bb3 3 = f[x= f[x3 3 ,x,x2 2 ,x,x11] = (0.2027326 – 0.4620981) /(6-1) ] = (0.2027326 – 0.4620981) /(6-1)
= -0.05187311= -0.05187311
– f[xf[x4 4 ,x,x3 3 ,x,x22] = (0.1823216 – 0.2027326) /(5-4) ] = (0.1823216 – 0.2027326) /(5-4)
= -0.02041100= -0.02041100
– bb4 4 =f[x=f[x4 4 ,x,x3 3 ,x,x22,x,x11] = (-0.02041100 + 0.05187311) /(5-1) ] = (-0.02041100 + 0.05187311) /(5-1)
= 0.007865529= 0.007865529
ff33(x) = 0 + 0.4620981(x-1)(x) = 0 + 0.4620981(x-1)
- 0.05187311(x-1)(x-4) - 0.05187311(x-1)(x-4)
+ 0.007865529(x-1)(x- 4)(x-6)+ 0.007865529(x-1)(x- 4)(x-6)
function yint = Newtint(x,y,xx)function yint = Newtint(x,y,xx)
% yint = Newtint(x,y,xx):% yint = Newtint(x,y,xx):
% Newton interpolation. Uses an (n - 1)-order Newton% Newton interpolation. Uses an (n - 1)-order Newton
% interpolating polynomial based on n data points (x, y)% interpolating polynomial based on n data points (x, y)
% to determine a value of the dependent variable (yint)% to determine a value of the dependent variable (yint)
% at a given value of the independent variable, xx.% at a given value of the independent variable, xx.
% input:% input:
% x = independent variable% x = independent variable
% y = dependent variable% y = dependent variable
% xx = value of independent variable at which% xx = value of independent variable at which
% interpolation is calculated% interpolation is calculated
% output:% output:
% yint = interpolated value of dependent variable% yint = interpolated value of dependent variable
% compute the finite divided differences in the form of a% compute the finite divided differences in the form of a% difference table% difference tablen = length(x);n = length(x);if length(y)~=n, error('x and y must be same length'); endif length(y)~=n, error('x and y must be same length'); endb = zeros(n,n);b = zeros(n,n);% assign dependent variables to the first column of b.% assign dependent variables to the first column of b.b(:,1) = y(:); % the (:) ensures that y is a column vector.b(:,1) = y(:); % the (:) ensures that y is a column vector.for j = 2:nfor j = 2:n for i = 1:n-j+1for i = 1:n-j+1 b(i,j) = (b(i+1,j-1)-b(i,j-1))/(x(i+j-1)-x(i));b(i,j) = (b(i+1,j-1)-b(i,j-1))/(x(i+j-1)-x(i)); endendendend% use the finite divided differences to interpolate % use the finite divided differences to interpolate xt = 1;xt = 1;yint = b(1,1);yint = b(1,1);for j = 1:n-1for j = 1:n-1 xt = xt*(xx-x(j));xt = xt*(xx-x(j)); yint = yint+b(1,j+1)*xt;yint = yint+b(1,j+1)*xt;endend
14.3 Lagrange Interpolating 14.3 Lagrange Interpolating polynomialpolynomial
weight coefficients
The first orderThe first order
Lagrange interpolating polynomialLagrange interpolating polynomial
The n-th orderThe n-th order
Lagrange interpolating polynomialLagrange interpolating polynomial
The second orderThe second order
Lagrange interpolating polynomialLagrange interpolating polynomial
Example 14.5Example 14.5Problem : Use a Lagrange interpolating Problem : Use a Lagrange interpolating polynomial of the first and second order to polynomial of the first and second order to evaluate the density of unused motor oil at T = evaluate the density of unused motor oil at T = 1515o o CC– xx11 = 0 f(x = 0 f(x11) = 3.85 ) = 3.85
– xx22 = 20 f(x = 20 f(x22) = 0.800 ) = 0.800
– xx33 = 40 f(x = 40 f(x33) = 0.212) = 0.212
Solution : Solution : First-order at x=15:First-order at x=15:
– ff11(x)= (x)=
Second-order :Second-order :
– ff22(x)= (x)=
5625.1800.0020
01585.3
200
2015
3316875.1212.0)2040)(040(
)2015)(015(
800.0)4020)(020(
)4015)(015(85.3
)400)(200(
)4015)(2015(
function yint = Lagrange(x,y,xx)% yint = Lagrange(x,y,xx):% Lagrange interpolation. Uses an (n - 1)-order Lagrange% interpolating polynomial based on n data points (x, y)% to determine a value of the dependent variable (yint)% at a given value of the independent variable, xx.% input:% x = independent variable% y = dependent variable% xx = value of independent variable at which the% interpolation is calculated% output:% yint = interpolated value of dependent variable
14.3.1 MATLAB M-file:Lagrange
n = length(x);if length(y)~=n, error('x and y must be same length'); ends = 0;for i = 1:n product = y(i); for j = 1:n if i ~= j product = product*(xx-x(j))/(x(i)-x(j)); end end s = s+product;endyint = s;
14.4 Inverse Interpolation 14.4 Inverse Interpolation (1,1) (3,3) (5,5) -> ((1,1) (3,3) (5,5) -> (??,2),2)
Polynomial interpolationPolynomial interpolation– Determine (n-1)-th order polynomial Determine (n-1)-th order polynomial p(x)p(x)
– solve solve p(t) = 2 p(t) = 2
Ex : (2, 0.5), (3,0.3333), and (4,0.25) Ex : (2, 0.5), (3,0.3333), and (4,0.25) – Find x so that f(x)=0.3Find x so that f(x)=0.3
– ff2 2 (x) = 0.041667x(x) = 0.041667x2 2 – 0.375x+1.08333– 0.375x+1.08333
– Solve 0.3 = 0.041667xSolve 0.3 = 0.041667x2 2 – 0.375x+1.08333 for x– 0.375x+1.08333 for x– x = 5.704158 or 3.295842x = 5.704158 or 3.295842
Extrapolation is the process of estimating Extrapolation is the process of estimating a value of f(x) that lies outside the range a value of f(x) that lies outside the range of the known base points. of the known base points.
14.5.1 Extrapolation14.5.1 Extrapolation
Illustration of the possible divergence of an extrapolated prediction. The extrapolation is based on fitting a parabola through the first three known points.
Figure 14.10
Example 14.6Example 14.6
Problem : Fit a seventh-order polynomial Problem : Fit a seventh-order polynomial to the first 8 points (1920 to 1990). Use it to the first 8 points (1920 to 1990). Use it to compute the population in 2000 by to compute the population in 2000 by extrapolationand compare your prediction extrapolationand compare your prediction with the actual result.with the actual result.
Solution : Solution : >> t = [1920 :10:1990];>> t = [1920 :10:1990];
>> pop = [106.46 123.08 132.12 152.27 180.67 205.05 227.23 249.46];>> pop = [106.46 123.08 132.12 152.27 180.67 205.05 227.23 249.46]; >> p = polyfit(t, pop, 7)>> p = polyfit(t, pop, 7)
Warning message ……Warning message ……
>> ts = (t-1955)/35;>> ts = (t-1955)/35;
>> P = polyfit(ts, pop, 7);>> P = polyfit(ts, pop, 7);
>> polyval(p, (2000-1955)/35)>> polyval(p, (2000-1955)/35)
>>tt=linspace(1920,2000);>>tt=linspace(1920,2000);
>>pp=polyval(p, (tt-1955)/35);>>pp=polyval(p, (tt-1955)/35);
Plot(t,pop, ‘o’, tt, pp)Plot(t,pop, ‘o’, tt, pp)
Use of a seventh-order polynomial to make a prediction of U.S. population in 2000 based on data from 1920
through 1990.
Figure 14.11
14.5.2 Oscillations14.5.2 OscillationsDangers of higher-order polynomial Dangers of higher-order polynomial interpolationinterpolation
Ex : Ringe’s functionEx : Ringe’s function
2251
1)(
xxf
Comparison of Runge’s function (dashed line) with a fourth-order polynomial fit to 5 points sampled from the function.
Figure 14.12
Comparison of Runge’s function (dashed line) with a tenth-order polynomial fit to 11 points sampled from the function.
Figure 14.13