Numerical Methods Study Guide
Numerical Methods Study Guide
Methods
1 Polynomial Interpolation 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Lagrange Interpolating Polynomial . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Newton’s Divided Difference Formula . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Spline Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Numerical Differentiation 9
3.1 General Approach: Differentiating Polynomials . . . . . . . . . . . . . . . . . . . 9
3.1.1 Formulas from n = 1 Polynomial (P1 ) . . . . . . . . . . . . . . . . . . . . 9
3.1.2 Formulas from n = 2 Polynomial (P2 ) . . . . . . . . . . . . . . . . . . . . 9
3.2 Taylor Series Derivations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.2.1 First Derivative Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.2.2 Higher-Order First Derivative Formulas . . . . . . . . . . . . . . . . . . . 11
3.2.3 Second Derivative Formulas . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.3 Total Error and Optimal h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.4 Examples and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
iii
Contents Contents
iv
Polynomial Interpolation
Chapter 1
Polynomial Interpolation
1.1 Introduction
Definition 1.1.1 (Interpolation). Given a set of n + 1 distinct data points (xi , yi ) for i =
0, 1, . . . , n, where yi = f (xi ), the goal of interpolation is to find a polynomial Pn (x) of degree
at most n such that:
Pn (xi ) = yi = f (xi ) for all i = 0, 1, . . . , n
This Pn (x) is called the interpolating polynomial.
Theorem 1.1.3 (Existence and Uniqueness). There exists a unique polynomial Pn (x) of degree
at most n that interpolates the n + 1 distinct points (x0 , y0 ), . . . , (xn , yn ).
Theorem 1.1.4 (Interpolation Error). Let f ∈ C n+1 [a, b] and let Pn (x) be the unique interpo-
lating polynomial for f (x) at n + 1 distinct nodes x0 , . . . , xn ∈ [a, b]. Then for any x ∈ [a, b],
there exists a ξ(x) ∈ (a, b) such that:
f (n+1) (ξ(x))
f (x) − Pn (x) = (x − x0 )(x − x1 ) . . . (x − xn )
(n + 1)!
Definition 1.2.1 (Lagrange Basis Polynomials). For n + 1 nodes x0 , . . . , xn , the k-th Lagrange
basis polynomial (for k = 0, . . . , n) is defined as:
n
(x − xi )
Ln,k (x) =
Y
i=0,i6=k (xk − xi )
1
1.3 Newton’s Divided Difference Formula Polynomial Interpolation
1 if k = j
These basis polynomials have the property: Ln,k (xj ) = δkj = .
0 if k =
6 j
k=0
Example 1.2.3 (Quadratic Lagrange Polynomial, n = 2). Given (x0 , f (x0 )), (x1 , f (x1 )), (x2 , f (x2 )):
P2 (x) = f (x0 )L2,0 (x) + f (x1 )L2,1 (x) + f (x2 )L2,2 (x)
(x − x1 )(x − x2 ) (x − x0 )(x − x2 )
= f (x0 ) + f (x1 )
(x0 − x1 )(x0 − x2 ) (x1 − x0 )(x1 − x2 )
(x − x0 )(x − x1 )
+ f (x2 )
(x2 − x0 )(x2 − x1 )
Problem 1.2.4. For f (x) = cos x, let x0 = 0.3, x1 = 0.6, x2 = 0.9. Construct an interpolating
polynomial of degree at most 2 to approximate f (0.45).
Solution 1. First, find the function values: f (x0 ) = cos(0.3) = 0.95534 f (x1 ) = cos(0.6) =
0.82534 f (x2 ) = cos(0.9) = 0.62161
The polynomial P2 (x) is (using the formula from the example above):
(0.45 − 0.6)(0.45 − 0.9) (0.45 − 0.3)(0.45 − 0.9)
P2 (0.45) = (0.95534) + (0.82534)
(0.3 − 0.6)(0.3 − 0.9) (0.6 − 0.3)(0.6 − 0.9)
(0.45 − 0.3)(0.45 − 0.6)
+ (0.62161)
(0.9 − 0.3)(0.9 − 0.6)
(−0.15)(−0.45) (0.15)(−0.45) (0.15)(−0.15)
= (0.95534) + (0.82534) + (0.62161)
(−0.3)(−0.6) (0.3)(−0.3) (0.6)(0.3)
0.0675 −0.0675 −0.0225
= (0.95534) + (0.82534) + (0.62161)
0.18 −0.09 0.18
= (0.95534)(0.375) + (0.82534)(0.75) + (0.62161)(−0.125)
= 0.35825 + 0.61900 − 0.07770
= 0.89955
2
Polynomial Interpolation 1.3 Newton’s Divided Difference Formula
Problem 1.3.3. Construct the Newton’s forward interpolating polynomial for the following
data:
x 0 1 2 3 4
f (x) 0 1 8 27 64
3
1.4 Spline Interpolation Polynomial Interpolation
Definition 1.4.1 (Cubic Spline). A function S(x) is a cubic spline interpolant for the data
(x0 , f (x0 )), . . . , (xn , f (xn )) if it satisfies:
4
Polynomial Interpolation 1.4 Spline Interpolation
This leads to 4n unknowns (4 coefficients for each of n intervals) but only 4n − 2 equations.
We need 2 more "boundary conditions".
Definition 1.4.2 (Natural Cubic Spline). A cubic spline is natural if it satisfies the boundary
conditions:
S 00 (x0 ) = 0 and S 00 (xn ) = 0
Definition 1.4.3 (Clamped Cubic Spline). A cubic spline is clamped if the first derivatives
at the endpoints are specified:
5
1.4 Spline Interpolation Polynomial Interpolation
6
Least Squares Approximation
Chapter 2
Definition 2.1.1 (Linear Least Squares). Given n data points (xi , yi ), i = 1, . . . , n, we want
to find the line y(x) = a0 + a1 x that "best" fits the data. We define "best" by minimizing the
sum of the squared errors (SSE).
The error for each point is ei = y(xi ) − yi = (a0 + a1 xi ) − yi . We want to minimize the total
error function E(a0 , a1 ):
n n
E(a0 , a1 ) = (y(xi ) − yi ) =2
(a0 + a1 xi − yi )2
X X
i=1 i=1
To find the minimum, we take the partial derivatives with respect to a0 and a1 and set them
to zero:
n
∂E
= 2(a0 + a1 xi − yi )(1) = 0
X
∂a0 i=1
n
∂E
= 2(a0 + a1 xi − yi )(xi ) = 0
X
∂a1 i=1
7
2.2 Non-linear Data Linearization Least Squares Approximation
xi yi − ( xi )( yi )
P P P
n
a1 =
n x2i − ( xi )2
P P
x2i yi − xi xi yi
P P P P
a0 = = ȳ − a1 x̄
n x2i − ( xi )2
P P
P P
xi yi
where x̄ = n
and ȳ = n
.
Problem 2.1.2. Construct a linear least square fitting polynomial for the data (0, 6), (1, 0), (2, 0).
xi yi x2i xi y i
0 6 0 0
1 0 1 0
2 0 4 0
=3 =6 =5 =0
P P P P
3a0 + 3a1 = 6
3a0 + 5a1 = 0
3(2 − a1 ) + 5a1 =0
6 − 3a1 + 5a1 =0
6 + 2a1 =0
a1 = −3
8
Numerical Differentiation
Chapter 3
Numerical Differentiation
9
3.2 Taylor Series Derivations Numerical Differentiation
f (x) − f (x − h) h 00
f 0 (x) = + f (ξ)
h 2
10
Numerical Differentiation 3.2 Taylor Series Derivations
• f 0 (x) terms: (−2h − 8(−h) + 8(h) − (2h))f 0 (x) = (−2 + 8 + 8 − 2)hf 0 (x) = 12hf 0 (x)
2 2
• f 00 (x) terms: (2h2 − 8( h2 ) + 8( h2 ) − 2h2 )f 00 (x) = (2 − 4 + 4 − 2)h2 f 00 (x) = 0
3 3 3 8h3
• f 000 (x) terms: (− 8h6 − 8(− h6 ) + 8( h6 ) − 6
)f 000 (x) = (− 43 + 43 + 43 − 43 )h3 f 000 (x) = 0
4 4 4 16h4
• f (4) (x) terms: ( 16h
24
− 8( h24 ) + 8( h24 ) − 24
)f (4) (x) =0
− 25 h5 f (5) (ξ)
11
3.3 Total Error and Optimal h Numerical Differentiation
Definition 3.2.7 (3-Point Endpoint Formula for f 00 ). We can also find f 00 (x) by applying the
forward difference formula to f 0 (x):
f 0 (x + h) − f 0 (x)
f 00 (x) ≈
h
1 f (x + 2h) − f (x + h) f (x + h) − f (x)
" ! !#
≈ −
h h h
f (x + 2h) − 2f (x + h) + f (x)
=
h2
The error for this formula is O(h).
Definition 3.3.1 (Total Error). The total error is the sum of Truncation Error and Round-
off Error. Let f˜(x) be the computed value of f (x), and |f (x) − f˜(x)| ≤ .
Example 3.3.2 (Optimal h for Forward Difference). The total error e(h) is:
f˜(x + h) − f˜(x)
e(h) = − f 0 (x)
h
f˜(x + h) − f (x + h) + f (x) − f˜(x) f (x + h) − f (x)
= + − f 0 (x)
h h
|ex+h | + |ex |
!
h
≤ + f 0 (x) + f 00 (ξ) − f 0 (x)
h 2
2 hM
≤ + (where |f 00 (ξ)| ≤ M )
h 2
To find the h that minimizes e(h) = 2
h
+ hM
2
:
2 M 4
r
e (h) = − 2 +
0
= 0 =⇒ h2 = =⇒ hoptimal = 2
h 2 M M
12
Numerical Differentiation 3.4 Examples and Problems
f˜(x+h)−f˜(x−h)
Example 3.3.3 (Optimal h for Central Difference). The total error e(h) is: e(h) = 2h
− f 0 (x) ≤
h2 000 h2 M
2
2h
+ 6
f (ξ) e(h) ≤
h
+ 6
(where |f 000 (ξ)| ≤ M ) To find the h that minimizes e(h):
2hM hM 3
e0 (h) = − + = 0 =⇒ = 2 =⇒ h3 =
h 2 6 3 h M
s
3
hoptimal = 3
Solution 5. Exact derivative: f 0 (x) = 1/x, so f 0 (3) = 1/3 ≈ 0.333333... Derivatives for
bounds: f 00 (x) = −1/x2 , f 000 (x) = 2/x3 , f (4) (x) = −6/x4 .
(i) Approximations
Evaluate at x = x0 = 2:
4 − 2.2 − 2.6 4 − 2 − 2.6 4 − 2 − 2.2
f 0 (2) ≈ P20 (2) = f (2) +f (2.2) +f (2.6)
(2 − 2.2)(2 − 2.6) (2.2 − 2)(2.2 − 2.6) (2.6 − 2)(2.6 − 2.2)
13
3.4 Examples and Problems Numerical Differentiation
Problem 3.4.3. Use the second derivative 3-point midpoint formula to compute f 00 (3) with
h = 0.1 where f (x) = ln x. Find the absolute error and error bound.
Problem 3.4.4. Given the following data, compute f 0 (2.0) using all applicable 2-point, 3-point,
and 5-point formulas.
19.86] = 26.63
1.2
≈ 22.19
The 3-point and 5-point formulas all give results around 22.0-22.25, which are likely more
accurate than the 2-point formulas.
14
Numerical Integration (Quadrature)
Chapter 4
f 0 (η)(b−a)2
Error: E = f 0 (ξ)(x − a)dx = f 0 (η) a (x − a)dx = .
Rb Rb
a 2
a+b a+b
! !
Z b Z b
f (x)dx ≈ f dx = f (b − a)
a a 2 2
f 00 (η)(b−a)3
Error: E = 24
. This rule is more accurate.
15
4.1 Introduction to Newton-Cotes Formulas Numerical Integration (Quadrature)
f 00 (ξ)
Error: f (x) − P1 (x) = 2
(x − a)(x − b).
Z b f 00 (ξ(x))
E= (x − a)(x − b)dx
a 2
Since (x − a)(x − b) ≤ 0 on [a, h b], by i Weighted Mean Value Theorem for Integrals: E =
f 00 (η) R b f 00 (η) (b−a)3
2 a (x − a)(x − b)dx = 2
− 6
h3 00
E = − f (η) for η ∈ (a, b)
12
The rule is exact for polynomials of degree ≤ 1.
Error Derivation for Simpson’s Rule. Instead of integrating P2 (x), we use Taylor expansion
2 3 4
around x1 . f (x) = f (x1 ) + (x − x1 )f 0 (x1 ) + (x−x
2
1)
f 00 (x1 ) + (x−x
6
1)
f 000 (x1 ) + (x−x
24
1)
f (4) (ξ)
Integrate term by term from x0 = x1 − h to x2 = x1 + h:
#x1 +h #x1 +h
(x − x1 )2 (x − x1 )3
" "
Z x1 +h
f (x)dx = [f (x1 ) · x]xx11 +h + f (x1 ) 0
+ f (x1 )00
+ ...
x1 −h
−h
2 x1 −h
6 x1 −h
h2 (4)
f (x0 )−2f (x1 )+f (x2 )
h2
− 12 f (η2 ). Substitute this f 00 (x1 ) back into the integral expansion:
16
Numerical Integration (Quadrature) 4.2 Composite Quadrature Rules
Definition 4.1.5 (Degree of Precision). The degree of precision (DOP) is the largest
integer n such that the formula is exact for all polynomials of degree ≤ n (i.e., f (x) = xk for
all k = 0, . . . , n).
Example 4.1.6 (Degree of Precision for Simpson’s Rule). Test f (x) = xk on [0, 2] with h = 1.
x0 = 0, x1 = 1, x2 = 2. Rule: 0 f (x)dx ≈ 13 [f (0) + 4f (1) + f (2)].
R2
2
• f (x) = x: xdx = [ x2 ]20 = 2. Rule: 13 [0 + 4(1) + 2] = = 2. (Exact)
R2 6
0 3
3
• f (x) = x2 : x2 dx = [ x3 ]20 = 83 . Rule: 13 [0 + 4(1)2 + 22 ] = 83 . (Exact)
R2
0
4
• f (x) = x3 : x3 dx = [ x4 ]20 = 4. Rule: 13 [0 + 4(1)3 + 23 ] = = 4. (Exact)
R2 12
0 3
5
• f (x) = x4 : x4 dx = [ x5 ]20 = = 6.4. Rule: 13 [0 + 4(1)4 + 24 ] = ≈ 6.67. (NOT exact)
R2 32 20
0 5 3
The rule is exact for degrees 0, 1, 2, 3, but not 4. Thus, the Degree of Precision is 3.
Definition 4.2.1 (Composite Trapezoidal Rule). Divide [a, b] into n intervals with h = (b−a)/n
and nodes xj = a + jh.
b n−1
X Z xj+1 n−1
Z
h
f (x)dx = f (x)dx ≈ [f (xj ) + f (xj+1 )]
X
a j=0 xj j=0 2
h
= [(f (x0 ) + f (x1 )) + (f (x1 ) + f (x2 )) + · · · + (f (xn−1 ) + f (xn ))]
2
n−1
h
= [f (x0 ) + 2 f (xj ) + f (xn )]
X
2 j=1
(b − a)h2 00
Etotal = − f (η)
12
Definition 4.2.2 (Composite Simpson’s Rule). Divide [a, b] into n intervals (where n must be
17
4.2 Composite Quadrature Rules Numerical Integration (Quadrature)
even). Let h = (b − a)/n. Apply Simpson’s 1/3 rule on n/2 pairs of intervals [x2j , x2j+2 ].
b n/2−1 Z x n/2−1
Z 2j+2 h
f (x)dx = f (x)dx ≈ [f (x2j ) + 4f (x2j+1 ) + f (x2j+2 )]
X X
a j=0 x2j j=0 3
h
= [(f (x0 ) + 4f (x1 ) + f (x2 )) + (f (x2 ) + 4f (x3 ) + f (x4 )) + . . . ]
3
n/2 n/2−1
h
= [f (x0 ) + 4 f (x2j−1 ) + 2 f (x2j ) + f (xn )]
X X
3 j=1 j=1
The formula separates odd (multiply by 4) and even (multiply by 2) indices. Error: Etotal =
(ηj ) = − h90 ( n2 f ¯(4) ) = − 180
Pn/2 h5 (4) 5 h4
j=1 − 90 f (nh)f (4) (η)
(b − a)h4 (4)
Etotal = − f (η)
180
The error is O(h4 ).
Remark 4.2.3 (Stability). Total error for Composite Simpson’s Rule: e(h) = Roundoff +
4M 4M
Truncation |e(h)| ≤ ni=0 |wi i | + (b−a)h ≈ (b − a) + (b−a)h Unlike differentiation, as
P
180 180
h → 0, the roundoff error is stable and the truncation error goes to 0. Numerical integration is
a stable process.
18