0% found this document useful (0 votes)
82 views

NA Ch4 Student

This document section discusses numerical differentiation and introduces two methods: 1) Approximating the derivative of a function f(x) at a point x0 by computing the slope of a chord between points (x0) and (x0 + h) for small h. The accuracy depends on how small h is. 2) Approximating f(x) using a linear interpolating polynomial P1(x) and taking its derivative to estimate f'(x0), which is equivalent to the chord slope method. The error is bounded and depends on f''(x).

Uploaded by

Ahiduzzaman Ahir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

NA Ch4 Student

This document section discusses numerical differentiation and introduces two methods: 1) Approximating the derivative of a function f(x) at a point x0 by computing the slope of a chord between points (x0) and (x0 + h) for small h. The accuracy depends on how small h is. 2) Approximating f(x) using a linear interpolating polynomial P1(x) and taking its derivative to estimate f'(x0), which is equivalent to the chord slope method. The error is bounded and depends on f''(x).

Uploaded by

Ahiduzzaman Ahir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 116

MATH 4513 Numerical Analysis

Chapter 4. Numerical Differentiation and Integration

Xu Zhang

Department of Mathematics
Oklahoma State University

Text Book: Numerical Analysis (10th edition)


R. L. Burden, D. J. Faires, A. M. Burden

Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 1 / 116
Chapter 4. Numerical Differentiation and Integration

Table of Contents

Chapter 4. Numerical Differentiation and Integration


4.1 Numerical Differentiation
4.2 Elements of Numerical Integration
4.3 Composite Numerical Integration
4.4 Adaptive Quadrature Method
4.5 Gaussian Quadrature
4.6 Multiple Integrals

Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 2 / 116
Chapter 4. Numerical Differentiation and Integration

Numerical Differentiation and Integration


Motivation
Numerical integration and differentiation are generally key steps in many
numerical computations.
Introduction
For example, finding the length of a sheet of corrugated roofing which is
A sheet of corrugated roofing is constructed by pressing a flat sheet of aluminum into one
approximated by a sine wave
whose cross section has the form of a sine wave.
f (x) = sin(x), x ∈ [0, 48].

From Calculus, we know the length of the curve is


Z 48 p Z 48
A corrugated sheet 4 ft long is needed, the height of each wave isp
1 in. from the center
L= 1 + [f 0 (x)]2 dx = 1 + cos2 (x)dx = ?
line, and each wave has a period of0approximately 2π in. The problem0
of finding the length
of the initial flat sheet is one of determining the length of the curve given by f (x) = sin x
from x = The analytical
0 in. to x = 48 in. integral is not
From calculus weavailable. Welength
know that this needis to approximate this integral
numerically.
 48   48 
L =State University)
Xu Zhang (Oklahoma 1+ (f  (x))2 dx = 1 + (cosAnalysis
MATH 4513 Numerical x)2 dx, Fall 2020 3 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation

4.1 Numerical Differentiation

Goal: Develop numerical approximation of derivatives f 0 (x), f 00 (x), etc,


using only the values of the function f (x).

Recall that the derivative of f (x) at x0 is defined as

f (x0 + h) − f (x0 )
f 0 (x0 ) = lim .
h→0 h

Intuitively, we can choose small h and use

f (x0 + h) − f (x0 )
h
to approximation f 0 (x0 ).

Question: How “small” is small?

Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 4 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation

We use the formula above to approximate the derivative of f (x) = sin(x)


at x = 1. Note that the true value is f 0 (1) = cos(1).
1 1 1
We test with h = , , · · · , 15 , and find the relative error.
10 100 10
10/7/19 5:36 PM
A MATLAB Script /Users/xuzhang/Dropbox/Teac.../ex4_1_0

% Test the derivative approximation


% f(x) = sin(x), f'(x) = cos(x).
% This example we will approximation f'(1) = cos(1)
clear
format long
h = zeros(15,1);
err = zeros(15,1);
for i = 1:15
h(i) = 10^(-i);
app = (sin(1+h(i))-sin(1))/h(i);
err(i) = abs(app - cos(1))/cos(1);
end
disp(' h relative error')
disp([h,err])

Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 5 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation

10/7/19 5:37 PM MATLAB Command Window


Numerical Result
>> ex4_1_0
h relative error
0.100000000000000 0.079471349402736
0.010000000000000 0.007803640314835
0.001000000000000 0.000778870464261
0.000100000000000 0.000077872052482
0.000010000000000 0.000007787052229
0.000001000000000 0.000000778724808
0.000000100000000 0.000000077415348
0.000000010000000 0.000000005496710
0.000000001000000 0.000000097244201
0.000000000100000 0.000000108237621
0.000000000010000 0.000002163055846
0.000000000001000 0.000080029673119
0.000000000000100 0.001358343083757
0.000000000000010 0.006860929812673
0.000000000000001 0.027409112053748

>>

At the beginning, the relative error gets smaller as h decreases.


After the eighth iteration, h = 10−8 , the error starts to grows as we further
reduce the size of h. Why?
We will answer this question at the end of this section.

Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 6 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation

Idea: Approximate f (x) by an interpolating polynomial P (x), then use


P 0 (x) to approximate the derivative f 0 (x).
Consider the linear Lagrange polynomials at x0 and x1 = x0 + h.
x − x1 x − x0
L1,0 (x) = , L1,1 (x) = .
x0 − x1 x1 − x0

Then the linear Lagrange interpolation P1 (x) of f (x) is

P1 (x) = f (x0 )L1,0 (x) + f (x1 )L1,1 (x)


x − x1 x − x0
= f (x0 ) + f (x1 )
x0 − x1 x1 − x0
x − x1 x − x0
= f (x0 ) + f (x0 + h) .
−h h
Taking the derivative, we get
f (x0 + h) − f (x0 )
P10 (x) = ≈ f 0 (x0 ).
h
This formula is the same as the definition of derivative of f 0 (x0 ).
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 7 / 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!

So, the error bound of the derivative is


 00 
0 0 f (ξ(x))
f (x) − P1 (x) = Dx (x − x0 )(x − x1 ) .
2!
1 (x − x0 )(x − x1 )
= (2x − x0 − x1 )f 00 (ξ) + Dx (f 00 (ξ))
2 2
where ξ is between x0 and x.
When x = x0 , the second term on the right hand side is zero. We have

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

Forward/Backward Difference Formula


f (x0 + h) − f (x0 )
f 0 (x0 ) ≈ . (4.1)
h

If h > 0, it is called forward-difference formula.


If h < 0, it is called backward-difference formula.

Error Bound for Forward/Backward Difference Formula


For small value of h, we have

f (x0 + h) − f (x0 ) M |h|


f 0 (x0 ) − ≤ , (4.2)
h 2

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

In general, let Pn (x) be the Lagrange interpolation of f (x) at n + 1 distinct


points x0 , x1 , · · · , xn . That is
n
X
Pn (x) = f (xk )Ln,k (x)
k=0

where n
Y x − xj
Ln,k (x) = .
j6=k
x k − xj
j=0

The derivative of f (x) can be approximated by


n
X
0
f (x) ≈ Pn0 (x) = f (xk )L0n,k (x). (4.3)
k=0

The error when x = xj is


n
f (n+1) (ξ) Y
f 0 (xj ) − Pn0 (xj ) = (xj − xk ).
(n + 1)! k=0
k6=j

(4.3) is called an (n + 1)-point formula to approximate f 0 (xj ).


Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 10 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation

To approximate f 0 (x), 3-point and 5-point formulas are commonly used.


For three-point formulas, we have for each j = 0, 1, 2,
2
f 000 (ξ) Y
f 0 (xj ) = P20 (xj ) + (xj − xk ).
3! j=0
j6=k

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

If the nodes are equally spaced, i.e., x1 = x0 + h, x2 = x0 + 2h, then

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

Three-Point Endpoint Formula for f 0


1 h2
f 0 (x0 ) =
[−3f (x0 ) + 4(x0 + h) − f (x0 + 2h)] + f 000 (ξ) (4.4)
2h 3
where ξ lies between x0 and x0 + 2h.

Remark The other end-point formula can be obtained by replacing h with −h.

Three-Point Midpoint Formula for f 0


1 h2
f 0 (x0 ) =
[f (x0 + h) − f (x0 − h)] − f 000 (ξ) (4.5)
2h 6
where ξ lies between x0 − h and x0 + 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

merical Differentiation and Integration

An illustration of the three-point midpoint approximation


y

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

Five-Point Midpoint Formula for f 0


1 h i h4
f 0 (x0 ) = f (x0 −2h)−8(x0 −h)+8f (x0 +h)−f (x0 +2h) + f (5) (ξ). (4.6)
12h 30
where ξ lies between x0 − 2h and x0 + 2h.

Five-Point Endpoint Formula for f 0


1 h
f 0 (x0 ) = − 25f (x0 ) + 48f (x0 + h) − 36(x0 + 2h)
12h
i h4 (4.7)
+ 16f (x0 + 3h) − 3f (x0 + 4h) + f (5) (ξ)
5
where ξ lies between x0 and x0 + 4h.

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

Use three-point and five-point formulas to approximate f 0 (2.0).

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.

Using 3-point midpoint formula (4.5) with h = 0.1 we have


1
f 0 (2.0) ≈ [f (2.1) − f (1.9)] = 5(17.148957 − 12.7703199) = 22.228790.
0.2

Using 3-point midpoint formula (4.5) with h = 0.2 we have


1
f 0 (2.0) ≈ [f (2.2) − f (1.8)] = 2.5(19.855030 − 10.889365) = 22.41416.
0.4

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

Approximation of Higher Derivatives

Methods can also be derived to find approximations to higher derivatives


of a function using only tabulated values of the function at various points.
We will use Taylor expansion to derive the 2nd-order derivative
approximation. Note that

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 ).

Adding these two equations we obtain

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

Subtracting 2f (x0 ) on both side and dividing by h2 , we obtain

f (x0 + h) − 2f (x0 ) + f (x0 − h) h2  (4) 


f 00 (x0 ) = 2
− f (ξ1 ) + f (4) (ξ−1 ) .
h 24

Using the Intermediate Value Theorem, if f (4) is continuous, then there


exists ξ ∈ (x0 − h, x0 + h) such that
1 (4)
f (4) (ξ) = f (ξ1 ) + f (4) (ξ−1 ) .

2

Midpoint Formula for f 00


f (x0 + h) − 2f (x0 ) + f (x0 − h) h2 (4)
f 00 (x0 ) = − f (ξ). (4.8)
h2 12
for some ξ ∈ (x0 − h, x0 + h).

Remark: The truncation error of the midpoint formula (4.8) is O(h2 ).

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

Round-Off Error Stability


Example 3

Use three-point midpoint formula to approximate f 0 (0.9) where f (x) = sin(x)


with h = 10−i , i = 1, 2, · · · , 12.

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

Let us examine the 3-pt midpoint formula:


f (x0 + h) − f (x0 − h) h2 000
f 0 (x0 ) = − f (ξ).
2h 6
When evaluating f (x0 + h) and f (x0 − h) with computer, there are
round-off errors, denoted by e(x0 + h) and e(x0 − h). Then our
computational actually use the values f˜(x0 ± h) given by
f (x0 + h) = f˜(x0 + h) + e(x0 + h), f (x0 − h) = f˜(x0 − h) + e(x0 − h).

The total error in the approximation is

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

|e(x0 ± h)| ≤ ε, |f 000 (x)| ≤ M,

then the total computational error

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

Note that as h is reduced, the approximation error M h2 /6 decreases.


However, the round-off error ε/h grows. If h is too small (e.g. h < 10−6 as
in previous example), the round-off error dominates the calculation.

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],

M = max |f 000 (x)| = max | cos(x)| = cos(0.8) ≈ 0.69671.


x∈[0.8,1] x∈[0.8,1]

In a 64-bit computer, the machine precision ε ≈ 2.22 × 10−16 . Hence, the


total error
M h2 ε
e(h) = +
6 h
Mh ε
will reach its minimum where e0 (h) = − 2 = 0. That is
3 h
 1/3  1/3
3ε 3 × 2.22−16
h= = ≈ 0.0000098515.
M 0.69671

The value of h is optimal for the 3pt-midpoint formula on a 64-bit


computer. This is consistent with the numerical experiment results, i.e., h
is between 10−6 and 10−5 .
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 24 / 116
Chapter 4. Numerical Differentiation and Integration 4.1 Numerical Differentiation

Similar argument can be used to analyze why the forward difference


formula reaches the best accuracy around h = 10−8 . (the first example of
this section). (Exercise)

In practice, we cannot compute an optimal h to use in approximating the


derivative, since we have no knowledge of the derivatives of the function.
But we must remain aware that reducing the step size h will not always
improve the approximation.

Numerical differentiation, as an approximation method, is unstable, since


the small values of h needed to reduce truncation error also cause the
round-off error to grow.

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

4.2 Elements of Numerical Integration

In practical applications, we often need to evaluate the definite integral of


a function Z b
f (x)dx. (4.9)
a

However, the antiderivative is often hard to obtain, sometimes there is no


explicit antiderivative.
In this section, we introduce how to numerically approximate of the
integral (4.9). The method is called numerical quadrature.
The quadrature in this section is based on interpolation.

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

Let {x0 , x1 , · · · , xn } be a set of nodes from [a, b]. The nth-degree


Lagrange interpolating polynomial is
n
X
Pn (x) = f (xi )Li (x).
i=0
The idea is to use the integral of Pn to approximate the integral of f :
Z b Z b Z bX n
f (x)dx ≈ Pn (x)dx = f (xi )Li (x)dx
a a a i=0
n Z b n
Li (x)dx ,
X X
= f (xi ) ai f (xi ).
i=0 a i=0
The quadrature formula is
Z b Xn Z b
f (x)dx ≈ ai f (xi ), where ai = Li (x)dx, i = 0, 1, · · · , n,
a i=0 a
(4.10)
The error of the numerical quadrature (4.10) is given by
Z b n
1 Y
E(f ) = f (n+1) (ξ(x)) (x − xi )dx.
(n + 1)! a i=0
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 27 / 116
Chapter 4. Numerical Differentiation and Integration 4.2 Elements of Numerical Integration

Quadrature formula using first Lagrange polynomials

Let x0 = a, x1 = b, and h = b − a. Then


x − x1 x − x0
P1 (x) = f (x0 ) + f (x1 ) .
x0 − x1 x1 − x0

Then, the integral can be approximated by


b x1  
x − x1 x − x0
Z Z
f (x)dx ≈ f (x0 ) + f (x1 ) dx
a x0 x0 − x1 x1 − x0
x x1
(x − x1 )2 1 (x − x0 )2
= f (x0 ) + f (x1 )
2(x0 − x1 ) x0 2(x1 − x0 ) x0
x0 − x1 x1 − x0
= −f (x0 ) + f (x1 )
2 2
hh i
= f (x0 ) + f (x1 ) .
2

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

Illustration of Numerical Quadrature


y
y  f (x)

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.

This approximation gives no error if the function f is a linear polynomial.


We will analyze this error in the next slide.

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

Quadrature using Second Lagrange polynomials


b−a
Let h = , x0 = a, x1 = a + h, and x2 = b. Then
2
Z b Z x2 
(x − x1 )(x − x2 ) (x − x0 )(x − x2 )
f (x)dx = f (x0 ) + f (x1 )
a x0 (x 0 − x 1 )(x 0 − x 2 ) (x1 − x0 )(x1 − x2 )
 Z x2
(x − x0 )(x − x1 )
+f (x2 ) dx + E2 (x)dx
(x2 − x0 )(x2 − x1 ) x0
h h5
= [f (x0 ) + 4f (x1 ) + f (x2 )] − f (4) (ξ).
3 90

It is good exercise to go through the above calculation. We obtain

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.)

Illustration of Simpson’s Rule


4.4
y
y  f (x)

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

(a) x, (b) x2 , (c) x4 , (d) (1 + x)−1 , (e) sin(x)

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

How to measure the precision of a numerical quadrature formula?

Definition 5 (degree of precision)


The degree of precision of a quadrature formula is the largest positive
integer n such that the formula is exact for xk , for each k = 0, 1, · · · , n.

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.

There are two types of Newton-Cotes formulas: open and closed


Newton-Cotes Formulas, depending on whether or not to include the
endpoints in the quadrature formulas.

Closed Newton-Cotes Formula


The (n + 1)-point closed Newton-Cotes quadrature formula is
Z b n
X
f (x)dx ≈ ai f (xi ), (4.13)
a i=0
where
h = (b − a)/n, x0 = a, and xi = x0 + ih.
Z xn Z xn Y
(x − xj )
ai = Li (x)dx = dx.
x0 x0 (x i − xj )
j=0,j6=i

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

Illustration of Closed Newton-Cotes Formula


Figure 4.5
y

y = Pn(x)
y = f (x)

a  x0 x1 x2 xn1 xn  b x

Theorem 7 (Error Bound for Closed Newton-Cotes Formulas)


The (n + 1)-pointThe formula Newton-Cotes
closed assumes the form formula obeys the following error estimates
Z b n
 b n Z n
n+3 (n+2)
X fh(x) dx ≈
f ai f (ξ)
(xi ),
f (x)dx = ai f (xi ) + a i=0 t2 (t − 1) · · · (t − n)dt
a i=0
(n + 2)! 0
where
if n is even and f ∈ C n+2 (a, b), and
 xn  xn  n
(x − xj )
Z b n ai = Li (x) n+2
dx = (n+1) Z dx. n
X x0 h fx0 j=0 (x(ξ)i − xj )
f (x)dx = ai f (xi ) + j =i t(t − 1) · · · (t − n)dt
a i=0
(n + 1)! 0
The following theorem details the error analysis associated with the closed Newton-
if n is odd andCotes C n+1 For
f ∈formulas. (a,ab).
proof of this theorem, see [IK], p. 313.


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

Several Closed Newton-Cotes Formulas


b−a
For 0 ≤ i ≤ n, xi = a + ih and h = .
n
n h Formula Error Name
3
h h 00
1 b−a (f0 + f1 ) − f (ξ) Trapezoidal Rule
2 12
b−a h h5
2 (f0 + 4f1 + f2 ) − f (4) (ξ) Simpson’s Rule
2 3 90
b−a 3h 3h5 (4)
3 (f0 + 3f1 + 3f2 + f3 ) − f (ξ) Simpson’s 3/8 Rule
3 8 80
b−a 2h 8h7 (6)
4 (7f0 + 32f1 + 12f2 + 32f3 + 7f4 ) − f (ξ) Boole’s Rule
4 45 945

Here, we use the simplified notation fi = f (xi ).

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

Another class of Newton-Cotes quadrature formulas do not include the


endpoints of [a, b] as nodes for interpolation. To unify the notation, we
label these endpoints x−1 = a and xn+1 = b.

Open Newton-Cotes Formula


The (n + 1)-point open Newton-Cotes quadrature formula is
Z b n
X
f (x)dx ≈ ai f (xi ), (4.14)
a i=0

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

Theorem 8 (Error Bound forThe


Open Newton-Cotes Formulas)
following theorem is analogous to Theorem 4.2; its proof is contained in [IK],
The (n + 1)-point closed p.Newton-Cotes
314.
formula obeys the following error estimates
Z b n n n+3 (n+2)
XSuppose that i=0hai f (xif) denotes (ξ) the (n + n+1
Z
Theorem 4.3 1)-point2open Newton-Cotes formula with
f (x)dx = ai=f (x
x−1 )+
a, xin+1 t (tξ−
= b, and h = (b − a)/(n + 2). There exists 1)b)· for
∈ (a, · · which
(t − n)dt
a i=0 
(n + 2)! −1 
bn  hn+3 f (n+2) (ξ ) n+1
f (x) dx = a f (x ) + t 2 (t − 1) · · · (t − n) dt,
if n is even and f ∈ C n+2 (a, b),a and i=0
i i
(n + 2)! −1
Z b n n+2
fb], and (ξ) n+1
(n+1) Z
X if n is even and f ∈hC n+2 [a,
f (x)dx = ai f (x i) +
 b
t(t − 1) · · · (t − n)dt

a i=0
(n
n + 1)! hn+2 f (n+1)
−1(ξ ) n+1
f (x) dx = ai f (xi ) + t(t − 1) · · · (t − n) dt,
(n + 1)! −1
if n is odd and f ∈ C n+1 (a, b).
a i=0

if n is odd and f ∈ C n+1 [a, b].


Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 41 / 116
Chapter 4. Numerical Differentiation and Integration 4.2 Elements of Numerical Integration

Several Open Newton-Cotes Formulas


b−a
Let h = and x0 = a + h. For 0 ≤ i ≤ n, let xi = x0 + ih.
n+2

n h Formula Error Name


b−a h3 00
0 2hf0 f (ξ) Midpoint rule
2 3
b−a 3h 3h3 00
1 (f0 + f1 ) f (ξ) N/A
3 2 4
b−a 4h 28h5 (4)
2 (2f0 − f1 + 2f2 ) f (ξ) N/A
4 3 90
b−a 5h 95h5 (4)
3 (11f0 + f1 + f2 + 11f3 ) f (ξ) N/A
5 24 144

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

10/14/19 11:42 PM /Users/xuzhang/Drop.../NewtonCotesCl.m


Solution (2/5): Subroutine for Closed NC formulas
function y = NewtonCotesCl(fun,a,b,n)

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

10/14/19 11:42 PM /Users/xuzhang/Drop.../NewtonCot


Solution (3/5): Subroutine for Open NC formulas
function y = NewtonCotesOp(fun,a,b,n)

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);

formatSpec = '%2i % .8f % .8f % .8f \n';


disp('Closed Newton Cotes Formula')
disp(' n True Close N-C Error')
disp('-------------------------------------------------')
fprintf(formatSpec,1,y,y1c,abs(y-y1c))
fprintf(formatSpec,2,y,y2c,abs(y-y2c))
fprintf(formatSpec,3,y,y3c,abs(y-y3c))
fprintf(formatSpec,4,y,y4c,abs(y-y4c))
disp(' ')
disp('Open Newton Cotes Formula')
disp(' n True Open N-C Error')
disp('-------------------------------------------------')
fprintf(formatSpec,0,y,y0o,abs(y-y0o))
fprintf(formatSpec,1,y,y1o,abs(y-y1o))
fprintf(formatSpec,2,y,y2o,abs(y-y2o))
fprintf(formatSpec,3,y,y3o,abs(y-y3o))

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

9/16/20 6:23 PM MATLAB Command Window


Solution (5/5): Output from MATLAB Command Window
>> ex4_2_1
Closed Newton Cotes Formula
n True Close N-C Error
-------------------------------------------------
1 0.29289322 0.27768018 0.01521304
2 0.29289322 0.29293264 0.00003942
3 0.29289322 0.29291070 0.00001748
4 0.29289322 0.29289318 0.00000004

Open Newton Cotes Formula


n True Open N-C Error
-------------------------------------------------
0 0.29289322 0.30055886 0.00766565
1 0.29289322 0.29798754 0.00509432
2 0.29289322 0.29285866 0.00003456
3 0.29289322 0.29286923 0.00002399
>>

Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 47 / 116
Chapter 4. Numerical Differentiation and Integration 4.3 Composite Numerical Integration

4.3 Composite Numerical Integration

The Newton-Cotes formula is generally not suitable for large integrating


intervals.

High-degree formulas would be required, and the values of the


coefficients in these formulas are difficult to obtain.

Newton-Cotes formulas are based on interpolating polynomials that use


equally-spaced nodes, a procedure that is inaccurate over large intervals
because of the oscillatory nature of high-degree polynomials.

In this section, we discuss a piecewise approach to numerical integration


that uses the low-order Newton-Cotes formulas. These are the
techniques most often applied.

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.

The relative error is 0.00496.

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.

The relative error is 0.000337.

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)

a  x0 x2 x2j2 x2j1 x2j b  xn 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

Composite Simpson’s Rule


The composite Simpson’s rule on the interval [a, b] is given by
 
Z b (n/2)−1 n/2
h X X
f (x)dx ≈ f (a) + 2 f (x2j ) + 4 f (x2j−1 ) + f (b) . (4.15)
a 3 j=1 j=1

b−a
where n is even, h = , and xj = a + jh for each j = 0, 1, · · · , n.
n

Theorem 11 (Error of Composite Simpson’s rule)


Assume f ∈ C 4 [a, b], then the error of composite Simpson’s rule is

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

The subdivision approach can be applied to any Newton-Cotes formula. For


example, the composite Trapezoidal rule requires only one interval for each
4.4 odd
application, so the integer n can be either Composite Numerical Integration
or even. 207

Illustration of Composite Trapezoidal Rule


y

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

Composite Trapezoidal Rule


Suppose that the interval [a, b] is split up into n sub-intervals. The composite
trapezoidal rule is given by
 
Z b n−1
h X
f (x)dx ≈ f (a) + 2 f (xj ) + f (b) (4.17)
a 2 j=1

where xj = a + jh for each j = 0, 1, · · · , n, with h = (b − a)/n.

Theorem 12 (Error of Composite Trapezoidal Rule)


Assume f ∈ C 2 [a, b], then the error of composite trapezoidal rule is

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.

9 Illustration of Composite Midpoint Rule


y
y  f (x)

a  x1 x 0 x1 x2j1 x2j x2j1 xn1 x n b  x n1 x

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

Composite Midpoint Rule


Suppose that the interval [a, b] is split up into n + 1 sub-intervals. The
composite midpoint rule is given by
Z b n/2
X
f (x)dx = 2h f (x2j ) (4.19)
a j=0

b−a
where xj = a + (j + 1)h for each −1 ≤ j ≤ n + 1, with h = .
n+2

Theorem 13 (Error of Composite Midpoint Rule)


Assume f ∈ C 2 [a, b], then the error of composite midpoint rule is

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

by (a) Composite Trapezoidal rule and (b) Composite Simpson’s rule.

Solution. (1/3)
Note that the error bound of composite trapezoidal rule is

b − a 2 00
E(f ) = − h f (µ).
12

On the interval [0, π], we have

πh2 00 πh2 πh2


E(f ) ≤ f (µ) = | − sin(µ)| ≤ < 0.00002 =⇒ h < 0.008740.
12 12 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

πh4 (4) πh4 πh4


f (µ) = | sin(µ)| ≤ < 0.00002 =⇒ h < 0.1840.
180 180 180

Since h = π/n, then


π
< 0.1840 =⇒ n > 17.08
n
We need at least 18 subintervals to ensure the desired accuracy.

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)

Using composite Simpson’s rule with 18 subintervals, we obtain


Z π
sin(x)dx ≈ 2.0000103.
0

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

How long is the track?


How long is the track?

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

Using composite Simpson’s rule with h = 6,


Z 84
6h
l= s(t)dt ≈ 124 + 4(134 + 156 + 133 + 109 + 85 + 89 + 116)
0 3
i
+2(148 + 147 + 121 + 99 + 78 + 104) + 123
= 9858

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

4.4 Adaptive Quadrature Method


The composite formulas are very effective in most situations, but they suffer occasionally
because they require the use of equally-spaced nodes. This is inappropriate when integrating
a function on an interval that contains both regions with large functional variation and regions
The compositewith small formulas, such as composite Simpson’ rule, are very
functional variation.
effective in most situations, but they suffer occasionally because they
Illustration The unique solution to the differential equation y + 6y + 25 = 0 that additionally satisfies
require the use of equally-spaced nodes.
y(0) = 0 and y (0) = 4 is y(x) = e−3x sin 4x. Functions of this type are common in
mechanical engineering because they describe certain features of spring and shock absorber
This is inappropriate when integrating a function on an interval that
systems, and in electrical engineering because they are common solutions to elementary
contains circuit
bothproblems.
regions Thewith large
graph of y(x) forfunctional
x in the intervalvariation and
[0, 4] is shown regions
in Figure 4.11. with
small functional variation. For example, consider the integral of the
−3x
function
Figure 4.11 y = e sin(4x) over the interval [0, 4].
y

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

How to determine what technique should be applied on various portions


of the interval of integration, and how accurate can we expect the final
approximation to be?

If the approximation error for an integral on a given interval is to be evenly


distributed, a smaller step size is needed for the large-variation regions
than for those with less variation.

An efficient technique should predict the amount of functional variation


and adapt the step size as necessary. These methods are called
Adaptive quadrature methods.

Adaptive methods are particularly popular for inclusion in professional


software packages because, in addition to being efficient, they generally
provide approximations that are within a given specified tolerance.

Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 66 / 116
Chapter 4. Numerical Differentiation and Integration 4.4 Adaptive Quadrature Method

In this section, we consider the adaptive Simpson’s rule, but the


technique can be modified to use other composite methods.
Rb
Suppose we want to approximate a f (x)dx by the Simpson’s rule

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

Now we have two error bounds:


Z b     5
a+b a+b 1 h (4) ˜
f (x)dx − S(a, ) + S( , b) = − f (ξ) (4.21)
a 2 2 16 90
b
h5 (4)
Z
f (x)dx − S(a, b) = − f (ξ) (4.22)
a 90
where ξ and ξ˜ are both in (a, b).
˜ Thus,
The error estimation is derived by assuming f (4) (ξ) ≈ f (4) (ξ).

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

Substituting (4.23) in (4.21) and (4.22), we have


Z b  
a+b a+b
f (x)dx − S(a, ) + S( , b) (4.24)
a 2 2
 
1 a+b a+b
≈ S(a, b) − S(a, ) + S( , b) .
15 2 2

If we assign the tolerance


 
a+b a+b
S(a, b) − S(a, ) + S( , b) < 15, (4.25)
2 2

then we expect to have


Z b  
a+b a+b
f (x)dx − S(a, ) + S( , b) < , (4.26)
a 2 2

We may use the computable quantity (4.25) as an error indicator to


approximate the error.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 70 / 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.

If the approximation on one of the subintervals fails to be within the


tolerance /2, then that subinterval is itself subdivided, and the procedure
is reapplied to the two subintervals to determine if the approximation on
each subinterval is accurate to within /4.

This halving procedure is continued until each portion is within the


required tolerance.

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];

if abs(Q1+Q2-Q) > 15*tol


tol = tol/2;
[Q1,X1] = simpsonAdpt(fun,a,m,tol);
[Q2,X2] = simpsonAdpt(fun,m,b,tol);
Q = Q1+Q2;
% remove the repeated nodes and sort these nodes
X = sort(unique([X1,X2]));
end

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

Solution (1/2): MATLAB


10/4/20 2:41Driver File
PM /Users/xuzhang/Dropbox/.../ex4_6_2.m 1 of 1

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

10/4/20 2:46 PM MATLAB Command Window 1 of


Solution (2/2): MATLAB Driver File
-------------------------------------------------------------------
pt hmin True Integral Adaptive Simpson Abs Error
-------------------------------------------------------------------
44 0.01562 -1.42602476 -1.42593843 0.00008633

-------------------------------------------------------------------
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

In the region the function is steeper, there are more points.


Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 77 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature

4.5 Gaussian Quadrature

Recall Newton-Cotes Formulas (e.g. Trapezoidal, Simpson’s rule):


Z b n
X Z b
f (x)dx ≈ ai f (xi ), ai = Li (x)dx.
a i=1 a

They use equally-spaced points: xi = a + ih.


The degree of precision is at most n. i.e., it is exact for polynomial of
degree no more than n.

Question: Can we choose the evaluating points in an optimal, rather than an


equally-spaced way in order to get higher-degree precision?

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

In this section, we introduce a numerical quadrature that gives the best


approximation of the integral given the number of the quadrature points. It
is called the Gaussian Quadrature.
We need to determine a set of nodes x1 , x2 , · · · , xn in the interval [a, b],
and a set of coefficients (or weights) c1 , c2 , · · · , cn such that
Z b n
X
f (x)dx ≈ ci f (xi ) (4.27)
a i=1

gives the highest possible degree of precision.

Remark
There are 2n parameters to determine, and the polynomial of degree
2n − 1 also contains 2n parameters.

Thus, we expect the n-point Gaussian quadrature (4.27) to be exact for


polynomials of degree 2n − 1.

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

is accurate whenever f (x) is a polynomial of degree up to 3.


Due to the linearity of integral operator, that is
Z 1
(a0 + a1 x + a2 x2 + a3 x3 )dx
−1
Z 1 Z 1 Z 1 Z 1
= a0 1dx + a1 xdx + a2 x2 dx + a3 x3 dx.
−1 −1 −1 −1

It suffices to test for monomials f (x) = 1, x, x2 , and x3 .

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

In general, to determine the Gaussian nodes and weights for higher


degrees, we introduce a family of orthogonal polynomials, called
Legendre polynomials {Pn }.

The first few Legendre polynomials are

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

(n + 1)Pn+1 (x) = (2n + 1)xPn (x) − nPn−1 (x), n ≥ 1.

Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 83 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature

Illustration of Legendre Polynomials


1

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)

-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1

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

Theorem 19 (Gaussian Quadrature Weights)

Suppose that x1 , x2 , · · · , xn are the roots of the n-th Legendre polynomial


Pn (x), and that for each i = 1, 2, · · · , n, the number wi is defined by
1 n
x − xj
Z Y
wi = dx. (4.28)
−1 j=1,j6=i xi − xj

If P (x) is any polynomial of degree less than 2n, then


Z 1 n
X
P (x)dx = wi f (xi ).
−1 i=1

The proof of this theorem is on Page 231-232 on the text book.


While the weight wi for the quadrature rule can be generated from (4.28),
but both the weights wi and the roots xi of the Legendre polynomials are
extensively tabulated.

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

Nodes and Weights of Gaussian Quadratures onto[−1,


n = 5)
1]

Number of points, n Points, x i Approximately, x i Weights, w i Approximately, w i

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

Evaluating f (x) = ex cos(x) we have

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.

The exact integration is 1.9334215. The absolute error is 3.1 × 10−5 .

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

The absolute error is 3.1 × 10−5 .


The Simpson’s Rule (also 3-point) gives
1
S= [f (−1) + 4f (0) + f (1)] ≈ 1.8891533.
3
The absolute error is 4.4 × 10−2 .
The composite Simpson’s Rule with 13 points gives

S12 ≈ 1.93338827

the error is 3.3 × 10−5 .


The adaptive Simpsons’ Rule also requires at least 15 points to get a
more accurate approximation (error = 2.5 × 10−5 )
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 88 / 116
Chapter 4. Numerical Differentiation and Integration 4.5 Gaussian Quadrature

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

Using the Gaussian quadrature on the reference interval [−1, 1] we obtain

Gaussian Quadrature on [a, b]


Z b n
X
f (x)dx ≈ ci f (xi ) ,
a i=1

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

Theorem 21 (Error Estimate for Gaussian Quadrature)


Let f ∈ C 2n [a, b], the Gaussian quadrature has the error estimate
n
bY
f (2n) (ξ)
Z
E= (x − xi )2 dx
(2n)! a i=1

where ξ is a number between a and b.

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

9/28/20 4:54 PM /Users/xuzhang/Dropbox/Teac.../gaussQuad.m


MATLAB File for Gaussian Quadrature
function y = gaussQuad(fun,a,b,n)

%% On reference interval [-1,1]


if n == 1
t = 0; w = 2;
elseif n == 2
t = [-sqrt(3)/3,sqrt(3)/3];
w = [1,1];
elseif n == 3
t = [-sqrt(3/5),0,sqrt(3/5)];
w = [5/9,8/9,5/9];
elseif n == 4
t1 = sqrt(3/7-2/7*sqrt(6/5));
t2 = sqrt(3/7+2/7*sqrt(6/5));
t = [-t1,t1,-t2,t2];
c = (18+sqrt(30))/36; d = (18-sqrt(30))/36;
w = [c,c,d,d];
elseif n == 5
t1 = 1/3*sqrt(5-2*sqrt(10/7));
t2 = 1/3*sqrt(5+2*sqrt(10/7));
t = [0,-t1,t1,-t2,t2];
c = (322+13*sqrt(70))/900; d = (322-13*sqrt(70))/900;
w = [128/225,c,c,d,d];
end

%% Map to the actual interval


X = (b-a)/2*t+(a+b)/2;
C = (b-a)/2*w;

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

A driver file for9/28/20


Example5:04
22PM /Users/xuzhang/Dropbox/Teachi.../ex4_7_2.m 1

% 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

MATLAB Output 9/28/20 5:04 PM MATLAB Command Window 1

----------------------------------------------------
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

Approximate the circumference of the ellipse 4x2 + 9y 2 = 36 using Gaussian


quadrature. The true value is 15.86544. There is no elementary formula for the
circumference of the an ellipse.

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

The circumference of the ellipse is


3 3 1/2
4x2
Z Z 
p
l=4 1 + [y 0 (x)]2 dx = 4 1+ dx
0 0 9(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π

The circumference of the ellipse is


Z π/2 p Z π/2 q
l=4 [x0 (t)]2 + [y 0 (t)]2 dt = 4 9 sin2 (t) + 4 cos2 (t)dx
0 0

Now there is no singular point. Apply Gaussian quadrature then we have


Gauss Quad 1pt = 16.0190422444
Gauss Quad 2pt = 15.8297617432
Gauss Quad 3pt = 15.8679352978
Gauss Quad 4pt = 15.8654872322
Gauss Quad 5pt = 15.8654236216
The convergence pattern is obvious. The relative error for the 5pt
quadrature is 1.03 × 10−6 .
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 98 / 116
c. 4. Numerical
Chapter Use Gaussian Quadrature
Differentiation with
and n = 2 on the
Integration Multiple [−1,
intervals
4.6 −0.5], [−0.5, 0], [0, 0.5] and [0.5, 1].
Integral
d. Give an explanation for the accuracy of the results.

4.6 Multiple Integral


4.8 Multiple Integrals
In this section, we extend the numerical quadratures to multiple integrals.
The techniques
For example, discussedintegral
the double in the previous sections can be modified for use in the approxi-
mation of multiple integrals. Consider the double integral
Z Z 
ff(x,
(x, y) dA,
y)dA
R
R
where R = { (x, y) | a ≤ x ≤ b, c ≤ y ≤ d }, for some constants a, b, c, and d, is a
where R isrectangular
some region
region
in thein R2(See
plane. . Figure 4.18.)
First we consider the simple case where R = [a, b] × [c, d] is a rectangle.
Figure 4.18
z

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

Now we apply it to the multiple integral


ZZ Z b Z d 
f (x, y)dA = f (x, y)dy dx
R a c
Z b  i
d − ch
≈ f (x, c) + f (x, d) dx
a 2
Z b Z b 
d−c
= f (x, c)dx + f (x, d)dx
2 a a
 
d − c b − a  b − a
≈ f (a, c) + f (b, c) + f (a, d) + f (b, d)
2 2 2
d−cb−a
= [f (a, c) + f (b, c) + f (a, d) + f (b, d)] .
2 2

Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 100 / 116
Chapter 4. Numerical Differentiation and Integration 4.6 Multiple Integral

Composite Trapezoidal Rule


Recall that the composite Trapezoidal rule is
Z b m−1
hh X i
f (x)dx ≈ f (a) + 2 f (xi ) + f (b)
a 2 i=1

where h = (b − a)/m, xi = a + ih for each i = 0, 1 · · · , m.


For multiple integral, we have
ZZ Z b Z d 
f (x, y)dA = f (x, y)dy dx
R a c
Z b n−1
hy h X i
≈ f (x, c) + 2 f (x, yj ) + f (x, d) dx
a 2 j=1
m−1 m−1
hy hx h X X
≈ f (a, c) + 2 f (xi , c) + f (b, c) + f (a, d) + 2 f (xi , d) + f (b, d)
2 2 i=1 i=1
n−1
X m−1
X i
+2 f (a, yj ) + 2 f (xi , yj ) + f (b, yj ) ,
j=1 i=1

where hx = (b − a)/m, xi = a + ihx for each i = 0, 1, · · · , m,


and hy = (d − c)/n, yj = c + jhy for each j = 0, 1, · · · , n.
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 101 / 116
Chapter 4. Numerical Differentiation and Integration 4.6 Multiple Integral

Composite Trapezoidal Rule


Z b Z d 
hy hx h
f (x, y)dy dx ≈ f (a, c) + f (b, c) + f (a, d) + f (b, d)
a c 2 2
m−1
X n−1
X
 
+2 f (xi , c) + f (xi , d) + 2 f (a, yj ) + f (b, yj )
i=1 j=1
m−1
X n−1
X i
+4 f (xi , yj ) .
i=1 j=1

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);

% 2. top and bottom boundary nodes


Q2 = 0;
for i = 1:m-1
xi = a+i*hx;
Q2 = Q2 + 2*(fun(xi,c) + fun(xi,d));
end

% 3. left and right boundary nodes


Q3 = 0;
for j = 1:n-1
yj = c+j*hy;
Q3 = Q3 + 2*(fun(a,yj) + fun(b,yj));
end

% 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

Applying the Gaussian quadrature in each direction, we have


n
!
ZZ Z b Z d  Z b X
f (x, y)dA = f (x, y)dy dx ≈ βj f (x, yj ) dx
R a c a j=1
m n
! m X
n
X X X
≈ αi βj f (xi , yj ) = αi βj f (xi , yj ).
i=1 j=1 i=1 j=1

m and n are numbers of Gaussian nodes in x and y direction, respectively.


αi and βj are Gaussian weights in x and y direction, respectively.
xi and yj are Gaussian nodes in x and y direction, respectively.
That is
b−a b−a a+b
αi = wi , xi = ti + , i = 1, · · · , m.
2 2 2
d−c d−c c+d
βj = wj , yj = tj + , j = 1, · · · , n.
2 2 2

Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 104 / 116
Chapter 4. Numerical Differentiation and Integration 4.6 Multiple Integral

MATLAB File for Gaussian Quadrature


10/5/20 11:30 2D
PM /Users/xuzhang/Dro.../gaussQuad2D.m 1 of 1

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

%% Subroutine of Gaussian Quadrature


function [w,t] = gaussRef(n)
if n == 1
t = 0; w = 2;
elseif n == 2
t = [-sqrt(3)/3,sqrt(3)/3];
w = [1,1];
elseif n == 3
t = [-sqrt(3/5),0,sqrt(3/5)];
w = [5/9,8/9,5/9];
elseif n == 4
t1 = sqrt(3/7-2/7*sqrt(6/5));
t2 = sqrt(3/7+2/7*sqrt(6/5));
t = [-t1,t1,-t2,t2];
c = (18+sqrt(30))/36; d = (18-sqrt(30))/36;
w = [c,c,d,d];
elseif n == 5
t1 = 1/3*sqrt(5-2*sqrt(10/7));
t2 = 1/3*sqrt(5+2*sqrt(10/7));
t = [0,-t1,t1,-t2,t2];
c = (322+13*sqrt(70))/900; d = (322-13*sqrt(70))/900;
w = [128/225,c,c,d,d];
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

The true integral is 0.4295545265.

10/5/20 11:54 PM /Users/xuzhang/Dropbox.../ex4_8_1.m 1 of 1


Solution.
% ex_4_8_1
clc
fun = @(x,y) log(x+2*y);
a = 1.4; b = 2; c = 1.0; d = 1.5;
m = 2; n = 3; 10/5/20 11:54 PM MATLAB Co
Q1 = trapezoidComp2D(fun,a,b,c,d,m,n);
Q2 = gaussQuad2D(fun,a,b,c,d,m,n); -------------------------------
True Integral = 0.4295545265
disp('-------------------------------') Trapezoid Quad = 0.4292636231
disp(['True Integral = ', num2str(Q, '%12.10f')])
Gaussian Quad = 0.4295547137
disp(['Trapezoid Quad = ', num2str(Q1,'%12.10f')])
disp(['Gaussian Quad = ', num2str(Q2,'%12.10f')])
disp(' ');disp('-------- Error ----------------') -------- Error ----------------
disp(['Error Trapezoid = ', num2str(abs(Q-Q1),'%12.10f')]) Error Trapezoid = 0.0002909034
disp(['Error Gaussian = ', num2str(abs(Q-Q2),'%12.10f')]) Error Gaussian = 0.0000001872
>>

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)

d(a) y  d(x) A(x)


d(b)
k(a)
k(b)
c(b) a
c(a)
y  c(x)
k(a  h) y
b
R y  d(x)
a ah b x
x y  c(x)
(a) (b)
On this region, the double integral
!
ZZ Z b Z d(x)
f (x, y)dA = f (x, y)dy dx
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.

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

Gaussian Quadrature on Nonrectangular Regions

We consider the Gaussian quadrature


!  
ZZ Z b Z d(x) Z b n
X
f (x, y)dA = f (x, y)dy dx ≈  βj (x)f (x, yj (x)) dx
R a c(x) a j=1

d(x) − c(x) d(x) − c(x) c(x) + d(x)


where βj (x) = wj , yj (x) = tj + .
2 2 2
Continue to apply Gaussian quadrature in x direction, we have
ZZ m X
X n
f (x, y)dA ≈ αi βj (xi )f (xi , yj (xi ))
R i=1 j=1

b−a b−a a+b


where αi = wi , xi = ti + .
2 2 2

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)

right is calculated by the integral (0.1, 0.001, e0.01)


y
Z 0.5 Z x2 0.25 (0.5, 0.125, e0.25)

ey/x dydx. 0.125

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

10/6/20 12:35 AM /Users/xuz.../gaussQuad2DnonrectY.m 1 of 1


Solution. (1/2) MATLAB Subroutine for Gaussian Quadrature
function z = gaussQuad2DnonrectY(fun,a,b,funC,funD,m,n)

%% On reference interval [-1,1]


[w1,t1] = gaussRef(m);
[w2,t2] = gaussRef(n);

%% 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

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

The surface area formula z = f (x, y) is


ZZ q
1 + [fx (x, y)]2 + [fy (x, y)]2 dA
R
p
2 2
p octant z = f (x, y) = 1 − x − y . The region R is a
Note that in the first
circle with radius 3/4. That is
r r
3 3
R = {(x, y) : 0 ≤ x ≤ , 0≤y≤ − x2 }
4 4
Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 113 / 116
Chapter 4. Numerical Differentiation and Integration 4.6 Multiple Integral

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

Solution. (3/3) 10/6/20 2:40 AM MATLAB Command Wind


Using the Gaussian Quadrature, we have
-------------------------------
Gauss Quad 1x1 = 0.7924058157
Gauss Quad 2x2 = 0.7961419553
Gauss Quad 3x3 = 0.7905556919
Gauss Quad 4x4 = 0.7879893663
Gauss Quad 5x5 = 0.7868151206
Gauss Quad 6x6 = 0.7862434463
Gauss Quad 7x7 = 0.7859405439
Gauss Quad 8x8 = 0.7857667677
Gauss Quad 9x9 = 0.7856601777
>> 7856601777*4
The Gaussian quadratures seem to converge. The surface area of the
× 0.7856601777
original surface is 4ans = = 3.1426407108.
The true surface area is π. The absolute error is 0.0010480572.
3.142640710800000e+10

>> 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.

This technique is frequently used in numerical methods for partial


differential equations, such as finite element method (FEM).

Xu Zhang (Oklahoma State University) MATH 4513 Numerical Analysis Fall 2020 116 / 116

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy