Chapter 4 Interpolation
Chapter 4 Interpolation
* Find y(x=4) using a 3rd order polynomial that fits the above data.
* Start with a 3rd order polynomial:
2 3
P3(x) = a0 + a1 x + a2 x + a3 x
* Set P3(xn) = yn for n=0, 1, 2, and 3
a0 + a1 + a2 + a3 = 0
a0 + 2a1 + 4a2 + 8a3 = 6
a0 + 3a1 + 9a2 + 27a3 = 20
a0 + 5.5a1 +30.25a2 + 166.375a3 = 129.375
1 x0 x02 x03 a0 y0
1 x1 x12 x13 a1 y1
=
Or in general, 1 x2 x22 x23 a2 y2
1 x3 x32 x33 a3 y3
or [V] a = y
where the matrix V is called Vandermonde matrix.
* Gaussian elimination (a0, a1, a2, a3) = (-4, 5, -2, 1)
* Interpolation value: y(x = 4) ~ P3(x = 4) = 48.0
Graphical illustration for q0(x) when (x0, x1, x2, x3) = (0, 1, 2, 3):
1.2
L0
1
0.8
0.6
0.4
0.2
0
x
-1 -0.2 0 1 2 3
we obtain
( x − x0 )( x − x2 )( x − x3 )
q1(x) = (=l1(x))
( x1 − x0 )( x1 − x2 )( x1 − x3 )
Graphical illustration for q1(x):
1.2
L1
1
0.8
0.6
0.4
0.2
0
-1 -0.2 0 1 2 3 x
-0.4
* And similarly,
1.2
L3
1
0.8
0.6
0.4
0.2
0
x
-1 -0.2 0 1 2 3
(x) (n+1)
or E(x) = (n+1)! f () (=Eq. (*))
(x-x0) (x-x1)
+ ( x -x ) (x -x ) y2
2 0 2 1
4 59
P2(x=3) = 3 + 3 + 3 = 24.
P1(x) = x − x2 y1 + x − x1 y2
( x1 − x2 ) ( x2 − x1)
x-4 x-2
= -2 3 + 2 59
1 1
P1(x=3) = 2 * 3 + 2 * 59 = 31.
iii. Comparison:
f(3) = 22 (exact)
P1(x=3) = 31 (error =22-31= -9; it is large)
P2(x=3) = 24 (error =22-24= -2; closer to f(3) =22)
* Comparison of fits with y = f(x):
100 f(x)
P2 fit
80
60 P1(x)
40
20
0
-2 -1 0 1 2 x 3 4 5
-20
◊ Further comments:
• Both P1(x) and P2(x) work fine inside the interval (1, 4).
• Both are bad outside the interval
• Error(x) oscillates between node points.
Notes organized by Prof. Renwei Mei, University of Florida 9
* Error in the fit: Error2(x) = f(x) – P2(x)
4
3 Error2
2
1
0 x
-1 0 1 2 3 4
-2
-3
-4
0
The contribution from
-1 0 1 2 3 4 x x2=2 has too much
-2
L2 influence at x=4.
-4
-6
1
= 2 (x-x0) (x-x1) f"().
h2 Sup
Thus, |E1(x)| ≤ 8 [x0‚x1] |f"()|.
1
Thus, max(|f''(x))| = 4*103/2 for x in [10, 1000].
1 1
For h=1, E1≤ 8 4*103/2 = 9.88 E-4 using linear interpolation.
…
[n]
Similarly, an = f0 .
[1] [2]
Hence, Pn(x) = f0 + (x-x0) f0 + (x-x0)(x-x1) f0 +...
[n]
+ (x-x0) (x-x1)...(x-xn-1) f0 .
◊ Error: same as in Lagrangian interpolation (uniqueness)
◊ Note: (x0, x1, x2, ..., xn) can be in any order in the table.
y = f(x)
h h h h
x
x0 x1 x2 xn-1 xn
1 1
= 3h 2 [ (f3-2f2+f1) - (f2-2f1+f0)]
2h
1
= 3!h3 (f3-3f2+3f1-f0)
[2]
2fi fi+1 -fi = fi+2 - 2fi+1 + fi = 2h2fi .
...
k k-1 k-1
fi = fi+1 - fi =...
k(k-1)
= fi+k - kfi+k-1 + 2 fi+k-2 +...
[k]
= k! hk fi
[k] kfi
OR fi = k!hk
s(s-1)...(s-k+1) s
Note: k! = (k) (combinatoric notation)
n
s k
so that Pn(x) = (k) f0
k =1
= Newton-Gregory forward polynomial.
(f(x) = x3)
i xi fi f0 2
f0
3
f0
0 1 1 7 12 6
1 2 8 19 18
2 3 27 37
3 4 64
3.E-04
3f
2.E-04
1.E-04
0.E+00
-1.E-04
-2.E-04
-3.E-04
-4 -3 -2 -1 0 1 2 i
3
It is reasonable to assume that fi ~ -0.00002 based on the 1st
3~
two numbers (i=-3 & -2 in above chart) in fi column.
Hence, -0.00006 - 0.00002 = -0.00008
or - 0.00010 - 0.00002 = 0.00008,
or -3 ~ 0.00025 - 0.00002 ~ -0.000077,
or 3 ~ -0.00021 - 0.00002 ~ -0.000077.
Using = -0.00008, we trace back the error to xk+2 and correct
the data as fk+2 = ~fk +2 + = 0.18738 -0.00008 = 0.18730.
* Comment: errors of this magnitude (0.043%) are impossible to
spot on graphs.
3~
fi column is smoother now!
1
|En(x)| ≤ max{| (x-x0)(x-x1)...(x-xn)|} (n+1)!Cn+1
0.3
0.2
0.1
0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
-0.1
-0.2
-0.3
-0.4
-0.5
-1.2 -4
6 5
100 6
x2 x4 x5
0 50
x0 x1 x3 x
x1 x3 x4 x5 x6
-6 0
x0 x2 x
-12 -50
-18 -100
Polynomials of nth degree (n=2, 4, 8, & 16) fitting the given data in
-2<x≤2 are shown below.
1.2
y y
1.0
1.0
P 2 (x) P 4 (x)
0.8 0.5
0.6 f(x)
0.0
0.4
f(x) -0.5
0.2
0.0 -1.0
-2 -1 0 1 2 -2 -1 0 1 2
x x
1.5 2
y y
1.0 0 f(x)
0.5 P (x)
8
f(x) -2
0.0 P16 (x)
-4
-0.5
-1.0 -6
-1.5 -8
-2 -1 0 1 2 -2 -1 0 1 2
x x
=> Runge’s phenomenon – error increases as n increases.
-1
* Same problem appears if f(x) = (1+x8) , cos10x, ..., in the same
interval.
lim max | f ( x) − pn ( x) | =
n→ − 2 x 2
* Runge’s phenomenon demonstrates that high degree
polynomials are generally unsuitable for interpolation.
y2, y2′
y1, y1′
1 h1 1
slope=0 slope=0
y=1 y=1
h2
0.5 0.5
0 0
x
01 x12 x x01 x12 x
0
h3
0.2 -0.05 h4
slope=1,y=0
0.15 -0.1
0.1 -0.15
slope=1
0.05 -0.2 y=0
0 -0.25
x01 x12 x x01 x12 x
~ 2
= Choose hi(x) = c l i (x)(x-xi) (2n-1 degree)
~
so that hi(xk) = 0 (because li(xk) = ik),
~'
hi (xk) = cl i (xk) + c(xk - xi) 2 li(xk) li'(xk)
2
&
= c ik + 0.
hi'(xk) = 0 = al i (xk)+2li(xk)li'(xk)(axk+b)=0
2
&
is satisfied automatically
= a = - 2 li'(xi)
& b = 1 + 2 xi li'(xi)
◊ Example:
Find the desired fit for n=2
desired fit
Solution:
i) Let x1 = a, x2 = b. Then
2 2
H2(x) = 1 + 2 x − a b − x f (a) + 1 + 2 b − x x − a f (b)
b − a b − a
b − a b − a
2
+ ( x − a)(b − x) f ' (a) + ( x − a) (b − x) f ' (b)
2
2 2
(b − a) (b − a)
Thus P2n-1(x) = f(x1) + (x- x1) f ' ( x1 ) + (x- x1)2 f[x1, x1, x2]
+ (x- x1)2 (x- x2) f[x1, x1, x2, x2]
+ (x- x1)2 (x- x2)2 f[x1, x1, x2, x2, x3]+ …
+ (x- x1)2...(x- xn-1)2 (x- xn) f[x1, x1, x2, x2, ..., xn, xn]
1
= x - x { f[x1, x2] - f ' ( x1 ) }
2 1
1
f[x1, x1, x2, x2] = x - x { f[x1, x2, x2] - f[x1, x1, x2]}
2 1
1
where f[x1, x2, x2] = x - x { f ' ( x2 ) - f[x1, x2]}
2 1
…
Example:
Given: a=0, b=1, f(a)=2, f(b)=3, f'(a)=-2, f'(b)=-3,
use finite difference to find H2(x).
Solution:
x f f' f[x1,x2] f[x1,x1,x2] f[x1,x2,x2] f[x1,x1,x2,x2]
0 2 -2 1 3 -4 -7
1 3 -3
2 1
= n(x) (2n)! f (2n) ( ) .
2 2 1 1
Max 2(x) = 2(x=(x1+ x2)/2) = 16 (x2-x1)4 = 16 h4.
1 1
=> E(x) ≤ max|f(4)()| 4! 16 h4.
0.8
0.6
0.4
0.2 ψ 22
0
-0.2 0 0.5 1 1.5 2 2.5 3 x
-0.4
-0.6
-0.8 4
-1
-1.2
1
=> E(x) ≤ max|f(4)()| 4! h4 for x0≤ x ≤ x3;
1 9
& E(x) ≤ max| f(4)()| 4! 16 h4 for x1≤ x ≤ x2.
Zero curvatures
at end points
i.e. the 1st /2nd derivatives at xi evaluated from the LEFT interval
= that at xi from the MID interval.
gi-1'(xi) = gi'(xi)
at x = xi (6a)
gi-1"(xi) = gi"(xi)
gi'(xi+1) = gi+1'(xi+1)
at x = xi+1 (6b)
gi"(xi+1) = gi+1"(xi+1)
* Differentiate gi(x) to get the 1st order derivative
• gi'(x) = ci + 2bi (x-xi) + 3ai (x-xi)2
x=xi gi'(xi) = ci, (7)
x=xi+1 gi'(xi+1) = ci + 2bi hi + 3ai hi2, (8)
* If we move back by one interval to (xi-1, xi), we have
gi-1'(x) = ci-1 + 2bi-1 (x-xi-1) + 3ai-1 (x-xi-1)2
2
x=xi gi-1'(xi) = ci-1+ 2bi-1hi-1 + 3ai-1 hi-1 (9)
* This gives yi'(xi) = ci (see (7)) based on right interval (xi, xi+1).
* What about yi'(xi) from the left interval (xi-1, xi)? We have
2
yi'(xi) = gi-1'(xi) = ci-1 + 2bi-1 hi-1 + 3ai-1 hi-1 (13)
yi - yi-1 2Si-1+Si
ci-1 = hi-1 - 6 hi-1 (15)
(9)
yi - yi-1 2Si-1+Si Si-Si-1
gi-1'(xi) = h - 6 h i-1 + hi-1Si-1+
2 hi-1
i-1
yi - yi-1 1
= hi-1 + 6 hi-1(Si-1 + 2Si) (16)
-2
End
-4 condition
-6 (ii)
-8 (iii)
(iv)
-10
-12
-3 -2 -1 0 1 2 x
* Although Si’s near the ends of the interval are a little different from
using 3 different conditions, the coefficients (ai, bi, ci, di) differ very
little. Hence the curves for y(x) should be nearly the same.
1.2
End
1 conditions
0.8 f-ii
f-iii
0.6 f-iv
exact
0.4
0.2
0
-0.2
-2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 x
More detailed comparison can be seen from the table given below.
x exact ii iii iv
-2 0.00032 0.00032 0.00032 0.00032
-1.9 0.00048 0.00036 -7.4E-05 0.002137
-1.8 0.00073 0.000513 -2.7E-05 0.002720
-1.7 0.001123 0.000888 0.00046 0.002639
-1.6 0.001749 0.001600 0.001389 0.002462
-1.5 0.002758 0.002758 0.002758 0.002758
-1.4 0.004401 0.004572 0.004688 0.004096
-1.3 0.007100 0.007637 0.007781 0.007046
-1.2 0.011562 0.012644 0.012758 0.012175
-1.1 0.018969 0.020284 0.020340 0.020054
-1 0.031250 0.031250 0.031250 0.031250
-0.9 0.051476 0.047573 0.047542 0.047699
-0.8 0.084291 0.076645 0.076607 0.076801
-0.7 0.136166 0.127199 0.127169 0.127321
-0.6 0.214934 0.207966 0.207952 0.208026
-0.5 0.327680 0.327680 0.327680 0.327680
-0.4 0.476113 0.488544 0.488551 0.488514
-0.3 0.649931 0.666647 0.666655 0.666614
-0.2 0.821927 0.831550 0.831556 0.831528
-0.1 0.951466 0.952814 0.952816 0.952807
20
10
0
0 1 2 x 3 4 5
E M
=0 (-2xi) [yi - (a0 + a1xi + a2xi2)] = 0
a1 i =1
E M
=0 (-2xi2)[yi - (a0 + a1xi + a2xi2)] = 0
a2 i =1
M M M
a0M + a1 xi + a2 xi2 = yi
i =1 i =1 i =1
M M M M
a0 xi + a1 xi2 + a2 xi3 = xiyi (3)
i =1 i =1 i =1 i =1
M M M M
a0 xi2 + a1 xi3 + a2 xi4 = xi2yi
i =1 i =1 i =1 i =1
a0 = 0.9523, a1 = -0.7604
1 M
E= [ yi - (0.9523-0.7604 xi)]2 = 0.00102
M − 1 i =1
0.6 0.6
0.4 0.4
0.2 0.2
0.0 0.0
0.0 0.2 0.4 0.6 0.8 1.0 1.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2
x x
◊ Note:
* The matrix of the system is a symmetric one.
* If n≤3, there is no practical difficulty in obtaining the
coefficients (a0, a1, a2, a3).
* If n>3, the system [X]a = b can be very ill-conditioned.
* For very ill-conditioned system, we need more sophisticated
method to reduce the round off error.
* This method minimize the norm of the error, ||yi -
pn(xi)||2. In many cases, one is only interested in
minimizing ||a-a*||2. This is difficult since we do not
know the exact a* in advance.
Notes organized by Prof. Renwei Mei, University of Florida 51
◊ Matlab implementation for this same problem for least
square fit:
>> x=[0.05 0.11 0.15 0.31 0.46 0.52 0.7 0.74 0.82 0.98
1.17]';
>> y=[0.956 0.89 0.832 0.717 0.571 0.539
0.378 0.37 0.306 0.242 0.104]';
>> p=polyfit(x,y,1)
p =
-0.76040691274189 0.95227686777989
>> p=polyfit(x,y,2)
p =
0.22468213278795 -1.01804246473857 0.99796838418339
* Note: given N=11 data point, the use of the lower order (n<10)
polynomials in the fitting implies that a least square
procedure has been adopted.
* For n=1, Matlab gives: a1 = -0.76040691274189
a0 = 0.95227686777989
and they agree with what were obtained earlier.
* For n=2, Matlab gives: a2 =0.22468213278795
a1 = -1.01804246473857
a0 = 0.99796838418339
and they agree with what were obtained earlier.