NA Ch4 Student
NA Ch4 Student
Xu Zhang
Department of Mathematics
Oklahoma State University
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 1 / 116
Chapter 4. Numerical Differentiation and Integration
Table of Contents
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 2 / 116
Chapter 4. Numerical Differentiation and Integration
f (x0 + h) − f (x0 )
f 0 (x0 ) = lim .
h→0 h
f (x0 + h) − f (x0 )
h
to approximation f 0 (x0 ).
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 4 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 5 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation
>>
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 6 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation
Recall the error bound of linear Lagrange interpolation (ref. Sec 3.1) is
f 00 (ξ(x))
f (x) − P1 (x) = (x − x0 )(x − x1 ).
2!
h
f 0 (x0 ) − P10 (x0 ) = − f 00 (ξ).
2
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 8 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation
where M is the bound for |f 00 (x)| for x between x0 and x0 + h. We say that
the truncation error is O(h).
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 9 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation
where n
Y x − xj
Ln,k (x) = .
j6=k
x k − xj
j=0
2xj − x1 − x2 2xj − x0 − x2
= f (x0 ) + f (x1 )
(x0 − x1 )(x0 − x2 ) (x1 − x0 )(x1 − x2 )
2
2xj − x0 − x1 f 000 (ξ) Y
+f (x2 ) + (xj − xk ).
(x2 − x0 )(x2 − x1 ) 3! j=0
j6=k
3 1 1 h2
f 0 (x0 ) = − f (x0 ) + f (x1 ) − f (x2 ) + f 000 (ξ0 ),
2h h 2h 3
1 1 h2 000
f 0 (x1 ) = − f (x0 ) + f (x2 ) − f (ξ1 ),
2h 2h 6
1 2 3 h2
f 0 (x2 ) = f (x0 ) − f (x1 ) − f (x2 ) + f 000 (ξ2 ).
2h h 2h 3
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 11 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation
Remark The other end-point formula can be obtained by replacing h with −h.
Remark Although the truncation error for midpoint and endpoint formulas
are both O(h2 ), the error in (4.5) is approximately half the error in (4.4).
This is because (4.5) uses data on both sides of x0 while (4.4) only uses
data from one side.
Exercise Derive the three-point formulas for f 0 if the points are not
equally-spaced. That is x1 = x0 + h1 , and x2 = x1 + h2 .
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 12 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation
Slope f (x 0)
1
Slope [ f (x0 h) f (x 0 h)]
2h
x0 h x0 x0 h x
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 13 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation
Remark
The truncation errors of 5-point formulas (4.6) and (4.7) are O(h4 ).
The formula (4.7) is particularly useful for the clamped spline
interpolation for approximating the derivative of the boundary points.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 14 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation
Example 1
Given the following data for f (x) = xex .
x 1.8 1.9 2.0 2.1 2.2
f (x) 10.889365 12.703199 14.778112 17.148957 19.855030
Solution (1/3)
We first use 3-point endpoint formula (4.4) with h = 0.1,
1
f 0 (2.0) ≈ [−3f (2.0) + 4(2.1) − f (2.2)]
0.2
= 5[−3(14.778112) + 4(17.148957) − 19.855030]
= 22.032310.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 15 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation
Solution (2/3)
We can also use 3-point endpoint formula (4.4) with h = −0.1,
1
f 0 (2.0) ≈ [−3f (2.0) + 4(1.9) − f (1.8)]
−0.2
= −5[−3(14.778112) + 4(12.703199) − 10.889365]
= 22.054525.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 16 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation
Solution (3/3)
The only 5-point formula applicable is the 5-point midpoint formula (4.6)
with h = 0.1
1
f 0 (2.0) ≈ [f (1.8) − 8f (1.9) + 8f (2.1) − f (2.2)]
1.2
1
= [10.889365 − 8(12.703199) + 8(17.148957) − 19.855030]
1.2
= 22.166999.
The true derivative is f 0 (2.0) = (2 + 1)e2 = 22.167168.
The relative error of these approximations are
3-pt endpoint with h = 0.1: 0.00608
3-pt endpoint with h = −0.1: 0.00509
3-pt midpoint with h = 0.1: 0.00277
3-pt midpoint with h = 0.2: 0.01114
5-pt midpoint with h = 0.1: 0.0000076
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 17 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation
h2 00 h3 000 h4 (4)
f (x0 + h) = f (x0 ) + hf 0 (x0 ) +
f (x0 ) + f (x0 ) + f (ξ1 ),
2 6 24
h2 h3 000 h4 (4)
f (x0 − h) = f (x0 ) − hf 0 (x0 ) + f 00 (x0 ) − f (x0 ) + f (ξ−1 ),
2 6 24
where ξ1 ∈ (x0 , x0 + h) and ξ−1 ∈ (x0 − h, x0 ).
h4 (4)
f (x0 + h) + f (x0 − h) = 2f (x0 ) + h2 f 00 (x0 ) + f (ξ1 ) + f (4) (ξ−1 ) .
24
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 18 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 19 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation
Example 2
Given the following data for f (x) = xex .
x 1.8 1.9 2.0 2.1 2.2
f (x) 10.889365 12.703199 14.778112 17.148957 19.855030
Use the second derivative midpoint formula to approximate f 00 (2.0).
Solution
Using the midpoint formula (4.8) with h = 0.1 we have
1 h i
f 00 (2.0) ≈ 2
f (1.9) − 2f (2.0) + f (2.1) = 29.593200.
0.1
Using the midpoint formula with h = 0.2 we have
1 h i
f 00 (2.0) ≈ f (1.8) − 2f (2.0) + f (2.2) = 29.704275.
0.22
Because f 00 (x) = (x + 2)ex , the exact value is f 00 (2.0) = 29.556224.
The relative errors for h = 0.1 and 0.2 are 0.00125 and 0.00501,
respectively.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 20 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation
9/16/20 10:35 AM
Solution. (The screenshot MATLAB Command Window
of MATLAB command window) 1 of 1
--------------------------------------------------------------------------------
Approximation of derivative of sin(x) at 0.9 using three-point midpoint
--------------------------------------------------------------------------------
i h Approx cos(0.9) error
--------------------------------------------------------------------------------
1 0.100000000000 0.620574469542 0.621609968271 0.001035498729
2 0.010000000000 0.621599608156 0.621609968271 0.000010360114
3 0.001000000000 0.621609864669 0.621609968271 0.000000103602
4 0.000100000000 0.621609967235 0.621609968271 0.000000001036
5 0.000010000000 0.621609968254 0.621609968271 0.000000000016
6 0.000001000000 0.621609968277 0.621609968271 -0.000000000006
7 0.000000100000 0.621609967943 0.621609968271 0.000000000327
8 0.000000010000 0.621609969054 0.621609968271 -0.000000000783
9 0.000000001000 0.621609985707 0.621609968271 -0.000000017436
10 0.000000000100 0.621609985707 0.621609968271 -0.000000017436
11 0.000000000010 0.621608320373 0.621609968271 0.000001647898
12 0.000000000001 0.621613871488 0.621609968271 -0.000003903217
>>
The smallest error is around i = 5, 6, i.e., h is between 10−6 and 10−5 . Why?
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 21 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation
f˜(x0 + h) − f˜(x0 − h)
f 0 (x0 ) −
2h
f (x 0 + h) − f (x0 − h) e(x0 + h) − e(x0 − h)
= f 0 (x0 ) − +
2h 2h
h2 000 e(x0 + h) − e(x0 − h)
= − f (ξ) + .
6 2h
The first term is the approximation error, and the second term is round-off
error.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 22 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation
If we assume that the round-off errors and the third derivative f 000 are
bounded as follows
f˜(x0 + h) − f˜(x0 − h)
f 0 (x0 ) −
2h
h2 000 |e(x0 + h) − e(x0 − h)|
≤ |f (ξ)| +
6 2h
h2 000 |e(x0 + h)| + |e(x0 − h)|
≤ |f (ξ)| +
6 2h
M h2 ε
≤ + .
6 h
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 23 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation
Now we analyze why h around 10−6 and 10−5 yields the most accurate
approximation in Example 3. Note f (x) = sin(x), so f 000 (x) = − cos(x).
Around x0 = 0.9, we consider the interval [0.8, 1],
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 25 / 116
Chapter 4. Numerical Differentiation and Integration 4.2 Elements of Numerical Integration
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 26 / 116
Chapter 4. Numerical Differentiation and Integration 4.2 Elements of Numerical Integration
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 28 / 116
b
a f (x) dx is approximated by the and
Chapter 4. Numerical Differentiation area in a trapezoid,
Integration 4.2 Elements ofas shown
Numerical in Figure 4.3.
Integration
y P1(x)
a x0 x1 b x
Z b
The integral f (x)dx,
Reserved. May not be copied, scanned, or duplicated, which
in whole or in part. is the
Due to electronic rights,area
some thirdof
partythe
contentcurved rectangle,
may be suppressed iseChapter(s).
from the eBook and/or
a experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
ontent does not materially affect the overall learning
approximated by the area of a trapezoid.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 29 / 116
Chapter 4. Numerical Differentiation and Integration 4.2 Elements of Numerical Integration
To estimate the error of the quadrature E(f ), we use the Weighted Mean
Value Theorem
Z b
E(f ) , f (x) − P1 (x)dx
a
Z x1
1
by Error of Interpolation = f 00 (ξ(x))(x − x0 )(x − x1 )dx
2 x0
Z x1
1 00
by Weighted MVT = f (ξ1 ) (x − x0 )(x − x1 )dx
2 x0
3 x1
1 00 x x0 + x1 2
= f (ξ1 ) − x + x0 x1 x
2 3 2 x0
h3 00
= − f (ξ1 ).
12
Trapezoidal Rule
Z b i h3
hh
f (x)dx = f (x0 ) + f (x1 ) − f 00 (ξ). (4.11)
a 2 12
Here h = b − a, x0 = a, x1 = b, and ξ is between x0 and x1 .
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 30 / 116
Chapter 4. Numerical Differentiation and Integration 4.2 Elements of Numerical Integration
Simpson’s Rule
Z b
h h5
f (x)dx = [f (x0 ) + 4f (x1 ) + f (x2 )] − f (4) (ξ), (4.12)
a 3 90
where h = (b − a)/2, x0 = a, x1 = a + h, and x2 = b.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 31 / 116
equally-spaced
Chapternodes = a, x2and=Integration
x0 Differentiation
4. Numerical b, and 4.2 = a of+Numerical
x1 Elements h, where h = (b − a)/2. (See
Integration
Figure 4.4.)
y P2(x)
a x0 x1 x2 b x
The error term for (4.12) has a factor f (4) , so Simpson’s rule is exact for
any polynomial of degree up to three.
Therefore
b
x2
(x − x1 )(x − x2 ) (x − x0 )(x − x2 )
f (x) dx = f (x0 ) + f (x1 )
a x0 (x0 − x1 )(x0 − x2 ) (x1 − x0 )(x1 − x2 )
Xu Zhang (Oklahoma State University)
MATH 4513 Numerical Analysis Fall 2020 32 / 116
Chapter 4. Numerical Differentiation and Integration 4.2 Elements of Numerical Integration
Example 4
R2
Compare Trapezoidal rule and Simpson’s rule in approximating 0
f (x)dx
when f (x) is
Solution
For Trapezoidal rule, we have h = 2, x0 = 0, x1 = 2, then
Z 2
f (x)dx ≈ f (0) + f (2).
0
For Simpson’s rule, we have h = 1, x0 = 0, x1 = 1, x2 = 2, then
Z 2
1
f (x)dx ≈ [f (0) + 4f (1) + f (2)] .
0 3
f (x) x x2 x4 (1 + x)−1 sin(x)
Trapezoidal 2.0000 4.0000 16.0000 1.3333 0.9093
Simpson 2.0000 2.6667 6.6667 1.1111 1.4251
Exact 2.0000 2.6667 6.4000 1.0986 1.4161
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 33 / 116
Chapter 4. Numerical Differentiation and Integration 4.2 Elements of Numerical Integration
Measuring Precision
Remark
The DoP of the Trapezoidal rule is one.
The DoP of the Simpson’s rule is three.
The DoP of a quadrature formula is n if and only if the error is zero for all
polynomials of degree up to n, but not zero for some polynomial of
degree n + 1.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 34 / 116
Chapter 4. Numerical Differentiation and Integration 4.2 Elements of Numerical Integration
Example 6
Find the degree of the precision of the quadrature formula
Z 1 −√ 3 √3
f (x)dx = f +f .
−1 3 3
Solution (1/2)
By definition of DoP, we will test f (x) = 1, x, x2 , · · · , until we find a
number k such that the quadrature formula does not hold for xk .
Z √
1 √
f (x) = 1, f − 3/3 + f
1dx = 2,3/3 = 1 + 1 = 2.
−1
Z 1 √ √ √ √
3 3
f (x) = x, xdx = 0, f − 3/3 + f 3/3 = − + = 0.
−1 3 3
Z 1
2 √ √ 1 1 2
2
f (x) = x , x2 dx = , f − 3/3 + f 3/3 = + = .
−1 3 3 3 3
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 35 / 116
Chapter 4. Numerical Differentiation and Integration 4.2 Elements of Numerical Integration
Solution (2/2)
Z 1 √ √ 1 1
f (x) = x3 , x3 dx = 0,
f − 3/3 + f 3/3 = − √ + √ = 0.
−1 3 3 3 3
Z 1
2 √ √ 1 1 2
f (x) = x4 , x4 dx = , f − 3/3 + f 3/3 = + = .
−1 5 9 9 9
The formula is exact for polynomial of degree up to 3, but not accurate for
degree 4. The degree of precision is 3.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 36 / 116
Chapter 4. Numerical Differentiation and Integration 4.2 Elements of Numerical Integration
Newton-Cotes Formulas
Trapezoidal rule and Simpson’s rule are examples of a larger class of
methods called Newton-Cotes Formulas.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 37 / 116
where x0 = a, xn = b and h = (b − a)/n. (See Figure 4.5.) It is called closed because the
Chapter 4. Numerical
endpoints Differentiation
of the closed [a,Integration
intervaland 4.2 as
b] are included Elements
nodes.of Numerical Integration
y = Pn(x)
y = f (x)
a x0 x1 x2 xn1 xn b x
Xu Theorem 4.2 Suppose
Zhang (Oklahoma that ni=0 ai f (xi )
State University) denotes
MATHthe (n Numerical
4513 + 1)-point closed Newton-Cotes formula with
Analysis Fall 2020 38 / 116
Chapter 4. Numerical Differentiation and Integration 4.2 Elements of Numerical Integration
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 39 / 116
Chapter 4. Numerical Differentiation and Integration 4.2 Elements of Numerical Integration
where
b−a
h= , a = x−1 < x0 < x1 < · · · < xn < xn+1 = b, and xi = x0 + ih
n+2
Z xn+1 Z xn+1 Y
(x − xj )
ai = Li (x)dx = dx.
x−1 x−1 (xi − xj )
j=0,j6=i
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 40 / 116
200 CHAPTER 4 Numerical Differentiation and Integration
Chapter 4. Numerical Differentiation and Integration 4.2 Elements of Numerical Integration
Figure 4.6
Illustration of Open Newton-Cotes Formula
y
y = Pn(x)
y = f (x)
a x1 x0 x1 x2 xn xn1 b x
Here, fi = f (xi ).
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 42 / 116
Chapter 4. Numerical Differentiation and Integration 4.2 Elements of Numerical Integration
Example 9
Compare the results of the closed and open Newton-Cotes formulas when
approximating
Z π/4
sin(x)dx ≈ 0.29289322.
0
Solution (1/5)
We write two MATLAB subroutines for closed and open Newton-Cotes
formulas.
The subroutines should contain the following inputs:
1. function f , 2. interval [a, b], 3, number of points
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 43 / 116
Chapter 4. Numerical Differentiation and Integration 4.2 Elements of Numerical Integration
h = (b-a)/n;
if n == 1 % Trapezoidal rule
x0 = a; x1 = x0+h;
f0 = fun(x0); f1 = fun(x1);
y = (h/2)*(f0+f1);
elseif n == 2 % Simpson's rule
x0 = a; x1 = x0+h; x2 = x0+2*h;
f0 = fun(x0); f1 = fun(x1); f2 = fun(x2);
y = (h/3)*(f0+4*f1+f2);
elseif n == 3 % Simpson's 3/8 rule
x0 = a; x1 = x0+h; x2 = x0+2*h; x3 = x0+3*h;
f0 = fun(x0); f1 = fun(x1); f2 = fun(x2); f3 = fun(x3);
y = (3*h/8)*(f0+3*f1+3*f2+f3);
elseif n == 4 % Boole's rule
x0 = a; x1 = x0+h; x2 = x0+2*h; x3 = x0+3*h; x4 = x0+4*h;
f0 = fun(x0); f1=fun(x1); f2=fun(x2); f3=fun(x3); f4=fun(x4);
y = (2*h/45)*(7*f0+32*f1+12*f2+32*f3+7*f4);
end
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 44 / 116
Chapter 4. Numerical Differentiation and Integration 4.2 Elements of Numerical Integration
h = (b-a)/(n+2);
if n == 0 % Midpoint rule
x0 = a+h;
f0 = fun(x0);
y = (2*h)*f0;
elseif n == 1
x0 = a+h; x1 = x0+h;
f0 = fun(x0); f1 = fun(x1);
y = (3*h/2)*(f0+f1);
elseif n == 2
x0 = a+h; x1 = x0+h; x2 = x0+2*h;
f0 = fun(x0); f1 = fun(x1); f2 = fun(x2);
y = (4*h/3)*(2*f0-f1+2*f2);
elseif n == 3
x0 = a+h; x1 = x0+h; x2 = x0+2*h; x3 = x0+3*h;
f0 = fun(x0); f1=fun(x1); f2=fun(x2); f3=fun(x3);
y = (5*h/24)*(11*f0+f1+f2+11*f3);
end
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 45 / 116
Chapter 4. Numerical Differentiation and Integration 4.2 Elements of Numerical Integration
9/16/20
Solution (4/5): A MATLAB6:21Driver
PM /Users/xuzhang/Dropbox/Teachi.../ex4_2_1.m
File 1
% ex_4_2_1
clear
fun = @(x) sin(x);
a = 0;
b = pi/4;
y1c = NewtonCotesCl(fun,a,b,1);
y2c = NewtonCotesCl(fun,a,b,2);
y3c = NewtonCotesCl(fun,a,b,3);
y4c = NewtonCotesCl(fun,a,b,4);
y0o = NewtonCotesOp(fun,a,b,0);
y1o = NewtonCotesOp(fun,a,b,1);
y2o = NewtonCotesOp(fun,a,b,2);
y3o = NewtonCotesOp(fun,a,b,3);
y = -cos(pi/4)+cos(0);
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 46 / 116
Chapter 4. Numerical Differentiation and Integration 4.2 Elements of Numerical Integration
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 47 / 116
Chapter 4. Numerical Differentiation and Integration 4.3 Composite Numerical Integration
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 48 / 116
Chapter 4. Numerical Differentiation and Integration 4.3 Composite Numerical Integration
Example 10
R4
In this example, we apply the Simpson’s rule to approximate 0 ex dx, whose
true value is 53.59815. We will do it in the three ways.
R4
1 approximate 0 ex dx directly.
R2 R4
2 approximate separately on 0 ex dx and 2 ex dx
R1 R2 R3 R4
3 approximate separately on 0 ex dx, 1 ex dx, 2 ex dx, and 3 ex dx
Solution (1/3)
4−0
Directly applying the Simpson’s rule on [0, 4], we have h = = 2,
2
then, Z 4
2 0
ex dx ≈ [e + 4e2 + e4 ] = 56.76958.
0 3
The relative error is 0.0592.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 49 / 116
Chapter 4. Numerical Differentiation and Integration 4.3 Composite Numerical Integration
Solution (2/3)
Now we use Simpson’s rule separately on [0, 2] and [2, 4].
2−0 4−2
Note that on each subinterval h = = = 1, then,
2 2
Z 4 Z 2 Z 4
ex dx = ex dx + ex dx
0 0 2
1 0 1
≈ [e + 4e1 + e2 ] + [e2 + 4e3 + e4 ]
3 3
= 53.86385.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 50 / 116
Chapter 4. Numerical Differentiation and Integration 4.3 Composite Numerical Integration
Solution (3/3)
Next we use Simpson’s rule separately on [0, 1], [1, 2], [2, 3] and [3, 4].
1−0 1
Note that on each subinterval h = = , then,
2 2
Z 4 Z 1 Z 2 Z 3 Z 4
ex dx = ex dx + ex dx + ex dx + ex dx
0 0 1 2 3
1 1
≈ [e0 + 4e1/2 + e1 ] + [e1 + 4e3/2 + e2 ]
6 6
1 2 1
+ [e + 4e5/2 + e3 ] + [e3 + 4e7/2 + e4 ]
6 6
1 0
= [e + 4e1/2 + 2e1 + 4e3/2 + 2e2 + 4e5/2 + 2e3 + 4e7/2 + e4 ]
6
= 53.61622.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 51 / 116
b
Chapter 4. Numerical Differentiation and Integration 4.3 Composite Numerical Integration
To generalize this procedure for an arbitrary integral f (x) dx, choose an even
Z b a
integer n. Subdivide
In general, the interval [a,fb]
to approximate into nchoose
(x)dx, subintervals,
an evenandnumber
apply Simpson’s rule on
n, subdivide
each consecutive pair of subintervals.
a (See Figure 4.7.)
[a, b] to n subintervals, and apply Simpson’s rule on each consecutive pair of
subintervals.
y
y f (x)
eserved. 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).
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 52 / 116
Chapter 4. Numerical Differentiation and Integration 4.3 Composite Numerical Integration
b−a
Let n be an even number. Let h = , and xj = a + jh, j = 0, 1, · · · , n.
n
Z b Z x2 Z x4 Z xn
f (x)dx = f (x)dx + f (x)dx + · · · + f (x)dx
a x0 x2 xn−2
hh i hh i
≈ f (x0 ) + 4f (x1 ) + f (x2 ) + f (x2 ) + 4f (x3 ) + f (x4 )
3 3
hh i
+··· + f (xn−2 ) + 4f (xn−1 ) + f (xn )
3
hh
= f (x0 ) + 4f (x1 ) + 2f (x2 ) + 4f (x3 ) + 2f (x4 ) + · · ·
3 i
+2f (xn−2 ) + 4f (xn−1 ) + f (xn )
hh
= f (x0 ) + 4 f (x1 ) + f (x3 ) + · · · + f (xn−1 )
3 i
+2 f (x2 ) + f (x4 ) + · · · + f (xn−2 ) + f (xn ) .
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 53 / 116
Chapter 4. Numerical Differentiation and Integration 4.3 Composite Numerical Integration
b−a
where n is even, h = , and xj = a + jh for each j = 0, 1, · · · , n.
n
b − a 4 (4)
E(f ) = − h f (µ) (4.16)
180
for some µ ∈ (a, b).
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 54 / 116
Chapter 4. Numerical Differentiation and Integration 4.3 Composite Numerical Integration
y f (x)
a x0 x1 x j1 xj x n1 b xn x
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 55 / 116
Chapter 4. Numerical Differentiation and Integration 4.3 Composite Numerical Integration
b − a 2 00
E(f ) = − h f (µ) (4.18)
12
for some µ ∈ (a, b).
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 56 / 116
Chapter 4. Numerical Differentiation and Integration 4.3 Composite Numerical Integration
For the Composite Midpoint rule, n must again be even. (See Figure 4.9.)
Similarly, we can also apply the subdivision idea to the midpoint rule.
Let f ∈ C 2 [a, b], n be even, h = (b − a)/(n + 2), and xj = a + (j + 1)h for each
=Zhang
j Xu −1,(Oklahoma
0, . . . , State
n +University) μ∈
1. There exists aMATH (a,
4513 b) forAnalysis
Numerical which the Composite Midpoint
Fall 2020 rule
57 / 116
Chapter 4. Numerical Differentiation and Integration 4.3 Composite Numerical Integration
b−a
where xj = a + (j + 1)h for each −1 ≤ j ≤ n + 1, with h = .
n+2
b − a 2 00
E(f ) = − h f (µ) (4.20)
6
for some µ ∈ (a, b).
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 58 / 116
Chapter 4. Numerical Differentiation and Integration 4.3 Composite Numerical Integration
Example 14
Determine values of h that will ensure an approximation error of less than
0.00002 when approximating Z π
sin(x)dx
0
Solution. (1/3)
Note that the error bound of composite trapezoidal rule is
b − a 2 00
E(f ) = − h f (µ).
12
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 59 / 116
Chapter 4. Numerical Differentiation and Integration 4.3 Composite Numerical Integration
Solution. (2/3)
Since h = π/n, then
π
< 0.008740 =⇒ n > 359.4
n
We need at least 360 subintervals to ensure the desired accuracy.
The error bound of composite Simpson’s rule is
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 60 / 116
Chapter 4. Numerical Differentiation and Integration 4.3 Composite Numerical Integration
Solution. (3/3)
We write a MATLAB subroutine for composite Simpson’s rule, and a
driver file for testing this example.
9/19/20 3:05 AM /Users/xuzhang/Drop.../simpsonComp.m 1 of 1
function Q = simpsonComp(fun,a,b,n)
if mod(n,2) ~= 0
warning('n must be an even number. Replaced n by n+1');
n = n + 1;
end
9/19/20 3:08 AM /Users/
h = (b-a)/n;
Q = h/3*(fun(a)+fun(b));
% ex4_4_2
for j = 1:(n/2)-1
Q = Q + (h/3)*2*fun(a+2*j*h) + (h/3)*4*fun(a+(2*j-1)*h); fun = @(x) sin(x);
end a = 0;
Q = Q + (h/3)*4*fun(a+(n-1)*h); b = pi;
n = 18;
end Q = simpsonComp(fun,a,b,n)
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 61 / 116
randomly chosen value described by this distribution4.3
Chapter 4. Numerical Differentiation and Integration
lies in [a, b] is given by f (x) dx. Approximate
Composite Numerical Integration a
to within 10−5 the probability that a randomly chosen value described by this distribution will lie in
a. [−σ , σ15
Example ] b. [−2σ , 2σ ] c. [−3σ , 3σ ]
−6
Determine to within 10 the length of the graph of the ellipse with equation 4x 2 + 9y2 = 36.
A car laps a race track in 84 seconds. The speed of the car at each 6-second
A car laps a race track in 84 seconds. The speed of the car at each 6-second interval is determined
interval
by using aisradar
determined
gun and is given by using from the a radar
beginning gunofand is given
the lap, from the
in feet/second, beginning
by the of
entries in the
the lap, in feet/second,
following table. by the entries in the following table.
Time 0 6 12 18 24 30 36 42 48 54 60 66 72 78 84
Speed 124 134 148 156 147 133 121 109 99 85 78 89 104 116 123
Solution. (1/2)
eserved. 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).
ent 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.
Z 84
Note that the length l of the track is l = s(t)dt.
0
We don’t know the formula of s(t). So, the integral can only be
approximated by numerical quadrature using the above 15 data points:
Z 84 15
X
s(t)dt ≈ ai s(ti ).
0 i=1
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 62 / 116
Chapter 4. Numerical Differentiation and Integration 4.3 Composite Numerical Integration
Solution. (2/2)
Using composite trapezoidal rule with h = 6,
Z 84
6h i
l= s(t)dt ≈ 124 + 2(134 + 148 + · · · + 116) + 123
0 2
= 9855
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 63 / 116
4.6 Chapter
Adaptive Quadrature
4. Numerical Differentiation and Methods
Integration 4.4 Adaptive Quadrature Method
0.5
0.4
0.3
y (x) = e3xsin 4x
0.2
0.1
1
2 3 4 x
0.1
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 64 / 116
Chapter 4. Numerical Differentiation and Integration 4.4 Adaptive Quadrature Method
Z 4
The true integral value is e−3x sin(4x)dx = 0.160001.
0
Using Composite Simpson’s rule with 12 equal-spaced points, we obtain
the quadrature Q1 = 0.154225. The relative error is 3.61%.
0.5 0.5
0.4 0.4
0.3 0.3
0.2 0.2
0.1 0.1
0 0
-0.1 -0.1
0 0.5 1 1.5 2 2.5 3 3.5 4 0 0.5 1 1.5 2 2.5 3 3.5 4
If these 12 points are distributed more “smartly”, i.e., more nodes in the
region with large functional variation. Using the same quadrature rule, we
obtain Q2 = 0.160101. The relative error is 0.0626%.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 65 / 116
Chapter 4. Numerical Differentiation and Integration 4.4 Adaptive Quadrature Method
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 66 / 116
Chapter 4. Numerical Differentiation and Integration 4.4 Adaptive Quadrature Method
h
S(a, b) = [f (a) + 4f (a + h) + f (b)],
3
where the step size h = (b − a)/2.
The error is
b
h5 (4)
Z
E(f ) = f (x)dx − S(a, b) = − f (ξ), for some ξ ∈ (a, b).
a 90
The error term contains f (4) (ξ), which is not known a priori. We will
derive some alternative formula to estimate the error that does not
require f (4) (ξ).
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 67 / 116
Chapter 4. Numerical Differentiation and Integration 4.4 Adaptive Quadrature Method
Consider the composite Simpson’s rule with n = 4 and the step size
(b − a)/4 = h/2
Z b i (h/2)5
h/2 h 1
f (x)dx = f (a) + 4f (a + h) + f (a + h) − f (4) (ξ1 )
a 3 2 90
h/2 h 3 i (h/2)5
+ f (a + h) + 4f (a + h) + f (b) − f (4) (ξ2 )
3 2 90
h5 (4) h5 (4)
a+b a+b 1
= S(a, ) + S( , b) − f (ξ1 ) + f (ξ2 )
2 2 16 180 180
5
a+b a+b 1 h (4) ˜
= S(a, ) + S( , b) − f (ξ),
2 2 16 90
where we use the intermediate value theorem in the last step and the
value ξ˜ is in (a, b).
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 68 / 116
Chapter 4. Numerical Differentiation and Integration 4.4 Adaptive Quadrature Method
a+b a+b 15 h5
S(a, ) + S( , b) − S(a, b) ≈ − · f (4) (ξ)
2 2 16 90
or
h5
16 a+b a+b
− f (4) (ξ) ≈ S(a, ) + S( , b) − S(a, b) (4.23)
90 15 2 2
That means the error term f (4) (ξ) can be approximated by the
summation of some computable terms on the right hand side of (4.23).
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 69 / 116
Chapter 4. Numerical Differentiation and Integration 4.4 Adaptive Quadrature Method
When the approximations in (4.25) differ by more than 15, we can apply
the Simpson’s rule individually to the subintervals [a, (a + b)/2] and
[(a + b)/2, b], and let the tolerance on each integral within /2.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 71 / 116
Chapter 4. Numerical Differentiation and Integration 4.4 Adaptive Quadrature Method
10/4/20
MATLAB Driver: 11:45
Adaptive AM /Users/xuzhang/Dro.../simpsonAdpt.m
Simpson’s Method
function [Q,X] = simpsonAdpt(fun,a,b,tol)
% Adaptive Simpson's Rule
% Inputs
% fun --- function handle
% a,b --- integrating interval [a,b]
% tol --- tolerance
% Outputs
% Q --- quadrature value
% X --- nodes generated by adaptive strategy
%%
m = (a+b)/2;
Q = simpson(fun,a,b);
Q1 = simpson(fun,a,m);
Q2 = simpson(fun,m,b);
X = [a,m,b];
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 72 / 116
Chapter 4. Numerical Differentiation and Integration 4.4 Adaptive Quadrature Method
Example 16
Use adaptive Simpson’s method with the tolerance 10−3 to approximate the
integral
Z 3
100 10
2
sin( )dx.
1 x x
Then use the Composite Simpson’s rule with same number of nodes to
approximate this integral. Compare their results.
80
60
40
20
-20
-40
-60
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 73 / 116
Chapter 4. Numerical Differentiation and Integration 4.4 Adaptive Quadrature Method
clear; clc;
fun = @(x) (100./x.^2).*sin(10./x);
a = 1;
b = 3;
tQ = -1.426024756346266;
%% Adaptive Simpson
tol = 1e-4;
[Q,X] = simpsonAdpt(fun,a,b,tol);
hmin = min(X(2:end)-X(1:end-1));
%% Composite Simpson
n = length(X)-1; % using same number of points as in adaptive method
if mod(n,2) == 1
n = n+1;
end
Q2 = simpsonComp(fun,a,b,n);
%% Output
disp('-------------------------------------------------------------------')
disp(' pt hmin True Integral Adaptive Simpson Abs Error')
disp('-------------------------------------------------------------------')
formatSpec = '% 2i %9.5f %12.8f %12.8f %12.8f\n';
fprintf(formatSpec,[length(X)-1,hmin,tQ, Q,abs(tQ-Q)])
disp(' ')
disp('-------------------------------------------------------------------')
disp(' pt hmin True Integral Composite Simpson Abs Error')
disp('-------------------------------------------------------------------')
formatSpec = '% 2i %9.5f %12.8f %12.8f %12.8f\n';
fprintf(formatSpec,[n,(b-a)/(n-1),tQ, Q2,abs(tQ-Q2)])
figure(1); clf
qx = a:0.001:b;
plot(X,fun(X),'ro','lineWidth',2)
hold on
plot(X,zeros(size(X)),'k+-','lineWidth',2)
plot(qx,fun(qx),'b-','lineWidth',2)
hold off; axis tight
figure(2); clf
X2 = a:(b-a)/n:b;
plot(X2,fun(X2),'ro','lineWidth',2)
hold on
plot(X2,zeros(size(X2)),'k+-','lineWidth',2)
plot(qx,fun(qx),'b-','lineWidth',2)
hold off; axis tight
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 74 / 116
Chapter 4. Numerical Differentiation and Integration 4.4 Adaptive Quadrature Method
-------------------------------------------------------------------
pt hmin True Integral Composite Simpson Abs Error
-------------------------------------------------------------------
44 0.04651 -1.42602476 -1.42327343 0.00275133
>>
60 60
40 40
20 20
0 0
-20 -20
-40 -40
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3 1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 75 / 116
Chapter 4. Numerical Differentiation and Integration 4.4 Adaptive Quadrature Method
Example 17
In this problem, we use the area of a unit sphere to approximate the value of
π. Note that the area of the upper semi-sphere
Z 1 p π
1 − x2 dx =
−1 2
Use the adaptive Simpson’s method and the Composite Simpson’s method
with similar number of nodes to approximate this integral.
1
0.8
0.6
0.4
0.2
0
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 76 / 116
Chapter 4. Numerical Differentiation and Integration 4.4 Adaptive Quadrature Method
Solution
Set the tolerance to be tol = 1e − 3. We write a similar driver file as the
previous10/4/20
example.2:58 PM MATLAB Command Window 1 of
-------------------------------------------------------------------
pt hmin True Integral Adaptive Simpson Abs Error
-------------------------------------------------------------------
20 0.03125 1.57079633 1.56937393 0.00142239
-------------------------------------------------------------------
pt hmin True Integral Composite Simpson Abs Error
-------------------------------------------------------------------
20 0.10526 1.57079633 1.56350408 0.00729225
>>
1 1
0.8 0.8
0.6 0.6
0.4 0.4
0.2 0.2
0 0
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 78 / 116
y f (x) 4.7 y fQuadrature
Gaussian (x) 229
Chapter 4. Numerical Differentiation and Integration 4.5yGaussian
f (x) Quadrature
The Trapezoidal
Figure 4.15 rule approximates the integral function by integrating the
linear function that joins the endpoints.
y y y
a x1 x2 b x a x1 x2 b x a x1 x2 b x
y f (x) y f (x)
y f (x)
The Trapezoidal rule approximates the integral of the function by integrating the linear
function that joins the endpoints of the graph of the function. But this is not likely the best
line for approximating the integral. Lines such as those shown in Figure 4.16 would likely
give much better approximations in most cases.
a x1 x2 b x a x1 x2 b x a x1 x2 b x
This mayFigure
not 4.16
be the best line approximation for the integral. The following
choice of the evaluation points is likely to give better approximation:
The Trapezoidal rule approximates the integral of the function by integrating the linear
y y the endpoints of the graph of the function.
function that joins y But this is not likely the best
line for approximating the integral. Lines such as those shown in Figure 4.16 would likely
y f (x) give much better approximations in most cases.
y f (x)
y f (x)
Figure 4.16
y y y
a x1 x2 b x a x1 x2 b x a x1 x2 b x
y f (x)
y f (x)
Xu Zhang (Oklahoma State University) y Analysis
MATH 4513 Numerical f (x) Fall 2020 79 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature
Remark
There are 2n parameters to determine, and the polynomial of degree
2n − 1 also contains 2n parameters.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 80 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature
Example 18
Determine the two-point Gaussian quadrature formula on the interval [−1, 1].
Solution (1/2)
We need to determine two weights c1 , c2 and two points x1 , x2 so that
Z 1
f (x)dx ≈ c1 f (x1 ) + c2 f (x2 )
−1
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 81 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature
Solution (2/2)
Z 1
f (x) = 1 : 1dx = 2, c1 f (x1 ) + c2 f (x2 ) = c1 + c2 = 2.
−1
Z 1
f (x) = x : xdx = 0, c1 f (x1 ) + c2 f (x2 ) = c1 x1 + c2 x2 = 0.
−1
Z 1
2 2 2
f (x) = x : x2 dx = , c1 f (x1 ) + c2 f (x2 ) = c1 x21 + c2 x22 = .
−1 3 3
Z 1
f (x) = x3 : x3 dx = 0, c1 f (x1 ) + c2 f (x2 ) = c1 x31 + c2 x32 = 0.
−1
That is
c1 + c2 = 2 c1 = 1,
c1 x1 + c2 x2 = 0
c2 = 1,
=⇒ √
c1 x21 + c2 x22 = 2/3 x1 = − 3/3,
√
3
c1 x1 + c2 x32 = 0
x2 = 3/3
Z 1 √ √
The 2-pt Gaussian Quadrature is f (x)dx ≈ f (− 3/3) + f (− 3/3).
−1
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 82 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature
P0 (x) = 1
P1 (x) = x
1
P2 (x) = (3x2 − 1)
2
1
P3 (x) = (5x3 − 3x)
2
1
P4 (x) = (35x4 − 30x2 + 3)
8
In general, the Legendre polynomial can be obtained using the recursive
formula
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 83 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature
0.8
0.6
0.4
0.2
-0.2
-0.4
P0 (x)
-0.6
P1 (x)
P2 (x)
-0.8
P3 (x)
-1 P4 (x)
Pn (x) has n simple roots x1 , x2 , · · · , xn , and they all lie in (−1, 1). These
roots are the nodes of the n-point Gaussian quadrature formula.
Orthogonality
Z 1
2
Pm (x)Pn (x)dx = δmn .
−1 2n + 1
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 84 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 85 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature
Graphs of Legendre polynomials (up
1 0 0 2 2
2 ±0.57735 1 1
0 0 0.888889
3
±0.774597 0.555556
±0.339981 0.652145
4
±0.861136 0.347855
0 0 0.568889
±0.538469 0.478629
5
±0.90618 0.236927
Change
Nodes of interval
and Weights for higher order (up to n = 64) are available at
https://pomax.github.io/bezierinfo/legendre-gauss.html
https://en.wikipedia.org/wiki/Gaussian_quadrature Page 3 of 15
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 86 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature
Example 20
Z 1
Approximate ex cos(x)dx using Gaussian quadrature with n = 3.
−1
Solution
Extract the value for n = 3 from the Gaussian quadrature table.
Z 1
5 p 8 5 p
f (x)dx ≈ f (− 3/5) + f (0) + f ( 3/5)
−1 9 9 9
5 −√ 3 r3 8 5 √3 r 3
Z 1
x 0
e cos(x)dx ≈ e 5 cos − + e cos(0) + e 5 cos
−1 9 5 9 9 5
= 1.9333904.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 87 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature
Remark
Gaussian quadrature with 3 points
Z 1
5 p 8 5 p
f (x)dx ≈ f (− 3/5) + f (0) + f ( 3/5)
−1 9 9 9
S12 ≈ 1.93338827
Gaussian Quadrature
Gaussian Quadrature on Arbitrary
on Arbitrary IntervalsIntervals
b
An integral a f (x) dx over an arbitrary [a, b] can be transformed into an integral over
[−1,
Use 1]the
by using the change of variables (see Figure 4.17):
mapping
2x − a − b 1
= − a − b ⇐⇒ x = [(b1− a)t + a + b].
t 2x
t= b − a ⇐⇒ x = [(b − a)t + a + b]
2
b−a 2
4.17 we can map the arbitary interval [a, b] to the reference interval [−1, 1].
t
(b, 1)
1
2x a b
t
ba
a b x
1
(a, 1)
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 89 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature
1
Using the mapping x = [(b − a)t + a + b], we have
2
Z b Z 1
b−a a+b b−a
f (x)dx = f t+ dt.
a −1 2 2 2
where
b−a a+b
the nodes: xi = ti + , and
2 2
b−a
the weight: ci = wi
2
ti and wi are nodes and weights on the reference interval listed on page 86.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 90 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature
Remark
The degree of precision of the n-point Gaussian quadrature is 2n − 1.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 91 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature
Example 22
Z 3
Consider the integral x6 − x2 sin(2x)dx = 317.3442466. Compare the
1
n-point Gaussian quadrature formula where n = 1, 2, 3, 4, 5.
Solution. (1/2)
Note that a = 1, b = 3, f (x) = x6 − x2 sin(2x).
3−1 3−1 3+1
Let n = 1. t1 = 0, w1 = 2, c1 = 2 w1 = 2, and x1 = 2 t1 + 2 = 2.
Z 3
f (x)dx ≈ c1 f (x1 ) = 2f (2) = 134.0544. abs. err = 183.2898
1
p
Let n = 2. t1,2 = ± √13 , w1,2 = 1, c1,2 = 2, and x1,2 = 2 ± 1/3.
Z 3
f (x)dx ≈ c1 f (x1 ) + c2 f (x2 ) = 306.8199. abs. err = 10.5243
1
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 92 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature
Solution. (2/2)
For n = 3, 4, 5, we have
Z 3 3
X
f (x)dx ≈ ci f (xi ) = 317.2642. abs. err = 0.0801
1 i=1
Z 3 4
X
f (x)dx ≈ ci f (xi ) = 317.3454. abs. err = 0.0011
1 i=1
Z 3 5
X
f (x)dx ≈ ci f (xi ) = 317.3442. abs. err = 1.99 × 10−5 .
1 i=1
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 93 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature
y = 0;
for i = 1:n
y = y + C(i)*fun(X(i));
end
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 94 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature
% ex_4_7_2
clc
fun = @(x) x.^2.*(x.^4 - sin(2*x));
a = 1;
b = 3;
int = 317.3442466;
disp('----------------------------------------------------')
disp(' Gaussian Quadrature')
disp('----------------------------------------------------')
disp(' n true Gaussian Error')
disp('----------------------------------------------------')
formatSpec = '%2d %.7f % .7f % 12.7f \n';
for n = 1:5
Q = gaussQuad(fun,a,b,n);
fprintf(formatSpec,[n,int,Q,abs(int-Q)])
end
----------------------------------------------------
Gaussian Quadrature
----------------------------------------------------
n true Gaussian Error
----------------------------------------------------
1 317.3442466 134.0544200 183.2898266
2 317.3442466 306.8199345 10.5243121
3 317.3442466 317.2641517 0.0800949
4 317.3442466 317.3453903 0.0011437
5 317.3442466 317.3442267 0.0000199
>>
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 95 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature
Example 23
Solution (1/3).
Due to symmetry, we only approximate the curve length in the first
quadrant x > 0, y > 0.Then
2p 2 1 −2x
y= 9 − x2 , y 0 (x) = · (9 − x2 )−1/2 (−2x) = √
3 3 2 3 9 − x2
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 96 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature
Solution (2/2).
We use Gaussian quadrature to approximate this integral
Gauss Quad 1pt = 12.8582010147
Gauss Quad 2pt = 13.9568784560
Gauss Quad 3pt = 14.4788351496
Gauss Quad 4pt = 14.7801510738
Gauss Quad 5pt = 14.9748548061
It does not converges obviously. The reason is the the high variation of
the function around x = 3.
We can try to manually divide the interval into m subintervals, and apply
Gaussian quadrature on each of them.
Gauss Quad on 2 intervals = 15.2337481743
Gauss Quad on 4 intervals = 15.4180879236
Gauss Quad on 8 intervals = 15.5488761931
Gauss Quad on 16 intervals = 15.6415116260
Gauss Quad on 32 intervals = 15.7070690191
Still not converges very obviously.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 97 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature
Solution (3/3).
x2 y2
Note that the parametric formula of ellipse + = 1 is
9 4
x = 3 cos(t), y = 2 sin(t), 0 ≤ t ≤ 2π
z f (x, y)
a c
d
b y
R
x
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 99 / 116
Chapter 4. Numerical Differentiation and Integration 4.6 Multiple Integral
Trapezoidal Rule
We first consider the Trapezoidal rule. Recall that
b
b − ah
Z i
f (x)dx ≈ f (a) + f (b) .
a 2
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 100 / 116
Chapter 4. Numerical Differentiation and Integration 4.6 Multiple Integral
b−a d−c
hx = , hy = .
m n
xi = a + ihx , i = 1, 2, · · · , m.
yj = c + jhy , j = 1, 2, · · · , n.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 102 / 116
Chapter 4. Numerical Differentiation and Integration 4.6 Multiple Integral
10/5/20 11:10
MATLAB File for Composite PM /Users/xuzhang.../trapezoidComp2D.m
Trapezoidal 2D 1
function Q = trapezoidComp2D(fun,a,b,c,d,m,n)
hx = (b-a)/m;
hy = (d-c)/n;
% 1. Four corners
Q1 = fun(a,c)+fun(b,c)+fun(a,d)+fun(b,d);
% 4. internal nodes
Q4 = 0;
for i = 1:m-1
for j = 1:n-1
xi = a+i*hx; yj = c+j*hy;
Q4 = Q4 + 4*fun(xi,yj);
end
end
Q = (hx/2)*(hy/2)*(Q1+Q2+Q3+Q4);
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 103 / 116
Chapter 4. Numerical Differentiation and Integration 4.6 Multiple Integral
Gaussian Quadrature
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 104 / 116
Chapter 4. Numerical Differentiation and Integration 4.6 Multiple Integral
function Q = gaussQuad2D(fun,a,b,c,d,m,n)
% 1. On reference interval [-1,1]
[w1,t1] = gaussRef(m);
[w2,t2] = gaussRef(n);
% 2. Map to the actual interval
X = (b-a)/2*t1+(a+b)/2; % nodes in x direction
A = (b-a)/2*w1; % weight in x direction
Y = (d-c)/2*t2+(c+d)/2; % nodes in y direction
B = (d-c)/2*w2; % weight in y direction
Q = 0;
for i = 1:m
for j = 1:n
Q = Q + A(i)*B(j)*fun(X(i),Y(j));
end
end
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 105 / 116
Chapter 4. Numerical Differentiation and Integration 4.6 Multiple Integral
Example 24
Use Composite Trapezoidal rule and Gaussian Quadrature with m = 2 and
n = 3 to approximate
Z 2.0 Z 1.5
log(x + 2y)dydx
1.4 1.0
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 106 / 116
step size for the variable x is h = (b − a)/2, but the step size for y varies with x (see
Chapter 4. Numerical Differentiation
Figure 4.21) and Integration
and is written 4.6 Multiple Integral
d(x) − c(x)
Nonrectangular Regions k(x) =
2
.
Let R = {(x,
Figure : a ≤ x ≤ b, c(x) ≤ y ≤ d(x)} be a nonrectangular region
y) 4.21
z
z f (x, y)
R a c(x)
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 107 / 116
Chapter 4. Numerical Differentiation and Integration 4.6 Multiple Integral
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 108 / 116
ey/x dy dx.
Chapter 4. Numerical Differentiation and Integration 4.6 Multiple Integral 0.1 x3
This requires 121 evaluations of the function f (x, y) = ey/x and produces the value
0.0333054, which approximates the volume of the solid shown in Figure 4.22 to nearly
seven decimal places. Applying the Gaussian Quadrature Algorithm with n = m = 5 re-
quires only 25 function evaluations and gives the approximation 0.03330556611, which is
accurate to 11 decimal places.
Example 25
Figure 4.22
z
The volume of the solid on the 1 (0.1, 0.01, e0.1) (0.5, 0.25, e0.5)
0.1 x3
0.1
Use Gaussian quadrature to
approximate this integral. R (0.5, 0.25, 0)
(0.5, 0.125, 0)
0.5
x
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
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 109 / 116
Chapter 4. Numerical Differentiation and Integration 4.6 Multiple Integral
z = 0;
for i = 1:m
for j = 1:n
Bji = (funD(X(i))-funC(X(i)))/2*w2(j);
Yji = (funD(X(i))-funC(X(i)))/2*t2(j)+(funD(X(i))+funC(X(i)))/2;
z = z + A(i)*Bji.*fun(X(i),Yji);
end
end
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 110 / 116
Chapter 4. Numerical Differentiation and Integration 4.6 Multiple Integral
10/6/20 12:34
Solution. (2/2)AM /Users/xuzhang/Dropbox.../ex4_8_2.m
MATLAB Driver File 1 of 1
% ex_4_8_2
clc
format long
fun = @(x,y) exp(y./x);
funC = @(x) x.^3;
funD = @(x) x.^2;
a = 0.1; b = 0.5;
m = 1; n = 1;
Q1 = gaussQuad2DnonrectY(fun,a,b,funC,funD,m,n);
m = 2; n = 2;
Q2 = gaussQuad2DnonrectY(fun,a,b,funC,funD,m,n);
m = 3; n = 3;
Q3 = gaussQuad2DnonrectY(fun,a,b,funC,funD,m,n);
m = 4; n = 4;
Q4 = gaussQuad2DnonrectY(fun,a,b,funC,funD,m,n);
10/6/20 12:34 AM MATLAB C
m = 5; n = 5;
Q5 = gaussQuad2DnonrectY(fun,a,b,funC,funD,m,n);
-------------------------------
disp('-------------------------------') Gauss Quad 1x1 = 0.0306258369
disp(['Gauss Quad 1x1 = ', num2str(Q1, '%12.10f')]) Gauss Quad 2x2 = 0.0333453875
disp(['Gauss Quad 2x2 = ', num2str(Q2, '%12.10f')])
Gauss Quad 3x3 = 0.0333058313
disp(['Gauss Quad 3x3 = ', num2str(Q3, '%12.10f')])
disp(['Gauss Quad 4x4 = ', num2str(Q4, '%12.10f')]) Gauss Quad 4x4 = 0.0333055671
disp(['Gauss Quad 5x5 = ', num2str(Q5, '%12.10f')]) Gauss Quad 5x5 = 0.0333055661
>>
The Gaussian Quadrature results converge. The volume is approximately
0.0333056.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 111 / 116
Chapter 4. Numerical Differentiation and Integration 4.6 Multiple Integral
Example 26
Use Gaussian quadrature to approximate the surface area of the upper part of
the unit sphere x2 + y 2 + z 2 = 1 intersecting intersecting with the plane
z = 1/2
0.5
N 0
-0.5
0.5
-1
-1
-0.5
0 -0.5
0.5
-1
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 112 / 116
Chapter 4. Numerical Differentiation and Integration 4.6 Multiple Integral
Solution. (1/2)
We approximate the surface area of the sphere in the first octant
Solution. (2/3)
The partial derivatives
1 −x
fx (x, y) = (1 − x2 − y 2 )−1/2 (−2x) = p
2 1 − x2 − y 2
1 −y
fy (x, y) = (1 − x2 − y 2 )−1/2 (−2y) = p
2 1 − x2 − y 2
s
q x2 + y 2 1
1 + fx2 + fy2 = 1 + =p
1 − x2 − y 2 1 − x2 − y 2
The surface area formula z = f (x, y) is
ZZ q Z √3/4 Z √3/4−x2
1
1 + fx2 + fy2 dA = p dydx
R 0 0 1 − x2 − y 2
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 114 / 116
Chapter 4. Numerical Differentiation and Integration 4.6 Multiple Integral
>> 0.7856601777*4-pi
Xu Zhang (Oklahoma State University) ans = MATH 4513 Numerical Analysis Fall 2020 115 / 116
Chapter 4. Numerical Differentiation and Integration 4.6 Multiple Integral
Concluding Remarks
The integral over complicated 2D or 3D domains are usually
approximated through meshes (triangulations). That is to divide the
integral domain into triangles in 2D or tetrahedra in 3D, then apply
Gaussian quadrature on these simplicial geometries.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 116 / 116