Chapter 4 - Interpolation
Chapter 4 - Interpolation
Last lecture:
Method of undetermined coefficients for interpolation
This lecture:
Lagrange method
Newton divided difference method
IV. INTERPOLATION
Example:
Image of a whole alligator
a0 + a1 + a2 + a3 = 0 1 𝑥0 𝑥02 𝑥03 𝑎0 𝑦0
a0 + 2a1 + 4a2 + 8a3 = 6 1 𝑥1 𝑥12 𝑥13 𝑎1 𝑦1
a0 + 3a1 + 9a2 + 27a3 = 20 1 𝑥2 𝑥22 𝑥23 𝑎2 = 𝑦2
𝑎3 𝑦3
a0 + 5.5a1 +30.25a2 + 166.375a3 = 129.375 1 𝑥3 𝑥32 𝑥33
or [V] a = y
[V] = Vandermonde matrix.
* The solution for (a0, a1, a2, a3) exists and is unique
(𝑥 − 𝑥1 )(𝑥 − 𝑥2 )(𝑥 − 𝑥3 )
where L0(x)= (𝑥0 − 𝑥1 )(𝑥0 − 𝑥2 )(𝑥0 − 𝑥3 )
Lagrange Interpolating Polynomials
1.2
Now consider 1
L1
0.8
xi x0 x1 x2 x3 0.6
0.4
yi 0 y1 0 0 0.2
0
-1 -0.2 0 1 2 3 x
(𝑥 − 𝑥0 )(𝑥 − 𝑥2 )(𝑥 − 𝑥3 )
q1(x)= y1 ( y1 L1(x) ) -0.4
(𝑥1 − 𝑥0 )(𝑥1 − 𝑥2 )(𝑥1 − 𝑥3 )
1.2
L2
1
Now consider xi x0 x1 x2 x3 -1
0
-0.2 0 1 2 3 x
-0.4
yi y0 y1 y2 y3
1.2
L3
P3(x) = y0L0(x) + y1L1(x) + y2L2(x) + y3L3(x) 1
0.8
0.6
-- General Result
𝑛
𝑃𝑛 (𝑥) = 𝐿0 (𝑥)𝑓(𝑥0 ) + 𝐿1 (𝑥)𝑓(𝑥1 ) + ⋯ + 𝐿𝑛 (𝑥)𝑓(𝑥𝑛 ) = 𝐿𝑖 (𝑥)𝑓(𝑥𝑖 ) = nth order polynomial fit for the data
𝑖=0
𝑛
𝑥 − 𝑥𝑗 (𝑥 − 𝑥0 )(𝑥 − 𝑥1 ) ⋯ ⋯ (𝑥 − 𝑥𝑖−1 )(𝑥 − 𝑥𝑖+1 ) ⋯ ⋯ (𝑥 − 𝑥𝑛 )
𝐿𝑖 (𝑥) = ෑ =
𝑥𝑖 − 𝑥𝑗 (𝑥𝑖 − 𝑥0 )(𝑥𝑖 − 𝑥1 ) ⋯ (𝑥𝑖 − 𝑥𝑖−1 )(𝑥𝑖 − 𝑥𝑖+1 ) ⋯ (𝑥𝑖 − 𝑥𝑛 )
𝑗=0
𝑗≠𝑖
j = i ; Li ( xi ) = 1
𝐿𝑖 𝑥𝑗 =? Note : Li ( x j ) = ij
j i ; Li ( x j ) = 0
Note:
i. There is no need to solve ill-conditioned system.
ii. (x0, x1, x2, x3,...) can be unevenly spaced and out of order.
iii. Error is exactly zero at x = xi, i.e., E(xi) = 0 (i=1, 2,...n).
iv. If f(x) is a polynomial of order lower than (n+1), then Pn(x) is exactly equal to f(x) since f(n+1)(x)=0
v. The interpolating polynomial is unique. Why?
Uniqueness of interpolating polynomial
Error of Pn(x) in fitting f(x) using the data at (x0, x1, x2,..., xn) is G'(x) has (n+1) roots, ...
E(x) = f(x) - Pn(x) & G(j)(x) has (n+2-j) roots for j=0,1, … n+1
( Note: y(xi) = 0 for i=0, 1, ..., n; & y(n+1)(x) = (n+1)! ) G(n+1)(x) = f(n+1)(x) - –
𝑛+1 !
E(t)
𝜓 𝑡
𝜓 𝑥
Let G(x) = E(x) – E(t) ( E(x) is still UNKNOWN)
𝜓 𝑡 At x=x, G(n+1)(x) = 0
200
𝜓 𝑥i
Then G(xi) = E(xi) – E(t) = 0 𝜓 𝑡 0
𝜓 𝑡
E(t) = (𝑛+1)! f(n+1)(x) -200 0 1 2 3 4 5 6 7 8
because E(xi) = 0 & y(xi) = 0 for i=0, 1, ..., n.
𝜓 𝑥
-400 y (x )
𝜓 𝑡 (n+1)(x)
* Furthermore, G(x=t)= E(t) – E(t) =0 Or E(x) = (𝑛+1)!
f -600 n=8
𝜓 𝑡
-800
Thus G(x) has (n+2) roots
Example
yn -4 3 59 80
P2 fit
𝑥−4 𝑥−2
= *3 + *59 = 31.
−2 2
Example (contd)
• Outside the interval, consider the second order base functions The contribution from x2=2 has too much
L1(x) and L2(x) shown below
10 influence at x=-1 (outside the interval).
8
L1
6
The contribution from x1=1 • Thus extrapolation outside
has too much influence at x=-1. the interval can be dangerous!
4
0
-2 -1 0 1 2 3 4 5
-2
Application of error analysis in interpolation
* Truncation error in linear interpolation between (x0, x1) * Effect of round-off error (R) in interpolation:
Let h = x1-x0 Note: the values of f(x) at x0 and x1, f0 & f1,
𝑥−𝑥1 𝑥−𝑥0 are only approximate due to rounding
E1(x) = f(x) - P1(x) = f(x) - f
𝑥0 −𝑥1 0
-𝑥 f1
1 −𝑥0
1 f0T = f0A + e0, f1T = f1A + e1; e = max(|e0|, |e1|)
= (x-x0) (x-x1) f"(x) for some x in [x0, x1].
2
• This is useful in estimating the dependence of the error in Example: If we interpolate f(x) = 𝑥 in x = [10, 1000],
a general idea about the magnitude of |f"(x)|. Thus, max(|f (x)) = 1/[ 4*103/2 ] for x in [10, 1000],
1 1
For h=1, E1≤ 8 4∗103/2 = 9.88 E-4
𝑓2 −𝑓1 𝑓 −𝑓
− 𝑥1 −𝑥0 [2]
a2 =
𝑥2 −𝑥1 1 0
= 𝑓0
𝑥2 −𝑥0
…
[𝑛] [1] [2]
Similarly, an = 𝑓0 Pn(x) = f0 + (x-x0) 𝑓0 +(x-x0)(x-x1) 𝑓0 + … + (x-x ) (x-x )...(x-x ) 𝑓 [𝑛]
0 1 n-1 0
[𝑛]
i. It is easy to improve from Pn(x) to Pn+1(x) by just adding 𝑓0 (x-x0)(x-x1)...(x- xn) to Pn(x)
𝑥−𝑥𝑗
𝐿𝑖 (𝑥) = ς𝑛+1
𝑗=0 𝑥 −𝑥 , i=0, ...k, n+1
𝑖 𝑗
𝑗≠𝑖
[𝑛]
ii. 𝑓0 is easy to construct (see the table below for example).
1 1 −4 7 7 Sol: i) 𝑝1 3 = −4 + 7 𝑥 − 1 = −4 + 7 ∗ 2 = 10
x = 2 − 1 x = 4 − 1 𝑝2 (3) = 𝑝1 (𝑥) + 7(𝑥 − 1)(𝑥 − 2) = 10 + 7 ∗ 2 ∗ 1
=24
2 2 3 28
For linear interpolation, only 2 points are needed.
x = 4 − 2
In order to avoid extrapolation, we use x2 & x3,
3 4 59 to obtain BEST interpolation for p1(x=3)
p1 (3) = 3 + 28( x − 2) = 3 + 28*1 = 31
x=1 2 3 4
x
ii) At x=1.5, we first compute P1(x=1.5)
[1] [1]
◊ Denote: f0 = f1-f0 = h 𝑓0 , … fi = fi+1 - fi = h 𝑓𝑖
[2]
2f0 f1-f0 = f2-2f1-f0 = 2h2 𝑓0 , … 2fi fi+1 -fi = fi+2 - 2fi+1 + fi = 2h2 𝑓𝑖[2] [𝑘] 𝛥𝑘 𝑓𝑖
𝑓𝑖 =
[3] [𝑘] 𝑘!ℎ𝑘
3f0 2f1 - 2f0 =...= 3!h3 𝑓0 ,… kfi = k-1fi+1 - k-1fi =... = k! hk 𝑓𝑖
4.4.2 Interpolation using forward difference
𝑥−𝑥0
◊ Define s = x – xk = (x - x0) + (x0 – xk) = sh -kh = (s-k)h
ℎ
Consider the evenly spaced data (xi, fi) given below. (f(x) = x3)
i xi fi f0 2 f0 3 f0
0 1 1 7 12 6 𝑠 k
Pn(x) =σ𝑛𝑘=1 f0
1 2 8 19 18 𝑘
2 3 27 37
3 4 64
For the data f(xi, fi) given below (f(x) = x3 ) with h=0.5
i xi fi f0 2 f0 3 f0
0 1.5 3.375 4.625 3 0.75
1 2 8 7.625 3.75
2 2.5 15.625 11.375
3 3 27
* For x=1.8 we use starting point x0=1.5 for both P2 & P3.
s = (x-1.5)/h= 0.3/0.5 = 0.6
* P2(x) = 3.375 + 4.625s + 1.5s(s-1)
=3.375 + 4.625*0.6+1.5*0.6*(-0.4)= 5.79
(Error = 5.832-5.79 = 0.042; much smaller for h=0.5 than 0.192 for h=1.0)
𝑠 3
* P3(x) = P2(x) + f0= 5.79+ 0.125 s(s-1)(s-2)
3
= 5.79 +0.125*0.6(0.6-1)(0.6-2) = 5.832
4.5 Detecting error/noise in data using finite difference
𝜀 if i = 𝑘
* Consider e(xi) = ቊ
0 if i ≠ 𝑘
We can use this behavior to extract isolated error e(xk) from original data and make correction.
Example:
Consider the following discrete data 𝑓ሚ𝑖 on an equal interval which contain an isolated error.
Solution: Let fi = 𝑓ሚ𝑖 + ei
xi 𝑓ሚ𝑖 𝑓ሚ𝑖 2𝑓ሚ𝑖 3𝑓ሚ𝑖 Error guess The kth order forward difference error is
xk-3 0.10396
0.01700
k fi = k 𝑓ሚ𝑖 + k ei
xk-2 0.12096 -0.00014
0.01686 -0.00003 0
k ei = k fi - k 𝑓ሚ𝑖
xk-1 -0.00017
0.13782
Expect kfi to be small for smooth f(x) so that kei ~ -k 𝑓ሚ𝑖
0.01669 -0.00002 0
xk 0.15451 -0.00019
0.01650 0.00006 e
As a first estimate, we try -3e ~ -0.00025 e ~ 0.00008
xk+1 0.17101 -0.00013
0.01637 -0.00025 −3e If we try 3e~0.00021 e ~ 0.00007
xk+2 0.18738 -0.00038
0.01599 0.00021 3e Various estimates for the order of magnitude of e are close.
xk+3 0.20337 -0.00017
0.01582 -0.00010 −e * Note: 3fi will also contribute to 3𝑓ሚ𝑖 besides 3ei
xk+4 0.21919 -0.00027
0.01555
xk+5 0.23474
k fi = k 𝑓ሚ𝑖 + k ei k 𝑓ሚ𝑖 = k fi -k ei
Example (contd)
3.E-04 ~
3f
2.E-04 Comment: errors of this magnitude
1.E-04 (0.043%) are impossible to spot on raw graphs.
0.E+00 * Results after correction
-1.E-04 3 𝑓ሚ𝑖
xi 𝑓ሚ𝑖 𝑓ሚ𝑖 2𝑓ሚ𝑖
-2.E-04 xk-3 0.10396
0.01700
-3.E-04 xk-2 0.12096 -0.00014
-4 -3 -2 -1 0 1 2 i 0.01686 -0.00003
xk-1 0.13782 -0.00017
It is reasonable to assume that 3f
~ -0.00002 based on the
i 0.01669 -0.00002
1st two numbers (i=-3 & -2 in above chart) in 3𝑓ሚ𝑖 column. xk 0.15451 -0.00019
0.01650 -0.00002
Hence, e ~ -0.00006 - 0.00002 = -0.00008 xk+1 0.17101 -0.00021
0.01629 -0.00001
or -e ~ 0.00010 - 0.00002 = 0.00008, xk+2 0.18730 -0.00022
0.01607 -0.00003
or -3e ~ 0.00025 - 0.00002 e ~ -0.000077, xk+3 0.20337 -0.00025
or 3e ~ -0.00021 - 0.00002 e ~ -0.000077. 0.01582 -0.00002
xk+4 0.21919 -0.00027
3 fi column
Using e = -0.00008, we trace back the error to xk+2 and 0.01555 is smooth now
correct the data as fk+2 = 𝑓ሚ𝑘+2 + e = 0.18738 -0.00008 = 0.18730 xk+5 0.23474
Lecture 18
Last lecture:
Newton divided difference method
Finite difference
This lecture:
0.2
where Cn+1 =[ x ‚x ,...x
Sup {| f ( n+1) ( ) |}
0 1 n]
is a constant. 0.1
0
Q: How does En(x) depend on x? -0.1
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
-0.3
Let yn(x) = (x-x0)(x-x1)... (x-xn). -0.4
Polynomials of nth degree (n=2, 4, 8, & 16) fitting the given data in -2<x≤2 are shown below.
1.2 1.5 2
y 1.0 y y y
1.0 P 4 (x) 1.0 0 f(x)
P 2 (x)
0.8 0.5 0.5 P (x)
8
f(x) -2
f(x) 0.0 P16 (x)
0.6
0.0
-4
0.4 -0.5
f(x) -0.5 -6
0.2 -1.0
Given: y1 & y1' at x=x1 and y2 & y2' at x=x2. y2, y2′
Goal: find the interpolating curve that will match the slopes (y1', y2')
at the end of interval in addition to (y1, y2). y1, y1′
◊ Now given (y1, y2, ..., yn) & (y1', y2', ..., yn') at (x1, x2, ..., xn).
We want to find a polynomial P(x) so that
P(xi) = yi
and P'(xi) = yi' for i = 1, 2, ..., n.
◊ Given 2n conditions P(x) is a polynomial of degree (2n-1).
& hi'(xk) = 0 a 𝐿𝑖 2 𝑥𝑘 +2𝐿𝑖 (𝑥𝑘) 𝐿𝑖 ′(𝑥𝑘)(axk + b) = 0 Since R(xi) = R'(xi) = 0 for i = 1, 2, ..., n,
The above is automatically satisfied for i≠k, since 𝐿𝑖 (xk)=dik=0. R(x) = A(x- x1)2(x- x2)2...(x- xn)2 = A 𝜓𝑛2 (𝑥)
R(x) has 2n roots!
For i=k, li(xk)=1, we need a + 2 𝐿𝑖 '(xi) * 1 = 0
R(x) = 0 Gn(x) = Hn(x)
Example:
Find the desired fit for n=2 Hn(x) = σ𝑛𝑖=1 yi𝐿𝑖 2(𝑥)[1 − 2(x− xi) 𝐿𝑖 ′(xi)]
desired fit + σ𝑛𝑖=1 yi′𝐿𝑖 2(𝑥)(x−xi)
Solution:
𝑥−𝑏 𝑏−𝑥 𝑥−𝑎
i) Let x1 = a, x2 = b. Then 𝐿1 𝑥 = 𝑎−𝑏 = 𝑏−𝑎 , 𝐿2 𝑥 = 𝑏−𝑎
* Lagrangian interpolation is not easy to compute compared with Newton’s divided difference
P2n-1(x) = f(z1) + (x- z1) f [z1, z2] +(x- z1) (x- z2) f [z1, z2, z3] + (x- z1)... (x- z2n-1) f [z1, z2, ..., z2n]
E(x) = f(x) - P2n-1(x) = (x- z1)... (x- z2n) f [z1, z2, ..., z2n, x]
* Thus
P2n-1(x) = f(x1) + (x- x1) 𝑓′(𝑥1 ) + (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 𝑓2 −𝑓1 1
= [ - 𝑓′(𝑥1 )] = {f[x1, x2]- 𝑓′(𝑥1 )}
𝑥2 −𝑥1 𝑥2 −𝑥1 𝑥2 −𝑥1
1
& f[x1, x1, x2, x2] = 𝑥 { f[x1, x2, x2] - f[x1, x1, x2]}
2 −𝑥1
1
where f[x1, x2, x2] = 𝑥 {𝑓′(𝑥2 ) − f[x1, x2]}
2 −𝑥1
…
Lecture 19
Last lecture:
Interpolation Error in General
Runge’s phenomenon
Hermite Interpolation
This lecture:
Cubic spline
Example of computable form of Hermite interpolation
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: 1
f[x1, x1, x2] = 𝑥 {f[x1, x2]- 𝑓′(𝑥1 )}
x f f' f[x1, x2] f[x1, x1, x2] f[x1, x2, x2] f[x1, x1, x2, x2] 2 −𝑥1
0 2 -2 1 3 -4 -7 1
1 3 -3 f[x1, x2, x2] = 𝑥 {𝑓′(𝑥2 ) − f[x1, x2]}
2 −𝑥1
In order to evaluate H2(x), various divided differences are needed. 1
f[x1, x1, x2, x2] = 𝑥 { f[x1, x2, x2]
2 −𝑥1
f[x1, x2] = (3-2)/(1-0) = 1; f[x1, x1, x2] = (1-(-2))/(1-0) = 3 - f[x1, x1, x2]}
f[x1, x2, x2] = (-3-1)/(1-0)= -4; f[x1, x1, x2, x2] = (-4-3)/(1-0)= -7.
P2n-1(x) = f(x1) + (x- x1) 𝑓′(𝑥1 ) + (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]
Error in Hermite interpolation:
* Again, consider having 2n nodes: z1, z2, ..., z2n = x1, x1, x2, x2, ..., xn, xn.
Hermite H2(x): (x1, x2), (y1, y2), and (y1', y2') are given
1 1
Error: E(x) = 4! f(4)(x) (x-x1)2(x-x2)2 = 4! f(4)(x) 22 (x)
1 4
Max{ 22 (x) }= 22 (x =(x1+ x2)/2) = h
16
Comparing with error in Lagrange interpolation:
* Lagrangian L3(x): (x0, x1, x2, x3) and (y0, y1, y2, y3) are given
1 1
Error: E(x)= 4! f(4)(x) (x-x0)(x-x1)(x-x2)(x-x3) = 4! f(4)(x) 4(x)
y gi(x) yi+1
yi
gi+1(x)
gi-1(x) yi+2
yi-1
left mid right
interval interval interval
gi(x) = di + ci (x- xi) + bi (x- xi)2 + ai (x- xi)3 (1) y gi(x) yi+1
yi
gi+1(x)
gi-1(x) yi+2
* Two obvious constraints are:
yi-1
gi(xi) = yi & gi(xi+1) = yi+1 (2) left mid right
interval interval interval
gi(x) = di + ci (x- xi) + bi (x- xi)2 + ai (x- xi)3 (1) y gi-1"(xi)= gi "(xi) gi"(xi+1)= gi+1"(xi+1)
* Now we pose "smoothness conditions" between adjacent intervals gi-1′(xi)= gi′(xi) gi′(xi+1)= gi+1′(xi+1)
i.e. the 1st & 2nd derivatives at xi evaluated from the LEFT interval gi(x)
gi-1(x) gi+1(x) yi+2
= those at xi from the MID interval. yi-1
left mid right
gi-1'(xi) = gi'(xi)
at x = xi (6a) xi-1 xi xi+1 xi+2 x
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)
gi(x) = di + ci (x- xi) + bi (x- xi)2 + ai (x- xi)3 (1) y gi-1"(xi)= gi "(xi) gi"(xi+1)= gi+1"(xi+1)
Solve for ci using (ai, bi, di) and Si cihi + biℎ𝑖2 + ai ℎ𝑖3 = yi+1 - yi (4)
yi+1−yi 2ℎ𝑖 𝑺𝒊+ℎ𝑖𝑺𝒊+𝟏
gi'(xi) = ci = - (12)
hi 6
* 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 ℎ𝑖−1 (13)
* Repeat the same procedure from Eqns. (10-12) to obtain (ai-1, bi-1, ci-1) based on (Si-1, Si, yi-1, yi),
4.8.2 Smoothness conditions at joints
gi(x) = di + ci (x- xi) + bi (x- xi)2 + ai (x- xi)3 (1) y gi-1"(xi)= gi "(xi) gi"(xi+1)= gi+1"(xi+1)
gi-1′(xi)= gi′(xi) gi′(xi+1)= gi+1′(xi+1)
• Repeat the same procedure from Eqns. (10-12)
to obtain (ai-1, bi-1, ci-1) based on (Si-1, Si, yi-1, yi), gi(x)
gi-1(x) gi+1(x) yi+2
𝑆𝑖 −𝑆𝑖−1
bi-1 = Si-1/2, ai-1 = (14) yi-1
left mid right
6ℎ𝑖−1
gi-1'(xi) =
𝑦𝑖 −𝑦𝑖−1 1
+ 6 hi-1(Si-1 + 2Si) (16)
yi+1−yi 2ℎ𝑖 𝑺𝒊+ℎ𝑖𝑺𝒊+𝟏
gi'(xi) = ci = - (12)
ℎ𝑖−1 hi 6
yi+1−yi 𝑦𝑖 −𝑦𝑖−1
hi-1Si-1+2(hi-1+hi)Si+hiSi+1 = 6( - )
hi ℎ𝑖−1
gi(x) = di + ci (x- xi) + bi (x- xi)2 + ai (x- xi)3, i=0,1,2,..., n-1 (1) y gi-1"(xi)= gi "(xi) gi"(xi+1)= gi+1"(xi+1)
gi-1′(xi)= gi′(xi) gi′(xi+1)= gi+1′(xi+1)
* Simplification: hi = constant (as in many applications_
... ... ... gi(x)
𝑆1 gi-1(x) gi+1(x) yi+2
1 4 1
𝑆2 yi-1
1 4 1 ... left mid right
𝑆3 6
1 4 1 = 𝑦𝑖+1 − 2𝑦𝑖 + 𝑦𝑖−1
𝑆4 ℎ2 xi-1 xi xi+1 xi+2 x
1 4 1 ...
𝑆5
1 4 1 ...
...
... ... ...
𝑆𝑛−1
* Each equation for Si depends on Si-1 & Si+1.
* Hence a set of coupled equations for yi" = Si must be solved.
Total number of unknowns involved: (S0, S1, S2,..., Sn) n+1
Total number of equations for (17) = n-1
since there are (n-1) internal nodes: (x1, x2,..., xn-1).
There several possibilities for providing TWO end conditions y gi-1"(xi)= gi "(xi) gi"(xi+1)= gi+1"(xi+1)
i. If we know the values of S0 & Sn, use the given values. gi-1′(xi)= gi′(xi) gi′(xi+1)= gi+1′(xi+1)
(i.e. 1st & last intervals are fit by parabolas so that y"'=0)
Set S0 = S1 & Sn = Sn-1 from (17), the 1st & last Eqns.
(3h0+2h1) S1 + h1S2 = 6(f[x2, x1] - f[x1, x0]) (18)
hn-2Sn-2+(2hn-2+3hn-1)Sn-1 = 6 (f[xn, xn-1]- f[xn-1, xn-2]) (19)
The number of unknowns is (n-1) now.
gi(x)
& cihi + biℎ𝑖2 + ai ℎ𝑖3 = yi+1 - yi (4) gi-1(x) gi+1(x) yi+2
yi-1
left mid right
Let Si gi"(xi) = 2bi (10a)
xi-1 xi xi+1 xi+2 x
bi = Si /2, ai = (Si+1 - Si)/(6hi) (11)
yi+1−yi 2ℎ𝑖𝑺𝒊 +ℎ𝑖𝑺𝒊+𝟏
ci = - (12)
hi 6
yi+1−yi 𝑦𝑖 −𝑦𝑖−1
hi-1Si-1+2(hi-1+hi)Si+hiSi+1 = 6( - ) i. S0 & Sn are given
hi ℎ𝑖−1
valid at (n-1) internal nodes: (x1, x2,..., xn-1) n-1 eqns. iii. Take S0 = S1 & Sn = Sn-1
involves (n+1) unknowns involved: (S0, S1, S2,..., Sn) iv. “Not-a-knot”
Need TWO additional conditions to close the system
4.8.3 End conditions
iv. “Not-a-knot” (extrapolation) condition gi(x) = di + ci (x- xi) + bi (x- xi)2 + ai (x- xi)3, i=0,1,..., n-1
1.2
End x exact ii iii iv
-2 0.00032 0.00032 0.00032 0.00032
1 conditions -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
0.8 f-ii natural -1.6 0.001749 0.001600 0.001389 0.002462
0
0 1 2 x 3 4 5
◊ Fitting with an nth order polynomial using a =(a0, a1, a2,..., an)T.
Let Pn(x) = a0 + a1x + a2x2 + ... + anxn, (4) [X] a = b.
[X] can be very ill-conditioned for large n
Example
Given the data below, find the least square fit using P1(x) and P2(x).
xi 0.05 0.11 0.15 0.31 0.46 0.52 0.70 0.74 0.82 0.98 1.17
yi 0.956 0.89 0.832 0.717 0.571 0.539 0.378 0.370 0.306 0.242 0.104
Solution:
* n = 11, n n n
𝑛 𝑛 𝑥i xi2 𝑦𝑖
σ𝑛𝑖=1 𝑥𝑖 = 6.01, 𝑖=1 𝑥𝑖2 = 4.6545 i=1 i=1 i=1
𝑛 𝑛
n n n 𝑎0 n
𝑖=1 𝑥𝑖3 = 4.115, 𝑖=1 𝑥𝑖4 = 3.9161 𝑥i 𝑥i2 xi3 𝑎1 = 𝑥i 𝑦𝑖
i=1 i=1 i=1 𝑎2 i=1
𝑛
σ𝑛𝑖=1 𝑦𝑖 = 5.905, σ𝑛𝑖=1 𝑥𝑖 𝑦𝑖 = 2.1839, 𝑖=1 𝑥𝑖2 𝑦𝑖 = 1.3357 n 𝑛 n n
11 6.01 𝑎0 = 5.905 Standard error of the estimate for P1(x) (a0 and a1)
6.01 4.6545 𝑎1 2.1839
Let 𝑆𝑟 = σ𝑛𝑖=1(𝑦𝑖 − (𝑎0 + 𝑎1 𝑥𝑖 ))2 = 0.009146
a0 = 0.9523, a1 = -0.7604
𝑆𝑟
P1(x) = 0.9523 - 0.7604 x 𝑆𝑦/𝑥 = = 0.03188 ~ “standard deviation”
𝑛−2
Example
Solution: n n n
* n = 11, 𝑛 𝑥i xi2 𝑦𝑖
i=1 i=1 i=1
𝑛 𝑛 n n n 𝑎0 n
σ𝑛𝑖=1 𝑥𝑖 = 6.01, 𝑖=1 𝑥𝑖2 = 4.6545, 𝑖=1 𝑥𝑖3 = 4.115,
𝑥i 𝑥i2 xi3 𝑎1 = 𝑥i 𝑦𝑖
𝑛 𝑎2
𝑖=1 𝑥𝑖4 = 3.9161, σ𝑛𝑖=1 𝑦𝑖 = 5.905, σ𝑛𝑖=1 𝑥𝑖 𝑦𝑖 = 5.905, i=1
n
i=1
𝑛
i=1
n
i=1
n
Standard error of the estimate for P2(x) (a0, a1 and a2)
P2(x) = 0.9980 -1.0180 x + 0.2247 x2 Let 𝑆𝑟 = σ𝑛𝑖=1(𝑦𝑖 − (𝑎0 + 𝑎1 𝑥𝑖 + a2xi2))2 = 0.001868
𝑆𝑟 ~ “standard deviation”
𝑆𝑦/𝑥 = = 0.01528
𝑛−3
Example
Given the data below, find the least square fit using P1(x) and P2(x).
xi 0.05 0.11 0.15 0.31 0.46 0.52 0.70 0.74 0.82 0.98 1.17
yi 0.956 0.89 0.832 0.717 0.571 0.539 0.378 0.370 0.306 0.242 0.104
Solution:
Linear least square fit Quadratic least square fit
1.0 1.0
y y = 0.9523 - 0.7604x y y = 0.9980 - 1.0180x
+ 0.2247x^2
0.8 0.8
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
Chapter 1 ExamplesSupplemental Reading for
Chapter IV Interpolation
Example 1 Polynomial and divided difference interpolation:
If f(x) is a monic quartic polynomial such that
f(-1) = -1, f(2) = -4, f(-3) = -9, and f(4) = -16,
find: f(1).
Note: “quartic” means that the polynomial is of fourth order;
“monic” means that the coefficient of the highest power term is one.
Solution:
Suppose that the 4th order polynomial we are looking for is P4(x). There are
two approaches we can use.
i) Elementary approach (using high school algebra):
We note that the discrete values of the function are related to –x2, which is
not a 4th order polynomial.
However, if we let P4(x) = –x2 + g4(x), where g4(x) should be a monic quartic
polynomial, then we see that
g4(-3) = g4(-1) = g4(2) = g4(4) = 0
i.e., x=-3, -1, 2 & 4 are roots of the monic quartic function g4(x).
Thus g4(x) can be written as g4(x) = (x+3)(x +1)(x-2)(x-4).
Hence P4(x) = (x+3)(x +1)(x-2)(x-4) - x2
And P4(1) = 24-1=23
ii) Newton’s divided difference method:
[1] [2] [3] [4]
x f fi fi fi fi
-3 -9 4 -1 0 B=?
-1 -1 -1 -1
2 -4 -6
4 -16
P4(x) = -9 +4(x+3) -1(x+3)(x+1) +0*(x +3)(x +1)(x -2)
+B*(x+3)(x +1)(x-2)(x-4)
Note: coefficient B of the last term of P4(x) is 1 since P4(x) is monic.
Simplify the above, we obtain
P4(x) =(x+3)(x +1)(x-2)(x-4) - x2
Therefore, P4(1) = 24-1=23
Example 2 (Polynomials and roots) Let P(x) be a polynomial of degree 2006.
If P(n)=1/n for n=1, 2, 3, …, 2007, compute the value of P(2008).
Solution:
Because p(n)=1/n, we have nP(n)-1=0 for n=1, 2, …, 2007,
we define a polynomial Q(x) such that Q(x) = xp(x)-1.
Because nP(n)-1=0 for n=1, 2, …2007, we have
Q(1)=Q(2)=…=Q(2007)=0 which has a total of 2007 real roots.
Since P(x) is a polynomial of order 2006, it implies that Q(x) is a
polynomial of order 2007. Thus
Q(x) = A(x-1)(x-2)…(x-2007) = xp(x)-1
=> P(x) = [Q(x)+1]/x
Since p(x) is a regular polynomial, its value at x=0 should be well defined,
=> Q(x)+1 at x = 0 must be 0 in order for P(0) to be well defined.
=> Q(0) = -1
=> A(0-1)(0-2)…(0-2007) = -1 => A = 1/2007!
( x − 1)( x − 2)...( x − 2007) / 2007!+1
P(x) =
x
Question: How do we obtain a reliable and accurate value for f(0.05) using
interpolation?
Solution:
* We know that Lagrange interpolation gives the same result as Newton divided
difference; thus we choose to work with divided difference method. In fact, for
what we are going to do, divided difference method is ideal.
* In real world, we do not have exact answer and we have to be open-minded to
explore various possibilities. We do not know what the best order of polynomial
to use. It is noted that higher order is not always necessarily better than the
lower order ones since the data given in the table has round offer errors (due to
its finite number of significant digits). Thus we have to examine the results
from various orders, Pk(0.05).
* The interpolated result for f(0.05) also depends the choice of x0. We need to
examine the effect of x0 on the behavior of Pk(0.05) on as well.
* We first establish the divided difference table based on the given data
x f f[1] f[2] f[3] f[4]
0.047026004 -29.0031775811 607.28378 -12563.716 256416.32 -5158347.79
0.047691894 -28.5987937135 590.50417 -12048.564 242559.10 -4812194.74
0.048361565 -28.2033497952 574.32108 -11558.479 229558.14 -4495011.17
0.049035048 -27.8165543888 558.70782 -11092.017 217344.78 -4195169.24
0.049712371 -27.4381285867 543.63913 -10647.849 205880.93 -3928614.26
0.050393564 -27.0678053440 529.09113 -10224.701 195083.95 -3664603.32
0.051078657 -26.7053288540 515.04121 -9821.4430 184954.73 -3437574.10
0.05176768 -26.3504539631 501.46793 -9436.9264 175398.38 -3213430.89
0.052460662 -26.0029456212 488.35103 -9070.1774 166413.64
0.053157634 -25.6625783659 475.67123 -8720.2075
0.053858628 -25.3291358386 463.41028
0.054563674 -29.0031775811
* Let x=0.05 (=xx), we then compute the interpolation using linear (n=1),
quadratic (n=2), cubic, & 4th order polynomials for f(x=0.05) as follows:
[1] [2] [n]
Pn(x) = f0 +(x-x0)f0 +(x-x0)(x-x1)f0 +... +(x-x0) (x-x1)...(x-xn-1)f0 .
The implementation for obtaining Pn(x) for n=1 to 4 is shown below:
X f f[1] f[2] f[3] f[4] linear quadratic cubic forth
=H2+E2* =I2+F2*(xx- =J2+G2*(xx-
- =C2+D2* (xx-B2) B2)*(xx- B2)*(xx-B3)*(xx
0.04703 29.003 # … (xx-B2) *(xx-B3) B3)*(xx-B4) -B4)*(xx-B5)
=H3+E3* =I3+F3*(xx- =J3+G3*(xx-
- =C3+D3* (xx-B3) B3)*(xx- B3)*(xx-B4)*(xx
0.04769 28.599 # … (xx-B3) *(xx-B4) B4)*(xx-B5) -B5)*(xx-B6)
=H4+E4* =I4+F4*(xx- =J4+G4*(xx-
- =C4+D4* (xx-B4) B4)*(xx- B4)*(xx-B5)*(xx
0.04836 28.203 # … (xx-B4) *(xx-B5) B5)*(xx-B6) -B6)*(xx-B7)
=H5+E5* =I5+F5*(xx- =J5+G5*(xx-
- =C5+D5* (xx-B5) B5)*(xx- B5)*(xx-B6)*(xx
0.04904 27.817 # … (xx-B5) *(xx-B6) B6)*(xx-B7) -B7)*(xx-B8)
=H6+E6* =I6+F6*(xx- =J6+G6*(xx-
- =C6+D6* (xx-B6) B6)*(xx- B6)*(xx-B7)*(xx
0.04971 27.438 # … (xx-B6) *(xx-B7) B7)*(xx-B8) -B8)*(xx-B9)
The results of the interpolations for xx=0.05 from various orders of polynomials
based on the above formula are:
x0 F f[1] f[2] f[3] f[4] linear quadratic cubic Forth
0.04703 -29.003 # # # # -27.197 -27.2834 -27.280475 -27.28053133
0.04769 -28.599 # # # # -27.236 -27.2814 -27.280526 -27.28053097
0.04836 -28.203 # # # # -27.262 -27.2806 -27.280532 -27.28053102
0.04904 -27.817 # # # # -27.277 -27.2805 -27.280530 -27.28053099
0.04971 -27.438 # # # # -27.282 -27.2806 -27.280532 -27.28053105
0.05039 -27.068 # # # # -27.276 -27.2804 -27.280524 -27.28053056
0.05108 -26.705 # # # # -27.261 -27.2796 -27.280476 -27.2805272
0.05177 -26.350 # # # # -27.237 -27.2779 -27.280345 -27.28051532
If we use a third order polynomial, the best result (with smallest error) should
come from the group of data (in almost equal interval cases) with two points on
the left of x and two points on the right so that
E3(x) = (x-x0)(x-x1)(x-x2)(x-x3)
will have the smallest magnitude.
Thus using x0= 0.4904, we obtain P3(x=0.05) = -27.280530
For P4(0.05), we need 5 points. If we examine the result on the “forth” order
column, we see that p4(0.05)= -27.28053102 when we choose the beginning
point x0=0.04836. If we choose the beginning point to be x0= 0.04904, the then
p4(0.05)= -27.28053099; the difference is 0.00000003. Either is acceptable.
The graph bellows shows how the cubic and forth order interpolation results
depend on the choice of x0; it is the graphical representation of the data in the
respective columns of the above table.
-27.28047
p(0.05)
-27.28048
cubic
-27.28049
forth order
-27.2805
-27.28051
-27.28052
-27.28053
-27.28054
0.047 0.048 0.049 0.050 0.051 x0
Clearly the result from the “forth” order interpolation, p4, is better than the cubic
polynomial fit, p3, because it is less sensitive to the choice of x0 or has less
fluctuations.
We can also see that for 0.04769≤ x0≤0.04971, the forth order polynomial gives
interpolation for x=0.05. For x0 outside (0.04769, 0.04971), the 4th order
polynomial gives extrapolation which should result in larger error and should
be avoided.
x0 P5(0.05)
0.047691894 -27.2805310067
0.048361565 -27.2805310078
0.049035048 -27.2805310057
0.049712371 -27.2805310063
0.050393564 -27.2805309415
For a 5th order polynomial fit, the smallest error for P5(0.05) will be obtained if
there are 3 data pints on the left of x=0.05 and 3 on the right. Thus the best
result should be between using x0 = 0.048361565. Thus we estimate that
P5(0.05) should be around -27.2805310078.
* For the data given, a 6th order fit using 7 gives
x0 P6(0.05)
0.047026004 -27.2805310077
0.047691894 -27.2805310073
0.048361565 -27.2805310070
0.049035048 -27.2805310058
0.049712371 -27.2805310018
0.050393564 -27.2805309480
0.051078657 -27.2805312436
0.051767680 -27.2805310671
If we use x0 =0.047691894, P6(0.05)=-27.2805310073386. If x0 =0.048361565,
P6(0.05)= -27.2805310070.
If we conclude using P6(x) then it is estimated that
f(0.05) ~ -27.280531007
Example 4 Interpolation using Matlab built-in functions
For the previous problem, we are still interested in finding the value of f(0.05)
for the data given. However, we would like to explore the capability of Matlab
built-in functions and to compare results for f(0.05) from various method.
Solution:
i) At the very end of the last problem, we obtained P6(0.05)=-27.2805310073386.
In doing so, we used 7 data points, which place x=0.05 in the middle of the
interval, to obtain a 6th order polynomial. Thus we first want to use a similar
approach on the same set of data. The functions we can use in Matlab are
“polyfit(x, y)” and “polyval(p, xnew)”.
% Input x(i)data
>> format long
>> x=[0.04769189351
0.04836156519
0.04903504805
0.04971237131
0.05039356444
0.05107865718
0.05176767950
];
>> y=[-28.5987937135
-28.2033497952
-27.8165543888
-27.4381285867
-27.0678053440
-26.7053288540
-26.3504539631
];
>> p=polyfit(x,y,6)
Warning: Polynomial is badly conditioned. Remove repeated data
points
or try centering and scaling as described in HELP
POLYFIT.
> In polyfit at 79
p =
1.0e+009 *
Columns 1 through 5
-1.37967600281235 0.50241468586258 -0.07823040313940
0.00675310209155 -0.00034908333831
Columns 6 through 7
0.00001080647011 -0.00000018554058
* Note: this p vector of dimension 7 contain coefficients of the 6th order
polynomial in the form of P6(x) = p(1)*x6+ p(2)*x5+ p(3)*x4+ p(4)*x3+ p(5)*x2+
p(6)*x+ p(7).
% evaluating P6(x) using “polyval” command:
>> xn=0.05;
>> yn=polyval(p, xn)
yn =
-27.28053100733874
% evaluating P6(x) using definition:
>>yfit=p(1)*xn^6+p(2)*xn^5+p(3)*xn^4+p(4)*xn^3+p(5)*
xn^2+p(6)*xn+p(7)
yfit =
-27.28053100733888
% evaluating P6(x) using nested multiplication which is
more accurate and involves less operation
>>yfit=(((((p(1)*xn+p(2))*xn+p(3))*xn+p(4))*xn+p(5))*xn
+p(6))*xn+p(7)
yfit =
-27.28053100733874
* It is clear that these two ways of evaluating P6(x) for the same coefficients yield
basically the same results.
* P6(x =0.05)= -27.28053100733874 which agrees with what we got in the
last problem using divided difference. The difference in the last two digits reflects
the effect of roundoff error in summing the essentially same polynomial using
different expressions.
ii) Matlab has cubic Hermite interpolating polynomial (PCHIP) and cubic spline
polynomial interpolation. See description of the methods in the next example.
For consistency, we use the same 7 points of (x, y) as in the last example.
x y
0.04769189351 -28.5987937135
0.04836156519 -28.2033497952
0.04903504805 -27.8165543888
0.04971237131 -27.4381285867
0.05039356444 -27.0678053440
0.05107865718 -26.7053288540
0.05176767950 -26.3504539631
• On each subinterval xkx xk+1, p(x) is the cubic Hermite interpolant to the
given values and certain slopes at the two endpoints.
• p(x) interpolates , i.e., p(xj) = yj, and the first derivative p′(x) is continuous.
p″(x) is probably not continuous; there may be jumps at the xj.
• The slopes at the xj are chosen in such a way that p(x) preserves the shape of
the data and respects monotonicity. This means that, on intervals where the
data are monotonic, so is p(x); at points where the data has a local extremum,
so does p(x).
Comments:
spline constructs S(x) in almost the same way pchip constructs p(x).
However, spline chooses the slopes at the xj differently, namely to make
even S″(x) continuous. This has the following effects:
Illustration:
>>x = -3:3;
>>y = [-1 -1 -1 0 1 1 1];
>>t = -3:.01:3;
>>p = pchip(x,y,t);
>>s = spline(x,y,t);
>>plot(x,y,'o',t,p,'-',t,s,'-.')
>>legend('data','pchip','spline',4)
1.5
0.5
-0.5
-1 data
pchip
spline
-1.5
-3 -2 -1 0 1 2 3
Example: 6 Interpolating curves with singularity
Consider y= x1/2, for x in [0, 2] with h=0.5. Investigate the behavior of the
interpolation using a cubic spline and a 4th order polynomial using Matlab.
Solution:
In solving this problem, we will use Matlab as a tool. The commands are listed
below; hopefully they are self-explanatory.
% define x & y
>> x=0:0.5:2;
>> y=x.^0.5;
% define fine grid x using new_x:
>> new_x=0:0.05:2;
% compute the y value using the cubic spline
>> new_y_spline=interp1(x,y,new_x,'spline');
% compute the exact value of y
>> exact_Y=new_x.^0.5;
% compute coefficient of 4th order polynomial fit given
5 points
>> p=polyfit(x,y,4)
p =
-0.2088 1.0878 -2.0947 2.2157 -0.0000
% tabulate the results for y’s and errors
>> [new_x', new_y_spline',y_fit4', exact_Y', (exact_Y-
new_y_spline)',(exact_Y-y_fit4)']
ans =
0 0 -0.0000 0 0.0000 0.0000
0.0500 0.1014 0.1057 0.2236 0.1222 0.1179
0.1000 0.1949 0.2017 0.3162 0.1213 0.1145
0.1500 0.2809 0.2888 0.3873 0.1064 0.0985
0.2000 0.3597 0.3677 0.4472 0.0875 0.0795
0.2500 0.4319 0.4392 0.5000 0.0681 0.0608
0.3000 0.4977 0.5039 0.5477 0.0500 0.0438
0.3500 0.5578 0.5624 0.5916 0.0338 0.0292
0.4000 0.6124 0.6154 0.6325 0.0201 0.0170
0.4500 0.6620 0.6635 0.6708 0.0088 0.0074
0.5000 0.7071 0.7071 0.7071 0 -0.0000
0.5500 0.7480 0.7469 0.7416 -0.0064 -0.0053
0.6000 0.7852 0.7832 0.7746 -0.0106 -0.0086
0.6500 0.8192 0.8167 0.8062 -0.0129 -0.0104
0.7000 0.8502 0.8476 0.8367 -0.0135 -0.0109
0.7500 0.8788 0.8764 0.8660 -0.0128 -0.0103
0.8000 0.9054 0.9034 0.8944 -0.0110 -0.0090
0.8500 0.9304 0.9290 0.9220 -0.0084 -0.0070
0.9000 0.9542 0.9534 0.9487 -0.0055 -0.0048
0.9500 0.9773 0.9770 0.9747 -0.0026 -0.0024
1.0000 1.0000 1.0000 1.0000 0 0.0000
1.0500 1.0228 1.0225 1.0247 0.0019 0.0022
1.1000 1.0456 1.0448 1.0488 0.0032 0.0040
1.1500 1.0684 1.0670 1.0724 0.0040 0.0054
1.2000 1.0912 1.0892 1.0954 0.0042 0.0062
1.2500 1.1139 1.1115 1.1180 0.0041 0.0065
1.3000 1.1365 1.1339 1.1402 0.0036 0.0063
1.3500 1.1590 1.1565 1.1619 0.0029 0.0054
1.4000 1.1812 1.1792 1.1832 0.0021 0.0041
1.4500 1.2031 1.2019 1.2042 0.0011 0.0022
1.5000 1.2247 1.2247 1.2247 0 -0.0000
1.5500 1.2460 1.2474 1.2450 -0.0010 -0.0025
1.6000 1.2669 1.2699 1.2649 -0.0020 -0.0050
1.6500 1.2874 1.2920 1.2845 -0.0028 -0.0075
1.7000 1.3073 1.3134 1.3038 -0.0035 -0.0096
1.7500 1.3267 1.3341 1.3229 -0.0039 -0.0112
1.8000 1.3456 1.3536 1.3416 -0.0039 -0.0119
1.8500 1.3638 1.3717 1.3601 -0.0036 -0.0116
1.9000 1.3813 1.3881 1.3784 -0.0029 -0.0097
1.9500 1.3981 1.4024 1.3964 -0.0017 -0.0060
2.0000 1.4142 1.4142 1.4142 0 0.0000
>> plot(x,y,new_x,new_y_spline,new_x,y_fit4,new_x,exact_Y,'-o')
1.6
1.4
1.2
0.8
0.6
0.4
0.2
-0.2
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
(red and blue lines: cubic spline and polyfit; symbol: exact; blue: raw data)
>> plot(new_x,exact_Y-new_y_spline,new_x,exact_Y-y_fit4,'-o')
0.14
0.12
0.1
0.08
0.06
0.04
0.02
-0.02
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
(symbol: error from polyfit4; blue line: error from cubic spline)
Discussion:
• The function has a singularity at x=0 since f′(0)→.
• Thus it is difficult to interpolate using any method.
• The cubic spline and a 4th order polynomial fit over the whole interval
perform similarly for this problem.
• The largest errors occur near x=0 (due to the singularity), as expected.
If we extend the interval further to consider x in [0, 3] and use h=0.5 to obtain
the raw data y= x1/2, we have 7 data points. Thus with the regular polynomial fit
using “polyfit(x,y,6)”, we can obtain a 6th order polynomial fit for the data.
The comparison of the errors from the regular 6th order polynomial fit and from
the cubic spline fit is shown below.
0.14
0.12
0.1
0.08
0.06
0.04
0.02
-0.02
0 0.5 1 1.5 2 2.5 3
(symbol: error from polyfit(x, y, 6); blue line: error from cubic spline)
Clearly, near x=3, cubic spline performs better despite its lower order.
Example 7 Two-dimensional interpolations using Matlab
Function: zi = interp2 (x, y, z, xi, yi)
-2
-4
-6
3
2
3
1 2
0 1
-1 0
-1
-2
-2
-3 -3
Example 8 Cubic spline interpolation –setting up systems for different end
conditions
Consider the following data: 1.5
y
n x y 1.0
0 0 0.0000000000 0.5
1 0.5 0.8414709848
2 1.0 0.9092974268 0.0
3 1.5 0.1411200081 0 1 2 3 4 x
4 2.0 -0.7568024953 -0.5
5 2.5 -0.9589242747 -1.0
6 3.0 -0.2794154982
7 3.5 0.6569865987 -1.5
8 4.0 0.9893582466
Find the cubic spline interpolations using three different end conditions.
Solution:
* We note that h=x=0.5 is a constant. Thus, the system of equations for S (2nd
order derivative) we will be solving is in the form of
... ... S1 ...
1 4 1 S y − 2y + y
2 i +1 i i −1
1 4 1 S3 6 ...
S = 2
1 4 1 4 h ...
1 4 1 S5 ...
1 4 1 S6 ...
... ... S 7 ...
x y S-(ii) (dy) c b a
0 0 0 0 1.98752315 0 -1.2183247
0.5 0.841471 -3.654974 … 1.07377961 -1.8274871 -0.0975328
1 0.9092974 -3.947572 … -0.8268570 -1.9737862 1.10958126
1.5 0.14112 -0.618829 … -1.9684573 -0.3094143 1.30927793
2 -0.756802 3.3090051 … -1.2959132 1.65450256 0.2576735
2.5 -0.958924 4.0820256 … 0.55184447 2.0410128 -0.8533333
3 -0.279415 1.5220258 … 1.95285732 0.7610129 -1.8422383
3.5 0.6569866 -4.004689 … 1.33219149 -2.0023446 1.33489638
4 0.9893582 0 0
iii. Not-a-Knot: Take S0 = S1 and S8 = S7,
S0 + 4S1 + S2 = (S1) + 4S1 + S2 = 5S1+ 1S2,
& S6 + 4S7 + S8 = S6 + 4S7 + (S7) = 1S6+ 5S7
the system of equations become
5 1 S1 - 18.56746903
1 4 1
S 2 = - 20.06409266
1 4 1 S 3 - 3.113882030
1 4 1 S 4 16.69921738
1 4 1 S 5 21.15913334
1 4 1 S 6 6.165439691
1 5 S 7 − 14.49673078
S 0 - 2.88235796
S - 2.88235796
1
S 2 - 4.15567923
= - 0.55901777
S3
S 4 3.277868268
S 5 4.146762072
S 6 1.294216783
S 7 - 3.15818951
S - 3.15818951
8
x y S-(iii) (d=y) c b a
0 0 0 0 2.40353146 -1.441179 0
0.5 0.841471 -3.654974 … 0.96235248 -1.441179 -0.4244404
1 0.9092974 -3.947572 … -0.79715682 -2.0778396 1.19888716
1.5 0.14112 -0.618829 … -1.97583107 -0.2795089 1.27896201
2 -0.756802 3.3090051 … -1.29611844 1.63893413 0.28963127
2.5 -0.958924 4.0820256 … 0.56003914 2.07338104 -0.9508484
3 -0.279415 1.5220258 … 1.92028386 0.64710839 -1.4841354
3.5 0.6569866 -4.004689 … 1.45429067 -1.5790948 0
4 0.9893582 0 0
iv. Take S0 = 2S1 - S2 and S8 = 2S7- S6, note that
S0 + 4S1 + S2 = (2S1 - S2) + 4S1 + S2 = 6S1 + 0 S2
S6 + 4S7 + S8 = S6 +4S7 + (2S7 – S6) = 0 S6 + 6 S7
the system of equations becomes
6 0 S1 - 18.56746903
1 4 1 S - 20.06409266
2
1 4 1 S3 = - 3.113882030
1 4 1 S 16.69921738
4
1 4 1 S 5 21.15913334
1 4 1 S 6 6.165439691
0 6 S 7 − 14.49673078
S 0 - 2.08938987
S - 3.09457817
1
S 2 - 4.09976647
=
S 3 - 0.57044861
S 4 3.26767889
S 5 4.19895042
S 6 1.09565277
S 7 - 2.41612180
S - 5.92789636
8
x y S-(iv) (d=y) c b a
0 0 0 0 2.28905513 -1.04469494 -0.33506277
0.5 0.841471 -3.654974 … 0.99306312 -1.54728909 -0.33506277
1 0.9092974 -3.947572 … -0.80552304 -2.04988323 1.17643928
1.5 0.14112 -0.618829 … -1.97307681 -0.28522431 1.27937584
2 -0.756802 3.3090051 … -1.29876924 1.63383945 0.31042384
2.5 -0.958924 4.0820256 … 0.56788809 2.09947521 -1.03443255
3 -0.279415 1.5220258 … 1.89153888 0.54782638 -1.17059152
3.5 0.6569866 -4.004689 … 1.56142163 -1.20806090 -1.17059152
4 0.9893582 0 0
v. Comparison of the interpolation results
(Note: the y values are generated using yexact = sin(2x))
1.2
1
f-exact
0.8 f-ii
0.6 f-iii
0.4 f-iv
0.2
0
-0.2 0 0.5 1 1.5 2 2.5 3 3.5 4 x
-0.4
-0.6
-0.8
-1
-1.2
0.04
0.03
0.02
0.01
0
-0.01
-0.02 Error-ii
-0.03 Error-iii
-0.04
Error-iv
-0.05
-0.06
0 1 2 3 4 x
Comments:
* At x=0, y=sin(2x) y"(0)=0.
Thus, the Natural Spline condition at x=0 is actually exact.
That is why the error (labeled as “Error-ii”) has the smallest magnitude
near x=0 among the three methods.
* However, at x=4, y"(4)≠0. Thus “Error-ii” has larger error near x=4
indicating that Natural Spline usually has larger error.
* Method iii (parabola at the ends) and method iv (“Not-a-knot”) perform
similarly for this problem.
Example 9 Setting up matrix for the least square fit method
Given the data below, set up the matrix for finding the linear and quadratic
polynomial fit for P2(x).
xi 0.05 0.11 0.15 0.31 0.46 0.52 0.70 0.74 0.82 0.98 1.17
yi 0.956 0.89 0.832 0.717 0.571 0.539 0.378 0.370 0.306 0.242 0.104
Solution:
* After we input (xi, yi) into the spreadsheet, we set up columns for xi^k…,
and xi^k*yi. The result is:
That is,