ES-341:
Numerical
Analysis
Dr. Mazhar Ali
Mehboob ul Haq
(TA)
Numerical
Integration
Trapezoidal Rule
Simpson’s Rule
ES-341: Numerical Analysis
Dr. Mazhar Ali
Mehboob ul Haq (TA)
Faculty of Engineering Sciences
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology
12th May 2022
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 1 / 17
ES-341:
Numerical
Analysis
Dr. Mazhar Ali
Mehboob ul Haq
(TA)
Numerical
Integration
Trapezoidal Rule
Simpson’s Rule
1 Numerical Integration
Trapezoidal Rule
Simpson’s Rule
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 2 / 17
ES-341:
Numerical
Analysis
Dr. Mazhar Ali
Mehboob ul Haq
(TA)
Numerical
Integration
Trapezoidal Rule
Simpson’s Rule
Numerical Integration
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 3 / 17
ES-341:
Numerical
Analysis
Dr. Mazhar Ali
Description
Mehboob ul Haq
(TA) The main goal of numerical integration is to evaluate definite
Numerical
integral of a function that has no explicit anti-derivativea .
Integration Rb
Trapezoidal Rule
The basic method involves in approximating a f (x)dx is called
Simpson’s Rule
“Numerical Quadrature”.
a It
refers to the operation that goes backward from the derivative of a
function to the function itself
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 4 / 17
ES-341:
Numerical
Analysis
Dr. Mazhar Ali
Description
Mehboob ul Haq
(TA) The main goal of numerical integration is to evaluate definite
Numerical
integral of a function that has no explicit anti-derivativea .
Integration Rb
Trapezoidal Rule
The basic method involves in approximating a f (x)dx is called
Simpson’s Rule
“Numerical Quadrature”.
a It
refers to the operation that goes backward from the derivative of a
function to the function itself
Key Idea
We can write a function f (x) as Lagrange integrating polynomial
with error terms as,
n n
X f (n+1) (ξ(x)) Y
f (x) = f (xi )Li (x) + (x − xi )
(n + 1)!
i=0 i=0
Applying integral on both sides.
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 4 / 17
ES-341:
Numerical
Analysis
Z b Z b n
Dr. Mazhar Ali X
Mehboob ul Haq
(TA)
f (x)dx = f (xi )Li (x)dx+
a a i=0
Numerical Z b n
Integration 1 Y
Trapezoidal Rule f (n+1) (ξ(x)) (x − xi )dx
Simpson’s Rule (n + 1)! a i=0
Rb
If we assume L (x)dx =
aR i
ai , then above expression can be used
b
to approximate a f (x)dx
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 5 / 17
ES-341:
Numerical
Analysis
Z b Z b n
Dr. Mazhar Ali X
Mehboob ul Haq
(TA)
f (x)dx = f (xi )Li (x)dx+
a a i=0
Numerical Z b n
Integration 1 Y
Trapezoidal Rule f (n+1) (ξ(x)) (x − xi )dx
Simpson’s Rule (n + 1)! a i=0
Rb
If we assume L (x)dx =
aR i
ai , then above expression can be used
b
to approximate a f (x)dx
Z b Z b n
X n
X
f (x)dx ≈ f (xi )Li (x)dx = f (xi )ai
a a i=0 i=0
With error term as,
Z b n
1 Y
E (f ) = f (n+1) (ξ(x)) (x − xi )dx
(n + 1)! a i=0
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 5 / 17
ES-341:
Numerical
Analysis Trapezoidal Rule
Dr. Mazhar Ali
Mehboob ul Haq Let us suppose we have two nodes such that,
(TA)
xi f (xi )
Numerical
Integration a = x0 f (x0 )
Trapezoidal Rule
Simpson’s Rule
b = x1 f (x1 )
with the following property, x1 − x0 = b − a = h. Here h is the
step size/step length.
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 6 / 17
ES-341:
Numerical
Analysis Trapezoidal Rule
Dr. Mazhar Ali
Mehboob ul Haq Let us suppose we have two nodes such that,
(TA)
xi f (xi )
Numerical
Integration a = x0 f (x0 )
Trapezoidal Rule
Simpson’s Rule
b = x1 f (x1 )
with the following property, x1 − x0 = b − a = h. Here h is the
step size/step length.
For the given set of nodes x0 and x1 , we can derive an expression
Rb
for approximating a f (x)dx and also error term from the general
expressions given below,
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 6 / 17
ES-341:
Numerical
Analysis Trapezoidal Rule
Dr. Mazhar Ali
Mehboob ul Haq Let us suppose we have two nodes such that,
(TA)
xi f (xi )
Numerical
Integration a = x0 f (x0 )
Trapezoidal Rule
Simpson’s Rule
b = x1 f (x1 )
with the following property, x1 − x0 = b − a = h. Here h is the
step size/step length.
For the given set of nodes x0 and x1 , we can derive an expression
Rb
for approximating a f (x)dx and also error term from the general
expressions given below,
Z b Z x1 1
X
f (x)dx ≈ f (xi )Li (x)dx
a x0 i=0
Z x1 1
1 Y
E (f ) = f (2) (ξ(x)) (x − xi )dx
(2)! x0 i=0
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 6 / 17
ES-341:
Numerical
Analysis
Dr. Mazhar Ali
Mehboob ul Haq Derivation: Trapezoidal Rule
(TA)
Z b Z x1 1
Numerical X
Integration f (x)dx ≈ f (xi )Li (x)dx
Trapezoidal Rule
Simpson’s Rule
a x0 i=0
Z x1 1
X Z x1
f (xi )Li (x)dx = f (x0 )L0 (x) + f (x1 )L1 (x) dx
x0 i=0 x0
x1
h (x − x ) (x − x0 )
Z i
1
= f (x0 ) + f (x1 ) dx
x0 (x0 − x1 ) (x1 − x0 )
2
x1
(x − x0 )2
(x − x1 )
= f (x0 ) + f (x1 )
2(x0 − x1 ) 2(x1 − x0 ) x0
2 2
x0 +h
(x − x0 − h) (x − x0 )
= f (x0 ) + f (x1 )
2(x0 − x0 − h) 2(x0 + h − x0 ) x0
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 7 / 17
ES-341:
Numerical
Analysis After simplification we get,
Dr. Mazhar Ali
Mehboob ul Haq Z b
(TA)
h
f (x)dx ≈ [f (x0 ) + f (x1 )]
Numerical a 2
Integration
Trapezoidal Rule
Simpson’s Rule
Calculating error term
Z x1 1
1 (2)
Y
E (f ) = f (ξ(x)) (x − xi )dx
(2)! x0
i=0
1 x1 ′′
Z
= f (ξ(x))(x − x0 )(x − x1 )
2 x0
We can apply weighted mean value theorem, such that
(x − x0 )(x − x1 ) do not change sign on [x0 , x1 ] and ξ(x) exists
between [x0 , x1 ], hence error term reduced to
f ′′ (ξ(x)) x1
Z
E (f ) = (x − x0 )(x − x1 )
2 x0
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 8 / 17
ES-341:
Numerical
Analysis
Dr. Mazhar Ali
After simplification we get,
Mehboob ul Haq
(TA)
−h3 ′′
Numerical E (f ) = f (ξ(x))
Integration 12
Trapezoidal Rule
Simpson’s Rule Rb
Finally, the Trapezoidal Rule approximates a
f (x)dx as
b
h3
Z
h
f (x)dx = [f (x0 ) + f (x1 )] − f ′′ (ξ(x))
a 2 12
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 9 / 17
ES-341:
Numerical
Analysis Simpson’s Rule
Dr. Mazhar Ali
Mehboob ul Haq Let us suppose we have two nodes such that,
(TA)
xi f (xi )
Numerical
Integration
a = x0 f (x0 )
Trapezoidal Rule x1 f (x1 )
Simpson’s Rule
b = x2 f (x2 )
with the following property, x2 − x0 = (b − a)/2 = h . Thus,
x1 = x0 + h. For the given set of nodes x0 , x1 and x2 , we can
Rb
derive an expression for approximating a f (x)dx and also error
term from the general expressions given below,
Z b Z x2 2
X
f (x)dx ≈ f (xi )Li (x)dx
a x0 i=0
Z x2 2
1 Y
E (f ) = f (3) (ξ(x)) (x − xi )dx
3! x0 i=0
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 10 / 17
ES-341:
Numerical
Analysis
Dr. Mazhar Ali
Mehboob ul Haq
(TA)
Rb
Numerical
Finally, the Simpson’s Rule approximates a
f (x)dx as
Integration
Trapezoidal Rule
b
h5
Z
Simpson’s Rule
h
f (x)dx = [f (x0 ) + 4f (x1 ) + f (x2 )] − f (4) (ξ)
a 3 90
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 11 / 17
ES-341:
Numerical
Analysis
Example 1
R2
Dr. Mazhar Ali
Mehboob ul Haq
Calculate 0
f (x)dx for
(TA)
Numerical f (x) = x 2 ; f (x) = x 4 ; f (x) = (x + 1)−1
Integration
Trapezoidal Rule p
Simpson’s Rule f (x) = 1 + x 2 ; f (x) = sin(x); f (x) = e x
R2
Calculating 0
x 2 dx using Trapezoidal Rule
Z 2
h
x 2 dx ≈ [f (x0 ) + f (x1 )]
0 2
First we calculate h. In case of trapezoidal rule
h = (b − a) = (2 − 0) = 2. Hence, x0 = 0 and x1 = 2.
x 3 2 2
≈ [(0)2 + (2)2 ]
3 0 2
2.666667 ≈ 4
We can see that there is a significant error.
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 12 / 17
ES-341:
Numerical
Analysis
Dr. Mazhar Ali
h3 ′′ 23
Mehboob ul Haq E (f ) = − f (ξ(x)) = − (2)
(TA) 12 12
Numerical
E (f ) ≈ 1.333334
Integration
Trapezoidal Rule
Simpson’s Rule R2
Calculating 0
x 2 dx using Simpson’s Rule
Z 2
h
x 2 dx ≈ [f (x0 ) + 4f (x1 ) + f (x2 )]
0 3
In case of Simpson’s rule h = (b − a)/2 = (2 − 0)/2 = 1. Hence,
x0 = 0, x1 = 1 and x2 = 2.
x 3 2 1
≈ [(0)2 + 4(1)2 + (2)2 ]
3 0 3
2.666667 ≈ 2.666667
h5 (4) 1
E (f ) = − f (ξ(x)) = − (0) = 0
90 90
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 13 / 17
ES-341:
Numerical
Analysis
Dr. Mazhar Ali
Mehboob ul Haq
R2
(TA) Calculating 0
sin(x)dx using Trapezoidal Rule
Z 2
Numerical
h
Integration
sin(x)dx ≈ [f (x0 ) + f (x1 )]
Trapezoidal Rule
Simpson’s Rule
0 2
First we calculate h. In case of trapezoidal rule
h = (b − a) = (2 − 0) = 2. Hence, x0 = 0 and x1 = 2.
2 2
− cos(x) ≈ [sin(0) + sin(2)]
0 2
1.41614 ≈ 0.90930
We can observe a relative large error in this case.
h3 ′′ 23 2
E (f ) = − f (ξ(x)) = − sin(ξ(x)) = − sin(ξ(x))
12 12 3
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 14 / 17
ES-341:
Numerical
Analysis
Dr. Mazhar Ali
Mehboob ul Haq
(TA) R2
Calculating 0
sin(x)dx using Simpson’s Rule
Numerical
Integration Z 2
Trapezoidal Rule h
Simpson’s Rule sin(x)dx ≈ [f (x0 ) + 4f (x1 ) + f (x2 )]
0 3
In case of Simpson’s rule h = (b − a)/2 = (2 − 0)/2 = 1. Hence,
x0 = 0, x1 = 1 and x2 = 2.
2 2
− cos(x) ≈ [sin(0) + 4 sin(1) + sin(2)]
0 3
1.41614 ≈ 1.425060
h5 (4) 1
E (f ) = − f (ξ(x)) = − sin(ξ(x))
90 90
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 15 / 17
ES-341:
Numerical
Analysis
Dr. Mazhar Ali
Mehboob ul Haq
(TA)
Numerical
Integration
Trapezoidal Rule
Simpson’s Rule
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 16 / 17
ES-341:
Numerical
Analysis
Trapezoidal Rule, n = 1
Dr. Mazhar Ali
Z x1
Mehboob ul Haq
h h3
(TA)
f (x)dx = [f (x0 ) + f (x1 )] − f ′′ (ξ)
x0 2 12
Numerical
Integration
Trapezoidal Rule h = (x1 − x0 ), x0 ≤ ξ ≤ x1
Simpson’s Rule
Simpson’s Rule, n = 2
Z x2
h h5
f (x)dx = [f (x0 ) + 4f (x1 ) + f (x2 )] − f (4) (ξ)
x0 3 90
h = (x2 − x0 )/2, x0 ≤ ξ ≤ x2
Simpson’s Three-Eight Rule, n = 3
x3
3h5 (4)
Z
3h
f (x)dx = [f (x0 ) + 3f (x1 ) + 3f (x2 ) + f (x3 )] − f (ξ)
x0 8 80
h = (x3 − x0 )/3, x0 ≤ ξ ≤ x3
Dr. Mazhar Ali Mehboob ul Haq (TA) (Ghulam Ishaq Khan Institute
ES-341:
of Engineering
NumericalSciences
Analysis
and Technology) 12th May 2022 17 / 17