NA Ch3 Student
NA Ch3 Student
Xu Zhang
Department of Mathematics
Oklahoma State University
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 1 / 92
Chapter 3. Interpolation and Polynomial Approximation
Table of Contents
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 2 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.1 Interpolation and Lagrange Polynomials
where n is a nonnegative integer and a0 , . . . , an are real constants. One reason for their
Theorem 1 (Weierstrass Approximation Theorem).
importance is that they uniformly approximate continuous functions. By this we mean that
given any function, defined and continuous on a closed and bounded interval, there exists
Suppose f ∈ C[a, b]. For each > 0, there exists a polynomial P (x)
a polynomial that is as “close” to the given function as desired. This result is expressed
such that
precisely in the Weierstrass Approximation Theorem. (See Figure 3.1.)
|f (x) − P (x)| < , for all x ∈ [a, b].
y
y f(x) ε
y P(x)
y f(x)
y f(x) ε
a b x
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 4 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.1 Interpolation and Lagrange Polynomials
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 5 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.1 Interpolation and Lagrange Polynomials
Figure 3.2
y
20
y P5(x)
y ex
y P4(x)
15
y P3(x)
10
y P2(x)
5
y P1(x)
y P0(x)
1 1 2 3 x
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 7 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.1 Interpolation and Lagrange Polynomials
L1 (x0 ) = 0, L1 (x1 ) = 1.
Example 2.
Determine the linear Lagrange interpolating polynomial that passes
through the points (2, 4) and (5, 1).
Solution
In this case, we have
x−5 1 x−2 1
L0 (x) = = − (x − 5), and L1 (x) = = (x − 2).
2−5 3 5−2 3
3.1 Interpolation and the Lagra
y f (x)
y P(x)
x0 x1 x2 xn x
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 10 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.1 Interpolation and Lagrange Polynomials
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 11 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.1 Interpolation and Lagrange Polynomials
Theorem 4.
If x0 , x1 , · · · , xn are n + 1 distinct numbers and f is a function whose
values are given at these numbers, then a unique polynomial P (x) of
degree at most n exists with
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 13 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.1 Interpolation and Lagrange Polynomials
Example 5.
Solution (1/2)
The Lagrange polynomials associated with x0 = 2, x1 = 2.75, and x2 = 4
are
(x − 2.75)(x − 4) 2
L0 (x) = = (x − 2.75)(x − 4).
(2 − 2.75)(2 − 4) 3
(x − 2)(x − 4) 16
L1 (x) = = − (x − 2)(x − 4).
(2.75 − 2)(2.75 − 4) 15
(x − 2)(x − 2.75) 2
L2 (x) = = (x − 2)(x − 2.75).
(2.75 − 2)(4 − 2.75) 5
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 14 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.1 Interpolation and Lagrange Polynomials
Solution (2/2)
Also,
1 4 1
f (x0 ) = f (2) = , f (x1 ) = f (2.75) = , and f (x2 ) = f (4) =
2 11 4
So the second-degree Lagrange Interpolating polynomial is
X 2
P2 (x) = f (xk )Lk (x)
k=0
1 2 4 16
= · (x − 2.75)(x − 4) − · (x − 2)(x − 4)
2 3 11 15
1 2
+ · (x − 2)(x − 2.75)
4 5
1 2 35 49
= x − x+
22 88 44
f (n+1) (ξ)
f (x) = P (x) + (x − x0 )(x − x1 ) · · · (x − xn ),
(n + 1)!
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 16 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.1 Interpolation and Lagrange Polynomials
f (n+1) (ξ)
(x − x0 )n+1 .
(n + 1)!
f (n+1) (ξ)
(x − x0 )(x − x1 ) · · · (x − xn ).
(n + 1)!
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 17 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.1 Interpolation and Lagrange Polynomials
Example 7.
In Example 5, we found the 2nd-degree Lagrange polynomial P2 (x) for
1
f (x) = on [2, 4] using the nodes x0 = 2, x1 = 2.75, x2 = 4 Determine the
x
error form of this polynomial, and the maximum error when the polynomial is
used to approximate f (x) for x ∈ [2, 4].
Solution (1/2)
f 000 (ξ) 6ξ −4 1 1
|f (x) − P (x)| = g(x) = |g(x)| = 4 |g(x)| ≤ |g(x)|
3! 6 ξ 16
35 2 49
where, g(x) = (x − 2)(x − 2.75)(x − 4) = x3 − 4 x + 2 x − 22.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 18 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.1 Interpolation and Lagrange Polynomials
Solution (2/2)
We now need to determine the maximum value of |g(x)| on x ∈ [2, 4].
35 49 1
g 0 (x) = 3x2 −
x+ = (3x − 7)(2x − 7).
2 2 2
7 7
The critical points are x1 = and x2 = . The global extrema are
3 2
among the critical points and endpoints x = 2 and x = 4.
7 25 7 9
g(2) = 0, g( ) = , g( ) = − , g(4) = 0.
3 108 2 16
Hence, the maximum error is
1 9 9
max |f (x) − P (x)| ≤ − = ≈ 0.0351526.
x∈[2,4] 16 16 256
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 19 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.1 Interpolation and Lagrange Polynomials
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 20 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.1 Interpolation and Lagrange Polynomials
L = LagP oly(dataX, k, x)
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 21 / 92
olation and Polynomial Approximation
Chapter 3. Interpolation and Polynomial Approximation 3.1 Interpolation and Lagrange Polynomials
Introduction
Exercise
A census of the population of the United States is taken every 10 years. The following
tionA census tableofliststhe
thepopulation
population, inof the United
thousands States
of people, fromis1950
taken every
to 2000, and10
theyears.
data are The
also
f the following
population of the table lists
United States the
is taken population,
every in
10 years. The following thousands of people, from 1950 to
represented in the figure.
e population, in thousands of people, from 1950 to 2000, and the data are also
in the2000,
figure. and the data are also represented in the figure.
1950 Year
1960 1970 19801950 1990 1960
2000 1970 1980 1990 2000
151,326 179,323 203,302 226,542 249,633 281,422
) Population 151,326 179,323 203,302 226,542 249,633 281,422
(in thousands)
P(t)
1950 1960 1970 1980 1990 2000 t Then use the interpolation polynomial
Year for prediction.
1
Xu Zhang (Oklahoma State University) 10 8 MATH 4513 Numerical Analysis Fall 2020 22 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.2 Divided Differences
where
n
Y (x − xi )
Ln,k (x) = .
i6=k
(xk − xi )
i=0
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 24 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.2 Divided Differences
f [xi+1 ] − f [xi ]
f [xi , xi+1 ] =
xi+1 − xi
The second divided difference of f at xi , xi+1 and xi+2 is
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 25 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.2 Divided Differences
In general, we have
ak = f [x0 , x1 , · · · , xk ], for all k = 0, 1, · · · , n.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 26 / 92
k=1
Chapter 3. Interpolation and Polynomial Approximation 3.2 Divided Differences
The value of f [x0 , x1 , . . . , xk ] is independent of the order of the numbers x0 , x1 , . . . , xk , as
shown in Exercise 21.
The generation of the divided differences is outlined in Table 3.9. Two fourth and one
fifth difference can also be determined from these data.
Divided Difference Table
Table 3.9
First Second Third
x f (x) divided differences divided differences divided differences
x0 f [x0 ]
f [x1 ] − f [x0 ]
f [x0 , x1 ] =
x1 − x0
f [x1 , x2 ] − f [x0 , x1 ]
x1 f [x1 ] f [x0 , x1 , x2 ] =
x2 − x 0
f [x2 ] − f [x1 ] f [x1 , x2 , x3 ] − f [x0 , x1 , x2 ]
f [x1 , x2 ] = f [x0 , x1 , x2 , x3 ] =
x2 − x1 x3 − x0
f [x2 , x3 ] − f [x1 , x2 ]
x2 f [x2 ] f [x1 , x2 , x3 ] =
x3 − x1
f [x3 ] − f [x2 ] f [x2 , x3 , x4 ] − f [x1 , x2 , x3 ]
f [x2 , x3 ] = f [x1 , x2 , x3 , x4 ] =
x3 − x2 x4 − x 1
f [x3 , x4 ] − f [x2 , x3 ]
x3 f [x3 ] f [x2 , x3 , x4 ] =
x4 − x 2
f [x4 ] − f [x3 ] f [x3 , x4 , x5 ] − f [x2 , x3 , x4 ]
f [x3 , x4 ] = f [x2 , x3 , x4 , x5 ] =
x4 − x3 x5 − x2
f [x4 , x5 ] − f [x3 , x4 ]
x4 f [x4 ] f [x3 , x4 , x5 ] =
x5 − x 3
f [x5 ] − f [x4 ]
f [x4 , x5 ] =
x5 − x4
x5 f [x5 ]
Example 9.
Compute a divided difference table for these function values:
x 3 1 5 6
f (x) 1 −3 2 4
Solution (1/2)
Arrange the table vertically to have
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 28 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.2 Divided Differences
Solution (2/2)
Note that the values marked in red and blue will be used for constructing the
interpolating polynomial
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 29 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.2 Divided Differences
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 30 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.2 Divided Differences
C = divdifTable(dataX,dataY)
For j = 1 : n
For i = 0 : n − j
ci+1,j−1 − ci,j−1
cij = (note: cij = f [xi , xi+1 , · · · , xi+j ])
xi+j − xi
End
End
x0 y0 c00 c01 c02 c03 · · · c0,n−1 c0n
x1 y1 c10 c11 c12 c13 · · · c1,n−1
x2 y2 c20 c21 c22 c23 · · ·
.. .. .. .. ..
. . . . .
xn−1 yn−1 cn−1,0 cn−1,1
xn yn cn0
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 31 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.2 Divided Differences
f [z0 , z1 , · · · , zn ] = f [x0 , x1 , · · · , xn ].
f (n) (ξ)
f [x0 , x1 , · · · , xn ] = .
n!
(Hint: use the generalized Rolle’s theorem.)
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 32 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.3 Hermite Interpolation
In particular, we look for a polynomial P (x) of the least degree such that
P (x) and P 0 (x) agrees with f and its derivative f 0 at n + 1 distinct points
x0 , x1 , · · · , xn , i.e.,
P (xi ) = f (xi )
i = 0, 1, · · · , n.
P 0 (xi ) = f 0 (xi )
This is called Hermite interpolation.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 33 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.3 Hermite Interpolation
Ak (xi ) = δik .
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 34 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.3 Hermite Interpolation
Thus,
A0k (xk ) = −2L0k (xk ) + 2L0k (xi ) = 0,
and
A0k (xi ) = 0, for i 6= k.
In summary, we have
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 35 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.3 Hermite Interpolation
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 36 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.3 Hermite Interpolation
Hermite Interpolation
If f ∈ C 1 [a, b] and x0 , x1 · · · , xn ∈ [a, b] are distinct, the unique
polynomial of least degree agreeing with f and f 0 at x0 , x1 · · · , xn is
the Hermite polynomial of degree at most 2n + 1 given by
n
X n
X
H2n+1 (x) = f (xk )Ak (x) + f 0 (xk )Bk (x).
k=0 k=0
where
Ak (x) = 1 − 2(x − xk )L0n,k (xk ) L2n,k (x),
Bk (x) = (x − xk )L2n,k (x),
n
Y (x − xi )
and Ln,k (x) = .
i6=k
(xk − xi )
i=0
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 37 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.3 Hermite Interpolation
Example 10.
Determine the Hermite polynomial that interpolates the data below
x 1 2
f (x) 1 −3
f 0 (x) −1 2
Solution(1/2)
First compute the Lagrange polynomials of the point x0 = 1 and x1 = 2.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 38 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.3 Hermite Interpolation
Solution (2/2)
Then, the Hermite interpolating polynomial is
H3 (x) = f (x0 )A0 (x) + f (x1 )A1 (x) + f 0 (x0 )B0 (x) + f 0 (x1 )B1 (x)
= 1 · (2x − 1)(x − 2)2 + (−3) · −(2x − 5)(x − 1)2
+(−1) · (x − 1)(x − 2)2 + 2 · (x − 2)(x − 1)2
= 9x3 − 39x2 + 50x − 19.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 39 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.3 Hermite Interpolation
z2i = z2i+1 = xi , i = 0, 1, · · · n.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 40 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.3 Hermite Interpolation
x0 x1 x2 ··· xn
f (x0 ) f (x1 ) f (x2 ) · · · f (xn )
f 0 (x0 ) f 0 (x1 ) f 0 (x2 ) · · · f 0 (xn )
can be represented by zi , 0 ≤ i ≤ 2n + 1
x0 x0 x1 x1 x2 ··· xn−1 xn xn
z0 z1 z2 z3 z4 ··· z2n−1 z2n z2n+1
f (z0 ) f (z1 ) f (z2 ) f (z3 ) f (z4 ) · · · f (z2n−1 ) f (z2n ) f (z2n+1 )
f (x0 ) f (x0 ) f (x1 ) f (x1 ) f (x2 ) · · · f (xn−1 ) f (xn ) f (xn )
2n+1
xample 2 Use the data given in Example
X 1 and the divided difference method to determine the Hermite
H2n+1
polynomial approximation at xf=
(x) = f [z 0 ] + , z1 , · · · , zk ](x − z0 )(x − z1 ) · · · (x − zk−1 ).
[z01.5.
k=1
Solution The underlined entries in the first three columns of Table 3.17 are the data given
in Example 1. The remaining entries in this table are generated by the standard divided-
Xu Zhang (Oklahoma
differenceState University)
formula (3.9). MATH 4513 Numerical Analysis Fall 2020 42 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.3 Hermite Interpolation
Example 11.
Determine the Hermite polynomial that interpolates the data below
x 1 2
f (x) 1 −3
f 0 (x) −1 2
Solution
The divided difference table is
z0 =1 f [z0 ] = 1 f [z0 , z1 ] = −1 f [z0 , z1 , z2 ] = −3 f [z0 , z1 , z2 , z3 ] = 9
z1 =1 f [z1 ] = 1 f [z1 , z2 ] = −4 f [z1 , z2 , z3 ] = 6
z2 =2 f [z2 ] = −3 f [z2 , z3 ] = 2
z3 =2 f [z3 ] = −3
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 43 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.3 Hermite Interpolation
For the error bound of the Hermite interpolation, we have the following
result.
Theorem 12 (Hermite Interpolation Error Theorem).
f (2n+2) (ξ)
f (x) = H2n+1 (x) + (x − x0 )2 (x − x1 )2 · · · (x − xn )2 ,
(2n + 2)!
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 44 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.3 Hermite Interpolation
Example 13.
A baseball pitcher throws a fastball from the pitcher’s mound to the catcher.
Although the distance from the mound to the home plate is 60 feet 6 inches,
the ball typically travels about 55 feet 6 inches. Suppose the initial velocity of
the ball is 95 miles per hour (mph), and the terminal velocity is 92 mph.
Construct a Hermite interpolating polynomial for the data
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 45 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.3 Hermite Interpolation
Solution (1/3)
Convert “miles per hour” to “feet per second” (note 1 mph = 1.46667 fps).
Rewrite the table as follow
Time t (in seconds) 0 0.4
Distance d(t) (in feet) 0 55.5
Speed s(t) = d0 (t)(in fps) 139.33 134.93
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 46 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.3 Hermite Interpolation
Solution (2/3)
The speed can be approximated by
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 47 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.3 Hermite Interpolation
Solution (3/3)
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 48 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
4 7
3 6
2 5
1 4
3
1 2 3 4 5 6 7 8 9 10 11 12 13 x
2
-1
-2
0 2 4 6 8 10 12 14
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 49 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
y f (x)
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 50 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 51 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 52 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
S n2
S n1
S1 Sj
S j1
S0
S j (x j1) f (x j1) S j1(x j1)
S j (x j1) Sj1(x j1)
S j(x j1) S j1
(x j1)
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 53 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
x0 x1 x2 ··· xn
f (x0 ) f (x1 ) f (x2 ) · · · f (xn )
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 54 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Example 14.
Construct a natural spline that passes through the points (1, 2), (2, 3), and
(3, 5).
Solution (1/2)
This spline consists of two cubics. The two subintervals are [1, 2] and
[2, 3]. We write the piecewise cubic polynomial as follows
(
S0 (x) = a0 + b0 (x − 1) + c0 (x − 1)2 + d0 (x − 1)3 , on [1, 2]
S(x) =
S1 (x) = a1 + b1 (x − 2) + c1 (x − 2)2 + d1 (x − 2)3 , on [2, 3].
S0 (1) = 2 =⇒ a0 = 2,
S0 (2) = 3 =⇒ b0 + c0 + d0 = 1,
S1 (2) = 3 =⇒ a1 = 3,
S1 (3) = 5 =⇒ b1 + c1 + d1 = 2,
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 55 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Solution (2/2)
Two conditions come from derivatives at interior nodes x1 = 2.
The last two conditions are from the natural boundary conditions
x0 x1 x2 ··· xn
f (x0 ) f (x1 ) f (x2 ) · · · f (xn )
S(x)|Ij = Sj (x) = aj + bj (x − xj ) + cj (x − xj )2 + dj (x − xj )3 .
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 57 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Sj (x) = aj + bj (x − xj ) + cj (x − xj )2 + dj (x − xj )3 .
aj = f (xj ), j = 0, 1, · · · , n − 1. (3.1)
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 60 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
%% Initialize h
n = length(dataX) - 1;
h = zeros(1,n);
for j = 1:n
h(j) = dataX(j+1) - dataX(j);
end
%% Prepare for Matrix A
A = zeros(n+1,n+1);
A(1,1) = 1; A(n+1,n+1) = 1;
for j = 2:n
A(j,j-1) = h(j-1);
A(j,j+1) = h(j);
A(j,j) = 2*(h(j-1)+h(j));
end
%% Prepare for vector bb
bb = zeros(n+1,1);
for j = 2:n
bb(j) = 3/h(j)*(dataY(j+1)-dataY(j)) - 3/h(j-1)*(dataY(j)-dataY(j-1));
end
%% Solve A*c = bb
c = A\bb;
%% Find a,b,d.
a = reshape(dataY(1:n+1),n+1,1);
b = zeros(n,1);
d = zeros(n,1);
for j = 1:n
b(j) = 1/h(j)*(a(j+1)-a(j)) - h(j)/3*(2*c(j)+c(j+1));
d(j) = (c(j+1) - c(j))/(3*h(j));
end
%% Remove the last entries of a and c
a(n+1) = [];
c(n+1) = [];
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 61 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 62 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Example 15.
At the beginning of Chapter 3, we gave some Taylor polynomial to
approximate the exponential function f (x) = ex . Use the data points
x 0 1 2 3
f (x) 1 e e2 e3
Solution (1/3)
Since this problem involves extensive calculation, we write a Matlab
code to solve this problem
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 63 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Solution (2/3)
9/14/20 3:41 PM /Users/xuzhang/Dropbox/Teachi.../ex3_4_1.m 1 of 1
The MATLAB driver file for this example
% ex 3.4.1
clc
dataX = [0,1,2,3];
dataY = [exp(0),exp(1),exp(2),exp(3)];
%% query point x
x = 0:0.01:3;
y = natural_spline(dataX,dataY,x);
%% plot
figure(1)
plot(dataX,dataY,'r*','linewidth',2)
hold on
plot(x,exp(x),'b-','linewidth',2)
plot(x,y,'k-.','linewidth',2)
lgd = legend('data points','y = e^x','y = S(x)');
hold off
grid on
lgd.FontSize = 16;
lgd.Location = 'NorthWest';
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 64 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Solution (3/3)
3
1.0000 + 1.4660x + 0.2523x ,
on [0, 1],
2 3
S(x) = 2.7183 + 2.2229(x − 1) + 0.7569(x − 1) + 1.69107(x − 1) , on [1, 2],
7.3891 + 8.8098(x − 2) + 5.8301(x − 2)2 − 1.9434(x − 2)3 , on [2, 3].
20
data points
18
y = ex
16 y = S(x)
14
12
10
0
0 0.5 1 1.5 2 2.5 3
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 65 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Clamped Splines
The clamped spline is another type of cubic splines that use
different boundary conditions.
Comparing with the free boundary condition in the natural spline,
S 00 (a) = 0, S 00 (b) = 0,
Example 16.
We revisit Example 14, and this time we construct a clamped spline that
passes through the points (1, 2), (2, 3), and (3, 5) that has S 0 (1) = 2 and
S 0 (3) = 1.
Solution (1/2)
There are two pieces in the spline S(x):
Solution (2/2)
The clamped boundary conditions yield
s00 (1) = 2 =⇒ b0 = 2,
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 68 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
The coefficients {aj }, {bj }, and {dj } are define as (3.1) and (3.2).
3 3
hj−1 cj−1 +2(hj−1 +hj )cj +hj cj+1 = (aj+1 −aj )− (aj −aj−1 ).
hj hj−1
This matrix
This matrix AAisisalso
alsostrictly
strictlydiagonally dominant,
diagonally so itsosatisfies
dominant, the conditions
it satisfies of
the conditions
Theorem 6.21 in Section 6.6. Therefore, the linear system has a unique solution for
Theorem 6.21 in Section 6.6. Therefore, the linear system has a unique solution f
Xu Zhang c0 , c1 , . .State
(Oklahoma . , cnUniversity)
. MATH 4513 Numerical Analysis Fall 2020 70 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Matlab File
9/14/20 5:14 for Clamped Spline Coefficient
PM /Users/xuzhang/Dr.../clamped_spline_coef.m 1 of 1
%% Initialize h
n = length(dataX) - 1;
h = zeros(1,n);
for j = 1:n
h(j) = dataX(j+1) - dataX(j);
end
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 71 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 72 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Example 17.
We revisit the spline interpolation of f (x) = ex at the following points
x 0 1 2 3
f (x) 1 e e2 e3 .
This time we use the clamped spline with the additional information
f 0 (0) = 1, and f 0 (3) = e3 . Then, compare the accuracy with the natural
spline interpolation.
Solution (1/3)
Since it involved extensive calculation, we solve the problem using
Matlab programing.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 73 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
9/14/20
Solution5:24 PM /Users/xuzhang/Dropbox/Teachi.../ex3_4_2.m
(2/4) 1 of 1
% ex 3.4.2
clc
dataX = [0,1,2,3];
dataY = [exp(0),exp(1),exp(2),exp(3)];
%% query point x
x = 0:0.01:3;
y = natural_spline(dataX,dataY,x);
y2 = clamped_spline(dataX,dataY,exp(0),exp(3),x);
%% plot
figure(1)
plot(dataX,dataY,'r*','linewidth',2)
hold on
plot(x,exp(x),'b-','linewidth',2)
plot(x,y,'k-.','linewidth',2)
plot(x,y2,'g-.','linewidth',2)
lgd = legend('data points','y = e^x','Natural','Clamped');
hold off
grid on
lgd.FontSize = 16;
lgd.Location = 'NorthWest';
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 74 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Solution (3/4)
The natural spline is
3
1.0000 + 1.4660x + 0.2523x ,
on [0, 1],
2 3
S(x) = 2.7183 + 2.2229(x − 1) + 0.7569(x − 1) + 1.6911(x − 1) , on [1, 2],
7.3891 + 8.8098(x − 2) + 5.8301(x − 2)2 − 1.9434(x − 2)3 , on [2, 3].
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 75 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Solution (4/4)
20
data points
18
y = ex
16 Natural
Clamped
14
12
10
0
0 0.5 1 1.5 2 2.5 3
From the plot, we can see that the clamped spline is more accurate than
the natural spline.
This is not surprise since the boundary conditions for the clamped spline
are exact.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 76 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
5M
|f (x) − S(x)| ≤ max (xj+1 − xj )4 .
384 0≤j≤n−1
Remark
A fourth-order error bound also holds in the case of natural spline
interpolation, but it is more difficult to express.
There are other cubic spline interpolations that do not require the
derivative of f . For example, the popular “not-a-knot spline” requires that
the third-order derivative S 000 (x) is continuous at x1 and xn−1 .
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 77 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Table 3.18
Illustration Figure 3.11 shows a ruddy duck in flight. To approximate the top profile of the duck, we
have chosen points along
polation and Polynomial Approximation x 0.9the1.3 curve
1.9 2.1through
2.6 3.0 3.9 4.4 4.7
which 5.0 6.0 the
we want 7.0approximating
8.0 9.2 10.5 11.3curve
11.6 12.0 12.6 13.0 13.3
to pass.
Example 19.
Table 3.18 lists thefcoordinates
(x) 1.3 1.5 1.85of2.121
2.6data points
2.7 2.4 relative
2.15 2.05 2.1 2.25to2.3
the superimposed
2.25 coordinate
1.95 1.4 0.9 0.7 0.6 0.5 0.4 0.25
In this
gure 3.11 shows aexample,
ruddysystem we To
duck inshown
flight. approximate
in approximate
Figure 3.12. top the
theNotice top
that
profile theprofile
of more points
duck, we areof used
the whenducktheusing
curve iscubic
changing spline
ve chosen points along the curve through
rapidly whichitwe
than when iswant the approximating
changing more slowly.curve to pass.
interpolation.
ble 3.18 lists the coordinates of 21 data points relative
Figure 3.12 to the superimposed coordinate
stem shown in Figure 3.12. Notice that more points are used when the curve
f (x) is changing
Figure
pidly than when3.11
it is changing more slowly.
4
3
2
1
1 2 3 4 5 6 7 8 9 10 11 12 13 x
In general, the more points we use, the better approximation we can expect.
Using Algorithm 3.4 to generate the natural cubic spline for this data produces the coeffi-
We choose 21 data points as cients depicted above
shown in Table and
3.19. This splineshown below
curve is nearly identical toin
thethe
profile,table.
as shown in
Figure 3.13.
9
Note
2.1 Table
that
2.6 3.03.18
more points are placed where the curve is changing more rapidly.
3.9 4.4 4.7 5.0 6.0 7.0 8.0 9.2 10.5 11.3 11.6 12.0 12.6 13.0 13.3
85 2.1 2.6 2.7 2.4 2.15 2.05 2.1 2.25 2.3 2.25 1.95 1.4 0.9 0.7 0.6 0.5 0.4 0.25
x 0.9 1.3 1.9 2.1 2.6 3.0 3.9 4.4 4.7 5.0 6.0 7.0 8.0 9.2 10.5 11.3 11.6 12.0 12.6 13.0 13.3
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
f (x) 1.3 1.5 1.85 2.1 2.6 2.7 2.4 2.15 2.05 2.1 2.25 2.3 2.25 1.95 1.4 0.9 0.7 0.6 0.5 0.4 0.25
f (x)
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 78 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Solution (1/4)
Since we don’t have derivative information, we use the natural spline
interpolation.
9/14/20 6:15 PM /Users/xuzhang/Dropbox/Teachi.../ex3_5_i.m 1 of 1
We write a Matlab driver file for this example.
% ex 3.5.illustration
clear; clc;
%% Input
dataX = [0.9 1.3,1.9 2.1 2.6 3.0 3.9 4.4 4.7 5.0 6.0 7.0 8.0 ...
9.2 10.5 11.3 11.6 12.0 12.6 13.0 13.3];
dataY = [1.3 1.5 1.85 2.1 2.6 2.7 2.4 2.15 2.05 2.1 2.25 2.3 2.25 ...
1.95 1.4 0.9 0.7 0.6 0.5 0.4 0.25];
[a,b,c,d] = natural_spline_coef(dataX,dataY); % coefficent of cubic spline
%% Display Outputs
disp('---------------------------------------------------')
disp(' j a b c d')
disp('---------------------------------------------------')
formatSpec = '%2i % .5f % .5f % .5f % .5f \n';
fprintf(formatSpec,[(0:length(dataX)-2)',a,b,c,d]')
%% Plot Spline
x = 0.9:0.01:13.3; % query points
y = natural_spline(dataX,dataY,x);
figure(1)
clf;
plot(x,y,'r-','linewidth',2)
hold on
plot(dataX,dataY,'b*','linewidth',2)
axis([0,14,-6,4])
grid on
hold off
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 79 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Table 3.18
Solution (2/4)
x 0.9 1.3 1.9 2.1 2.6 3.0 3.9 4.4 4.7 5.0 6.0 7.0 8.0 9.2 10.5 11.3 11.6 12.0 12.6 13.0 13.3
1
2
1
0
1 2 3 4 5 6 7 8 9 10 11 12 13 x
-1
-2
-3
-4
-5
0 2 4 6 8 10 12 14
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 80 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Solution (3/4)
9/14/20we
For comparison, 6:23
alsoPMuse/Users/xuzhang/Dropbox/Teach.../ex3_5_i2.m
Lagrange Interpolation. 1 of
%% Input
dataX = [0.9 1.3,1.9 2.1 2.6 3.0 3.9 4.4 4.7 5.0 6.0 7.0 8.0 ...
9.2 10.5 11.3 11.6 12.0 12.6 13.0 13.3];
dataY = [1.3 1.5 1.85 2.1 2.6 2.7 2.4 2.15 2.05 2.1 2.25 2.3 2.25 ...
1.95 1.4 0.9 0.7 0.6 0.5 0.4 0.25];
%% Plot Interpolations
figure(1)
clf;
plot(dataX,dataY,'b*','linewidth',2)
hold on
plot(x,y,'r-','linewidth',2)
plot(x,yy,'k-','linewidth',2)
axis([0,14,-2,7])
hold off
grid on
lgd = legend('Data Point','Natural Spline','Lagrange');
lgd.FontSize = 16;
lgd.Location = 'NorthWest';
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 81 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.4 Cubic Spline Interpolation
Solution (4/4)
Plotting the Lagrange interpolation, we observe that the 20th-degree
polynomial 160oscillates
C H A P T E R wildly.
3 It produces
Interpolation aApproximation
and Polynomial very strange illustration of the
back of a duck.
Figure 3.14
f (x)
7
4
6
3
2
5
1
4
3 1 2 3 4 5 6 7 8 9 10 11 12 x
2
-1
-2
0 2 4 6 8 10 12 14
4
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 83 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.5 Parametric Curves
Figure 3.15
y
1
1 1 x
1
Example 1 Construct a pair of Lagrange polynomials to approximate the curve shown in Figure 3.15,
Xu Zhang (Oklahoma State University) MATH
using the data points 4513
shown on theNumerical
curve. Analysis Fall 2020 84 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.5 Parametric Curves
x0 x1 x2 ··· xn
y0 y1 y2 ··· yn
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 85 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.5 Parametric Curves
1 1 x
1
i 0 1 2 3 4
The following example demonstrates the technique in the case where both approximat-
ing functions are Lagrange interpolating polynomials.
Example 1
t 0 0.25 0.5 0.75 1
Construct aipair of Lagrange polynomials to approximate the curve shown in Figure 3.15,
using the data points shown on the curve.
x i 1 0 1 0 −1
Solution There is flexibility in choosing the parameter, and we will choose the points
yi spaced−1
{t } equally
4
i i=0 0 the0.5
in [0,1], which gives data in Table 3.20.1 0
Table 3.20
i 0 1 2
Xu Zhang (Oklahoma State University) MATH 45133 Numerical
4
Analysis Fall 2020 86 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.5 Parametric Curves
Solution (2/3)
We write a MATLAB Driver file3:06
9/15/20 for this
PM example
/Users/xuzhang/Dropbox/Teachi.../ex3_6_1.m
% ex3_6_1
dataT = [0,0.25,0.5,0.75,1];
dataX = [-1,0,1,0,1];
dataY = [0,1,0.5,0,-1];
qt = 0:0.001:1;
qx = LagrangeInterpolation(dataT,dataX,qt);
qy = LagrangeInterpolation(dataT,dataY,qt);
figure(1); clf
plot(dataX,dataY,'r*','linewidth',2)
hold
plot(qx,qy,'b-.','linewidth',2)
grid on
lgd = legend('Data Point','Lagrange');
lgd.FontSize = 16;
lgd.Location = 'NorthEast';
y Data Point
Lagrange
1
1
0.5
1 1 x 0
-0.5
1
-1
-1.5 -1 -0.5 0 0.5 1 1.5
Remark
For this example, we can also use the natural cubic Spline interpolation for
the parametric
9/15/20 3:18 PM system.
/Users/xuzhang/Dropbox/Teac.../ex3_6_1_2.m 1 of 1
% ex3_6_1_2 1.5
dataT = [0,0.25,0.5,0.75,1]; Data Point
dataX = [-1,0,1,0,1];
Lagrange
dataY = [0,1,0.5,0,-1];
1
Natural Spline
qt = 0:0.001:1;
qx = LagrangeInterpolation(dataT,dataX,qt);
qy = LagrangeInterpolation(dataT,dataY,qt); 0.5
qxS = natural_spline(dataT,dataX,qt);
qyS = natural_spline(dataT,dataY,qt);
0
figure(1); clf
plot(dataX,dataY,'r*','linewidth',2)
hold
plot(qx,qy,'b-.','linewidth',2) -0.5
plot(qxS,qyS,'k-.','linewidth',2)
grid on
lgd = legend('Data Point','Lagrange','Natural Spline');
lgd.FontSize = 16; -1
lgd.Location = 'NorthEast'; -1.5 -1 -0.5 0 0.5 1 1.5
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 89 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.5 Parametric Curves
Example 21.
In this example, we demonstrate using natural cubic spline to interpolate
arbitrary hand-drawn curve. We need to use the graphic input MATLAB
command
[X,Y]=ginput(N)
to get N points from the click of the mouse.
(for more details, type help ginput in MATLAB command window)
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 90 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.5 Parametric Curves
9/15/20
A MATLAB Driver File 3:45 PM /Users/xuzhang/Dropbox/Teachi.../ex3_6_2.m
for Parametric Curve 1
% ex3_6_2
clc
clear
close all
for n = 1:N
[X,Y] = ginput(1);
plot(X,Y,'r*');
text(X,Y,int2str(n));
grid on
axis([0,1,0,1])
hold on
dataX(n) = X; dataY(n) = Y;
end
disp(' ')
disp(' I dataT dataX dataY')
disp('---------------------------------------')
disp([(1:N)',dataT,dataX,dataY])
%% Query Points
qt = 0:0.001:1; % use 1000 query points
qx = natural_spline(dataT,dataX,qt);
qy = natural_spline(dataT,dataY,qt);
figure(2);clf
plot(dataX,dataY,'r*',qx,qy,'k-','linewidth',2)
grid on
axis([0,1,0,1])
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 91 / 92
Chapter 3. Interpolation and Polynomial Approximation 3.5 Parametric Curves
Remarks
Applications in computer graphics require the rapid generation of
smooth curves that can be easily and quickly modified. For both
aesthetic and computational reasons, changing one portion of
these curves should have little or no effect on other portions of the
curves.
This eliminates the use of interpolating polynomials and splines
since changing one portion of these curves affects the whole
curve.
The choice of curve for use in computer graphics is generally a
form of the piecewise cubic Hermite polynomial. Popular graphics
programs using Hermite cubics are described as Bézier
polynomials, which uses the “guidepoint” to compute the
derivatives at the endpoints in each interval.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 92 / 92