1
Polynomial Interpolation Polynomial Interpolation ((多項式內插法多項式內插法 ))
Interpolation vs. Extrapolation
• Interpolation ( 內插法 )• Data to be found are within the range of observed data• Used to estimate values between data points• 與迴歸分析的差異
• Goes through data points, no error at data points
• The most common method is the polynomial interpolation
• Extrapolation (外插法 )• Data to be found are beyond the range of observation data (not
reliable)2
Same data points, different curve fitting
Polynomial Interpolation
Given n data points, fit an (n-1)th-order polynomial through them
Use polynomial interpolation to determine ai’s
For consistency with MATLAB, use
3
1nn
2321 xaxaxaaxf ...
n1n2n
21n
1 pxpxpxpxf )(
Interpolating Polynomials
4
First-order Second-order Third-order
Coefficients of an Interpolating Polynomial
Newton and Lagrange polynomials are well-suited for determining values between points However, they do not provide a convenient polynomial of
conventional form
Use n data points to determine n coefficients
5
nn1n2n
n21n
n1n
n21n2n
221n
212
n11n2n
121n
111
pxpxpxpxf
pxpxpxpxf
pxpxpxpxf
)(
)(
)(
n1n2n
21n
1 pxpxpxpxf )(
Coefficients of an Interpolating Polynomial
Can be solved with any matrix method, but inefficient
There are more efficient methods to find p’s The above equations are notoriously ill-conditioned (病態條
件 ), especially for large n Limit yourself to lower-order polynomials
6
)(
)(
)(
)(
n
3
2
1
n
3
2
1
n2n
n1n
n
32n
31n
3
22n
21n
2
12n
11n
1
xf
xf
xf
xf
p
p
p
p
1xxx
1xxx
1xxx
1xxx
Polynomial Coefficients
Example – use last four points of Table 14.1
7
432
23
141
432
23
131
432
23
121
432
23
111
432
23
1
)500()500()500(457.0)( ; 500
)400()400()400(525.0)( ; 400
)300()300()300(616.0)( ; 300
)250()250()250(675.0)( ; 250
)(
ppppxfx
ppppxfx
ppppxfx
ppppxfx
pxpxpxpxf
4570
5250
6160
6750
p
p
p
p
1500500500
1400400400
1300300300
1250250250
4
3
2
1
23
23
23
23
.
.
.
.
)()(
)()(
)()(
)()(
Vandermonde Matrices ( 凡德芒矩陣 )
Vandermonde Matrices
8
>> A=[250^3 250^2 250 1; 300^3 300^2 300 1; 400^3 400^2 400 1; 500^3 500^2 500 1]
A =
15625000 62500 250 1
27000000 90000 300 1
64000000 160000 400 1
125000000 250000 500 1
>> b=[0.675; 0.616; 0.525; 0.457]
b =
0.6750
0.6160
0.5250
0.4570
>> format long
>> p = A\b
p =
-0.00000000260000
0.00000427000000
-0.00293700000000
1.18300000000000
>> cond(A)
ans =
9.306535523991324e+009ill-conditioned matrix
18300000001x00293700000
x00000427000x00000000260xf 23
..
..)(
Newton Interpolation
Use Newton’s “divided differences” of the functional values
The lower order coefficients bi do not change when the order of interpolation is increased
Easy to add more data points and try a higher order polynomial
9
)())((
))(()()(
1n21n
2131211n
xxxxxxb
xxxxbxxbbxf
Newton Linear Interpolation
Start with linear interpolation: f1(x) = b1 + b2 (x - x1) Similar triangles
10
)()()(
)()(
)()()()(
112
1211
12
12
1
11
xxxx
xfxfxfxf
xx
xfxf
xx
xfxf
Newton Linear Interpolation
Newton linear interpolation formula
Example 1: interpolate e2 using e1 and e5
Example 2: Interpolate e2 using e1.5 and e2.5
11
112
1211 xx
xx
xfxfxfxf
4233614
71832411487183212
15
eee2f
151
1 ...
.
33218501
4817418251248174512
5152
eee2f
515251
1 ....
....
...
3891.7Exact 2 e
Newton Linear Interpolation
12
Small interval x provides a better estimate
Linear estimates of ln(2)
Logarithmic function
Accuracy of Interpolation
Quadratic Interpolation ( 二次內插 )
Quadratic interpolation – need three points Use the parabola ( 拋物線 )
This is the same as
14
2131212 xxxxbxxbbxf
33
231322
2131211
23212
ba
xbxbba
xxbxbba
xaxaaxf )(
where
Quadratic Interpolation
To get coefficients b’s 1) Set x = x1, get b1 = f(x1)
2) Use b1 and set x = x2 to get b2
15
1121113112112 xfbxxxxbxxbbxf
12
122
222123122122
xx
xfxfb
xfxxxxbxxbxfxf
2131212 xxxxbxxbbxf
Quadratic Interpolation
3) Use b1 and b2, and set x = x3 to get b3
16
13
12
12
23
23
3
3231331312
12132
xx
xx
xfxf
xx
xfxf
b
xfxxxxbxxxx
xfxfxfxf
b1 is a constant (0th order)
b2 gives slope (finite difference)
b3 gives curvature (difference of finite differences)
Hand Calculation Example
例 : interpolate e2 using e1, e3, and e5
17
41148xf 5x
08620xf 3x
71832xf 1x
33
22
11
.
.
.
8701315
13
ee
35
ee
b ;6836813
eeb ;71832b
1335
3
13
21 ...
468123212870131268368718322f2 .*.*..
38917e Exact 2 .
Hand Calculation Example — Different xi
例 : interpolate e2 using e1, e1.5, and e2.5
182512xf 52x
48174xf 51x
71832xf 1x
33
22
11
..
..
.
78272152
151
ee
5152
ee
b ;52683151
eeb ;71832b
1515152
3
151
21 ..
.....
.
...
.
6365751212782721252683718322f2 ..*.*..
38917e Exact 2 .
Quadratic Interpolation
Quadratic Interpolation
Order of Interpolation
Linear, quadratic and cubic estimates of ln(2)
21Higher-order interpolation improves the estimate
Logarithmic function
Newton’s Interpolating Polynomials
General form for Newton’s interpolating polynomials
Bracketed functions are finite differences
22
xxxxxxxxb
xxxxbxxbbxf
1n321n
2131211n
121nnn
1233
122
11
xxxxfb
xxxfb
xxfb
xfb
,,,,
,,
,
Newton’s Divided Differences
First finite difference ( 一次有限分割差分 )
Second finite difference (二次有限分割差分 )
The difference of two finite differences
23
ji
jiji xx
xfxfxxf
,
ki
kjjikji xx
xxfxxfxxxf
,,
,,
Newton’s Divided Differences
The nth finite difference
24
1n
122n1n231nn121nn xx
xxxxfxxxxfxxxxf
,,,,,,,,,,,,
An iterative procedure
1. Evaluate all first-order finite differences; save f (x1) for b1
2. Evaluate second-order from firsts; save f [x2, x1] for b2
3. Continue to nth-order, saving needed ones
Newton Interpolation
No need to solve a system of simultaneous equations Spacing may be non-uniform and xi may be in arbitrary
order 25
15
12342345123455
14
12323412344
13
12231233
12
12122
111
1n1n2131211n
xx
xx xxfxx xxfxx xxxfb
xx
xx xfxx xfxx xxfb
xx
x xfx xfx xxfb
xx
xfxfx xfb
xfxfb
xxxxbxxxxbxxbbxf
,,,,,,,,,,
,,,,,,,
,,,,
,
)(
)()())(()()(
)(
,)(
,,,)(
,,,,,,)(
,,,,,,,,,,)(
,,,,,,,,,,)(
,,,,,,,)(
66
5655
4564544
34563453433
2345623452342322
1234512341231211
i4ii3ii1i2ii1iiii
xfx6
xxfxfx5
xxxfxxfxfx4
xxxxfxxxfxxfxfx3
xxxxxfxxxxfxxxfxxfxfx2
xxxxxfxxxxfxxxfxxfxfx1
xxfxxfxxxfxxfxfyxi
1n
122n1n131nn121nnn
1n1n2131211n
xx
x xxxfx xxxfx xxxfb
xxxxbxxxxbxxbbxf
,,,,,,,,,,
)()())(()()(
,,
Use the top element of each column (= bn) to evaluate the interpolated
functional value f(x)
First Second Third Fourth
Newton Interpolation
Relative error decreases with increasing order of the interpolating polynomial
Error is also sensitive to the position and sequence of the original data (x1 , x2 , x3 , x4 ,…, xn)
Percentage Relative Error
)3,4,1,0()x,x,x,(x using 2xat ef(x) Estimate 4321x
93618754x1xx57197011xx8937523x718282112f
22407121xx8937523x718282112f
3
2
.))((.)(..)(
.)(..)(
085542034
43x
5126134598155443
13x 14x
60966282932917718282212
03x 04x 01x
571970189375237182821000000101
xxxxfxxxfxxfxfxi i1i2i3ii1i2ii1iii
.
..
...
....
,,,,,,)(
Hand Calculations: Newton Interpolation
Error Estimate
Error for Newton’s interpolating polynomial Estimate from
The leading term in the truncated polynomial Similar to the truncation error in a Taylor series
29
n2111nn1nn xxxxxxxxxxfR ,,,,
30
function yint = Newtint(x,y,xx)% Newton interpolation. Uses an (n - 1)-order Newton% 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% interpolation is calculated% output:% yint = interpolated value of dependent variable % compute the finite divided differences in the form of a% difference tablen = length(x);if length(y)~=n, error('x and y must be same length'); endb = zeros(n,n);% assign dependent variables to the first column of b.b(:,1) = y(:); % the (:) ensures that y is a column vector.for j = 2:n for i = 1:n-j+1 b(i,j) = (b(i+1,j-1)-b(i,j-1))/(x(i+j-1)-x(i)); endend% use the finite divided differences to interpolate xt = 1;yint = b(1,1);for j = 1:n-1 xt = xt*(xx-x(j)); yint = yint+b(1,j+1)*xt;end
31
Modified M-File: yint may be evaluated at multiple pointsfunction [b,yint] = Newtint2(x,y,xx)% yint = Newtint(x,y,xx):% Newton interpolation. Uses an (n - 1)-order Newton% 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. % compute the finite divided differences in the form of a% difference tablen = length(x);if length(y)~=n, error('x and y must be same length'); endb = zeros(n,n);% assign dependent variables to the first column of b.b(:,1) = y(:); % the (:) ensures that y is a column vector.for j = 2:n for i = 1:n-j+1 b(i,j) = (b(i+1,j-1)-b(i,j-1))/(x(i+j-1)-x(i)); endend% use the finite divided differences to interpolate for k = 1:length(xx) xt = 1; yint(k) = b(1,1); for j = 1:n-1 xt = xt*(xx(k)-x(j)); yint(k) = yint(k)+b(1,j+1)*xt; endend
>> x = [0 1 4 3 1.5 2.5]x = 0 1.0000 4.0000 3.0000 1.5000 2.5000>> y = exp(x)y = 1.0000 2.7183 54.5982 20.0855 4.4817 12.1825>> xx = 0:0.1:4; yy=exp(xx);>> [b, yint] = Newtint2(x, y, xx)b = 1.0000 1.7183 3.8938 1.5720 0.3312 0.0697 2.7183 17.2933 8.6097 2.0687 0.5055 0 54.5982 34.5126 9.6440 2.8270 0 0 20.0855 10.4026 5.4035 0 0 0 4.4817 7.7008 0 0 0 0 12.1825 0 0 0 0 0yint = Columns 1 through 8 1.0000 1.1355 1.2670 1.4000 1.5394 1.6893 1.8536 2.0356 Columns 9 through 16 2.2384 2.4651 2.7183 3.0008 3.3153 3.6649 4.0525 4.4817 Columns 17 through 24 4.9561 5.4801 6.0584 6.6964 7.4003 8.1770 9.0343 9.9809 Columns 25 through 32 11.0266 12.1825 13.4606 14.8744 16.4387 18.1698 20.0855 22.2054 Columns 33 through 40 24.5506 27.1441 30.0107 33.1774 36.6729 40.5284 44.7770 49.4543 Column 41 54.5982>> H = plot(x,y,'mo',xx,yy,'r',xx,yint,'bx'); set(H,'LineWidth',3);
Newton Interpolating Polynomial
Table
Coefficients of Newton interpolating
polynomial
f(x) = ex, Interpolation at [0 1 4 3 1.5 2.5]
Newton Interpolating Polynomial
Lagrange Interpolating Polynomials
Give the same result as the Newton’s polynomials, but different approach
35
)())(())((
)())(())((
)(
)()(
)()()()()()()()()(
1121
1121
1
122111
niiiiiii
nii
ii
in
ijj ji
ji
i
n
iinnn
xxxxxxxxxx
xxxxxxxxxx
xP
xP
xx
xxxL
xfxLxfxLxfxLxfxLxf
ijji
ji
ii
iiii
xL
0xL ij
1xP
xPxL i ;j
Note
)(
)(;
)(
)()(
:
Lagrange Interpolation
1st-order Lagrange polynomial
Second-order Lagrange polynomial
Third-order Lagrange polynomial
36
)()()()()()( 212
11
21
222111 xf
xx
xxxf
xx
xxxfxLxfLxf
)())((
))(()(
))((
))(()(
))((
))(()( 3
2313
212
3212
311
3121
322 xf
xxxx
xxxxxf
xxxx
xxxxxf
xxxx
xxxxxf
)())()((
))()(()(
))()((
))()((
)())()((
))()(()(
))()((
))()(()(
4342414
3213
432313
421
2423212
4311
413121
4324
xfxxxxxx
xxxxxxxf
xxxxxx
xxxxxx
xfxxxxxx
xxxxxxxf
xxxxxx
xxxxxxxf
Linear Lagrange Interpolation
Both L1(x) and L2(x) are straight lines37
)()()()()()( 212
11
21
222111 xf
xx
xxxf
xx
xxxfxLxfLxf
L2(x)f(x2)
L3(x)f(x3)L1(x)f(x1)
x1 x2 x3
Quadratic Lagrange Interpolation
First-Order Lagrange Interpolation
39
f(x) = ex
Interpolation at [0 4]
Second-Order Lagrange Interpolation
40
f(x) = ex, Interpolation at [0 1 4]
Third-Order Lagrange Interpolation
41
f(x) = ex, Interpolation at [0 1 4 3]
例: Lagrange Interpolation
Estimate f(x) = exp(x) at x = 2 using (x0, x1, x2, x3 ) = (0,1,4,3) Second-order: (x1, x2, x3 ) = (0,1,4)
Third order: (x1, x2, x3, x4 ) = (0,1,4,3)
42
224067125982541404
1202718281
4101
420201
4010
42122f
4f1404
1x0x1f
4101
4x0x0f
4010
4x1xxf
2
2
.).())((
))(().(
))((
))(().(
))((
))(()(
)())((
))(()(
))((
))(()(
))((
))(()(
936187508554206
4598254
12
2718282
6
401
12
22f
3f431303
4x1x0x4f
341404
3x1x0x
1f314101
3x4x0x0f
304010
3x4x1xxf
3
3
.).().().().()(
)())()((
))()(()(
))()((
))()((
)())()((
))()(()(
))()((
))()(()(
43
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 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;
M-file 執行例
給定不同溫度 t之空氣密度 d如下 t1 = -40,d(t1) = 1.52
t2 = 0, d(t2) = 1.29t3 = 20, d(t3) = 1.2t4 = 50, d(t4) = 1.09
求 15℃時的空氣密度
44
Coefficients of Lagrange Polynomial
Save the coefficients of Lagrange polynomial Evaluate the interpolated values at multiple locations
45
Evaluation of Interpolated Values
46
» x=[0 4]x = 0 4» y=exp(x)y = 1.0000 54.5982» c=Lagrange_coef(x,y)c = -0.2500 13.6495» t=2; p=Lagrange_eval(t,x,c)p = 27.7991
» x=[0 1 4 3]x = 0 1 4 3» y=exp(x)y = 1.0000 2.7183 54.5982 20.0855» c=Lagrange_coef(x,y)c = -0.0833 0.4530 4.5498 -3.3476» t=2; p=Lagrange_eval(t,x,c)p = 5.9362
» x=[0 1 4]x = 0 1 4» y=exp(x)y = 1.0000 2.7183 54.5982» c=Lagrange_coef(x,y)c = 0.2500 -0.9061 4.5498» t=2; p=Lagrange_eval(t,x,c)p = 12.2241
First-order Second-order
Third-order
Exact solution
e2 = 7.389056
Lagrange Interpolation
Very convenient for the same abscissas ( 橫座標 ) but different yi (e.g., measurements always taken using the same independent variables xi)
Lk(x) needs to be computed only once
However, less convenient when additional data may be added
48
1n1nnn2211
nn2211
nn2211
y,x, y,x,, y,x, y,x :2new
z,x,, z,x, z,x :1new
y,x,, y,x, y,x:original
Inverse Interpolation (反內插 )
Given x’s and f(x)’s – interpolation enables us to obtain new f(x) from new x
What about new x from new f(x)? Example: f(x) = 1/x
49
Switch x and f(x) and do new interpolation. However, non-uniform spacing in [x vs. f(x)] often leads to oscillations (振盪、擺盪 ) in the resulting interpolating polynomial (橫軸上數值分佈間隔不均常使得多項式內插造成振盪 )
Fit an nth-order polynomial to the original data [f(x) vs. x], then use root-finding techniques to find x. ( 求根 )
反內插求解例
給定若干數據點 (1,1) 、 (2,0.5) 、 (3,0.3333) 、(4,0.25) 、 (5,0.2) 、 (6,0.1667) 、 (7,0.1429) ,求f(x)=0.3 的 x值 配適二次多項式到三個點 (2,0.5) 、 (3,0.3333) 、 (4,0.25) ,求
f(x)=0.3 的 x值 f2(x) = 0.041667x2-0.375x+1.08333 (why?)
0.3 = 0.041667x2-0.375x+1.08333 ,求根 求得根為 5.704158 或 3.295842 哪一個為所求?
50
Extrapolation ( 外插 )
估計在已知基準點 x1, x2,…, xn範圍之外的 f(x)值的程序 外差的開放特性代
表將進入未知領域,因此將曲線拓展到超過已知的區域
可能與真實的曲線偏離,和預測不同
使用時應特別小心
51
此例說明外插可能引發的偏離情形;此例之外插是基於前三個已知點所擬合的拋物線配適
此例說明外插可能引發的偏離情形;此例之外插是基於前三個已知點所擬合的拋物線配適
例:外插的危險性
美國 1920—2000 年的人口數
以七階的多項式來配適前 8個點 (1920—1990) ,使用所得結果外插計算 2000 年的人口數
Reasonable to use interpolation, but not the extrapolation52
53
Oscillations ( 振盪 )
Higher-order polynomials tend to be ill-conditioned Tend to be highly sensitive to round-off error
例:倫基函數 (Runger’s functions)
54
2251
1)(
xxf
Runge’s function
Difficulties with polynomial interpolation: Humped or flat data
Runge’s Function
55
» x2=-1:0.2:1x2 = Columns 1 through 7 -1.0000 -0.8000 -0.6000 -0.4000 -0.2000 0 0.2000 Columns 8 through 11 0.4000 0.6000 0.8000 1.0000» y2=1./(1+25*x2.^2)y2 = Columns 1 through 7 0.0385 0.0588 0.1000 0.2000 0.5000 1.0000 0.5000 Columns 8 through 11 0.2000 0.1000 0.0588 0.0385» c=Lagrange_coef(x2,y2)c = Columns 1 through 7 0.1035 -1.5830 12.1102 -64.5875 282.5702 -678.1684 282.5702 Columns 8 through 11 -64.5875 12.1102 -1.5830 0.1035» x=-1:0.02:1; y=1./(1+25*x.^2);» t=x; p=Lagrange_eval(t,x2,c);» H=plot(x,y,'r',t,p,'b-',x2,y2,'mo');» set(H,'LineWidth',2.5)» print -djpeg075 poly5.jpg
2251
1)(
xxf
Oscillations
56
MATLAB Functions: polyfit and polyval
>> x = linspace(-1,1,5); y = 1./(1+25*x.^2)>> xx = linspace(-1,1); p = polyfit(x,y,4)p = 3.3156 0.0000 -4.2772 0.0000 1.0000>> y4 = polyval(p,xx);>> yr = 1./(1+25*xx.^2);>> H=plot(x,y,'o',xx,y4,xx,yr,'--')>> x=linspace(-1,1,11); y = 1./(1+25*x.^2)y = Columns 1 through 8 0.0385 0.0588 0.1000 0.2000 0.5000 1.0000 0.5000 0.2000 Columns 9 through 11 0.1000 0.0588 0.0385>> p = polyfit(x,y,10)p = Columns 1 through 8 -220.9417 0.0000 494.9095 -0.0000 -381.4338 0.0000 123.3597 -0.0000 Columns 9 through 11 -16.8552 0.0000 1.0000>> y10 = polyval(p,xx);>> H = plot(x,y,'o',xx,y10,xx,yr,'--')>> set(H,'LineWidth',3,'MarkerSize',12)>> print -djpeg Fig14_13.jpg
4th-order polynomial
10th-order polynomial
2251
1)(
xxf
Runge’s Function with polynomial fit
58
4th-order 10th-order
» [x,y]=examplex = Columns 1 through 7 0 0.2000 0.8000 1.0000 1.2000 1.9000 2.0000 Columns 8 through 10 2.1000 2.9500 3.0000y = Columns 1 through 7 0.0100 0.2200 0.7600 1.0300 1.1800 1.9400 2.0100 Columns 8 through 10 2.0800 2.9000 2.9500» c=Lagrange_coef(x,y)c = Columns 1 through 7 -0.0007 0.0512 -2.4384 8.3366 -7.7423 37.5192 -61.2208 Columns 8 through 10 26.4741 -1.1493 0.8958» t=0:0.05:3; p=Lagrange_eval(t,x,c);» H=plot(x,y,'ro',t,p,'b',x,x,'m');» set(H,'LineWidth',2.5); print -djpeg075 poly4.jpg
function [x, y] = examplex = [0.00 0.20 0.80 1.00 1.20 1.90 2.00 2.10 2.95 3.00];y = [0.01 0.22 0.76 1.03 1.18 1.94 2.01 2.08 2.90 2.95];
Noisy straight line
Noisy straight line