4.3 - Elements of Numerical Integration
4.3 - Elements of Numerical Integration
Elements of Numerical
Integration
Sec:4.3 Elements of Numerical Integration
Remark
Numerical integration is useful for approximating a definite integral of a function that has
no explicit antiderivative or whose antiderivative is not easy to obtain.
The basic method for approximating is called numerical quadrature. It uses a sum to
approximate .
Sec:4.3 Elements of Numerical Integration
Derivation
The derivation of the quadrature methods (Trapezoidal and Simpson rules) will be
based on the Lagrange interpolating polynomials discussed in Chapter 3.
𝑏 𝑏
∫ 𝑓 ( 𝑥 ) 𝑔 ( 𝑥 ) 𝑑𝑥= 𝒇 ( 𝒄)∫ 𝑔 ( 𝑥 ) 𝑑𝑥
𝑎 𝑎
Sec:4.3 Elements of Numerical IntegrationDerivation for The Trapezoidal Rul
𝑓 ( 𝑥 ) =𝑷 𝟏 ( 𝒙 ) + 𝑹𝟏 ( 𝒙 )
′′
𝒇 (𝝃 )
𝑓 ( 𝑥 ) =𝒇 ( 𝒙 𝟎 ) 𝑳 𝟎( 𝒙 )+ 𝒇 ( 𝒙 𝟏 ) 𝑳 𝟏( 𝒙 )+ ( 𝒙 − 𝒙 𝟎)( 𝒙 − 𝒙 𝟏 )
𝟐!
𝒙 − 𝒙𝟏 𝒙 − 𝒙𝟎 𝒇 ′ ′ ( 𝝃 )
𝑓 ( 𝑥 ) =𝒇 ( 𝒙 𝟎 ) + 𝒇 ( 𝒙 𝟏) + ( 𝒙 − 𝒙 𝟎)( 𝒙 − 𝒙 𝟏)
𝒙𝟎 − 𝒙 𝟏 𝒙𝟏− 𝒙𝟎 𝟐!
𝑏 𝒙𝟏 𝒙𝟏 𝒙𝟏 ′′
𝒇 (𝝃 )
∫ 𝑓 ( 𝑥 ) 𝑑𝑥= 𝒇 ( 𝒙 𝟎∫ 𝟎
) 𝑳 ( 𝒙 ) 𝒅𝒙 + 𝒇 ( 𝒙 𝟏 ∫ 𝟏
) 𝑳 ( 𝒙 ) 𝒅𝒙 +¿ ∫ 𝟐! (𝒙 − 𝒙 𝟎)(𝒙 − 𝒙𝟏 ) dx ¿
𝑎 𝒙 𝟎 𝒙 𝟎 𝒙 𝟎
𝑏 𝒙𝟏 ′′
1 𝒇 (𝝃)
∫ 𝑓 ( 𝑥 ) 𝑑𝑥=
2
h [ 𝒇 ( 𝒙 𝟎) + 𝒇 ( 𝒙 𝟏 )] ∫
+
𝟐!
(𝒙 − 𝒙𝟎 )(𝒙 − 𝒙𝟏 ) dx
𝑎 𝒙 𝟎
[ ]
𝒙𝟏 𝒙𝟏 𝒙𝟏 𝟐 𝒙𝟏
𝒙 𝟎
𝒙
𝒙−𝒙
𝟎𝟎
𝟏
𝟏
∫ 𝑳𝟎 ( 𝒙 ) 𝒅𝒙=∫ 𝒙 − 𝒙𝟏 𝒅𝒙= −𝒉
𝒙 𝟎
( 𝒙−𝒙 )
∫ ( 𝒙 − 𝒙 𝟏) 𝒅𝒙=− 𝒉𝟏 𝟐 𝟏
𝒙𝟎
( )[
=−
𝟏
𝒉
𝟏 𝟐
𝟐
𝟏
𝟐]
𝟎− 𝒉 =¿ 𝒉 ¿
[ ] ( )[
𝒙𝟏 𝒙𝟏 𝒙𝟏 𝟐 𝒙𝟏
∫ 𝟏
𝒙𝟎
𝑳 ( 𝒙 ) 𝒅𝒙 =
𝒙
𝒙 − 𝒙𝟎
∫ 𝒙 −𝒙
𝟏
𝟎
𝟎
𝒅𝒙=
𝟏
𝒉𝒙
∫ ( 𝟎) 𝒉 𝟐
𝒙
𝟎
− 𝒙 𝒅𝒙 =
𝟏 ( 𝒙 − 𝒙𝟎 )
𝒙𝟎
=
𝟏 𝟏 𝟐
𝒉 𝟐
𝟏
𝒉 − 𝟎 =¿ 𝒉 ¿
𝟐 ]
Sec:4.3 Elements of Numerical Integration Error for The Trapezoidal Rule
The Weighted Mean Value Theorem for Does not change its sign
[ ]
Integrals ′′ 𝒙𝟏
𝒇 ′′ ( 𝝃 )
𝒇 (𝒄) 𝒉
𝟑
𝒉
𝟑
𝟐 ∫
¿ (𝒙 − 𝒙 𝟎 )(𝒙 − 𝒙 𝟏) dx ¿ − =− 𝒇 ′ ′ ( 𝒄 ) (𝟐)
𝟐 𝟔 𝟏𝟐
𝒙
Substituting (2) into (1), we get 𝟎
𝑏 𝟑
1 𝒉 ′′
∫ 𝑓 ( 𝑥 ) 𝑑𝑥=
2
h [ ( 𝟎 ) ( 𝟏 ) ] 𝟏𝟐 𝒇 ( 𝒄 )
𝒇 𝒙 + 𝒇 𝒙 −
𝑎
Remark
The error term of the Trapezoidal rule involves , so if the Trapezoidal rule applied to any
function whose second derivative is zero, the error term will vanish. Therefore, the
Trapezoidal rule gives exact result for all polynomials of degree one or less.
Sec:4.3 Elements of Numerical Integration
Example
The Trapezoidal Rule
Approximate the integral
using the Trapezoidal 𝑏 𝒉=𝒃 − 𝒂
rule 2
∫ 𝑓 ( 𝑥 ) 𝑑𝑥 ≈ 𝒉
𝟐
[ 𝒇 ( 𝒙 𝟎 ) + 𝒇 ( 𝒙 𝟏) ]
𝑎
∫ 𝑥 𝑑𝑥
2
2
𝑓 ( 𝑥 ) =𝑥 , h= 2 , 𝑎=0 , 𝑏=2
∫ 𝑥 2 𝑑𝑥 ≈ 𝟏𝟐 𝒉 [ 𝒇 ( 𝒙 𝟎 ) + 𝒇 ( 𝒙 𝟏) ]
0
𝟏
𝟐 [
≈ 𝒉 𝒇 ( 𝒙 𝟎) + 𝒇 ( 𝒙𝟏 ) ]
𝟏
≈ ( 𝟐) [ 𝟎+ 𝟒 ]
𝟐
≈𝟒
Sec:4.3 Elements of Numerical Integration
Sec:4.3 Elements of Numerical Integration Simpson’s Rule Derivation
𝒙𝟐 𝒙𝟐 𝒙𝟐 𝒙𝟐 ′ ′′
𝑏
𝒇 ( 𝝃𝟏)
∫ 𝑓 𝑥 𝑑𝑥= 𝒇 ( 𝒙 𝟎 )∫ 𝑳𝟎 𝒙 𝒅𝒙 + 𝒇 ( 𝒙𝟏 ) ∫ 𝑳𝟏 𝒙 𝒅 𝑥+ 𝒇 ( 𝒙 𝟐 )∫ 𝑳𝟐 𝒙 𝒅𝒙+¿ ∫ 𝟑! (𝒙 − 𝒙 𝟎)(𝒙 − 𝒙 𝟏)( 𝒙 − 𝒙 𝟐)dx ¿
( ) ( ) ( ) ( )
𝑎 𝒙 𝟎 𝒙 𝟎 𝒙 𝟎 𝒙 𝟎
𝒙𝟐 ′′ ′
𝒇 ( 𝝃𝟏)
¿ 𝒂𝟎 𝒇 ( 𝒙𝟎 ) +𝒂 𝟏 𝒇 ( 𝒙𝟏 ) +𝒂 𝟐 𝒇 ( 𝒙𝟐 ) +∫ (𝒙 − 𝒙 𝟎)(𝒙 − 𝒙 𝟏)(𝒙 − 𝒙𝟐 )dx (3)
𝟎 𝒙
𝟑!
𝒙𝟐 𝒙𝟐 𝒙𝟐
𝒉 𝟒𝒉 𝒉
𝒂𝟎=∫ 𝑳 𝟎 𝒙 𝒅𝒙=¿ ¿
( ) 𝒂𝟏=∫ 𝑳𝟏 𝒙 𝒅𝒙=¿
( ) ¿ 𝒂𝟐=∫ 𝑳𝟐 ( 𝒙 ) 𝒅𝒙=¿ ( 𝟒) ¿
𝒙 𝟎
𝟑 𝒙 𝟎
𝟑 𝒙𝟎
𝟑
Substituting (4) into 3, we get
𝒙𝟐 ′′ ′
𝑏
𝒉 𝒇 ( 𝝃 𝟏)
∫ 𝑓 ( 𝑥 ) 𝑑𝑥= 𝟑 [ 𝒇 ( 𝒙 𝟎 ) +𝟒 𝒇 ( 𝒙 𝟏) + 𝒇 ( 𝒙𝟐 ) ] +∫ 𝟑 ! ( 𝒙 − 𝒙 𝟎)( 𝒙 − 𝒙 𝟏)(𝒙 − 𝒙 𝟐)dx
𝑎 𝒙
𝟎
Sec:4.3 Elements of Numerical Integration Simpson’s Rule Derivation
𝒙𝟐 ′′ ′
𝑏
𝒉 𝒇 ( 𝝃 𝟏)
∫ 𝑓 ( 𝑥 ) 𝑑𝑥= 𝟑 [ 𝒇 ( 𝒙 𝟎 ) +𝟒 𝒇 ( 𝒙 𝟏) + 𝒇 ( 𝒙𝟐 ) ] +∫ 𝟑 ! ( 𝒙 − 𝒙 𝟎)( 𝒙 − 𝒙 𝟏)(𝒙 − 𝒙 𝟐)dx (𝟓)
𝑎 𝟎 𝒙
It can be shown that the error term in the above formula is as follows:
𝒙𝟐 ′ ′′
𝒇 ( 𝝃𝟏) 𝒉𝟓 ( 𝟒)
∫ 𝟑!
( 𝒙 − 𝒙 𝟎 ) ( 𝒙 − 𝒙𝟏 ) ( 𝒙 − 𝒙 𝟐) dx =−
𝟗𝟎
𝒇 ( 𝝃 ∗ ) , 𝝃 ∗ ∈ ( 𝒙 𝟎 , 𝒙 𝟐 ) ( 𝟔)
𝒙𝟎
Dropping the error term from the above formula, we get the Simpson rule as follows:
Simpson Rule
𝒃− 𝒂
𝒉 𝒉=
𝑓 ( 𝑥 ) 𝑑𝑥 ≈
𝟑
[ 𝒇 ( 𝒙 𝟎 ) +𝟒 𝒇 ( 𝒙 𝟏 ) + 𝒇 ( 𝒙 𝟐 ) ]𝟐
Sec:4.3 Elements of Numerical Integration Simpson’s Rule Derivation
Simpson Rule
𝒃− 𝒂
𝒉=
𝒉 𝟐
𝑥 ) 𝑑𝑥 ≈
𝟑
[ 𝒇 ( 𝒙 𝟎 ) +𝟒 𝒇 ( 𝒙 𝟏 ) + 𝒇 ( 𝒙 𝟐 ) ]
Remark
The error term of Simpson rule involves , so if Simpson rule applied to any function whose
fourth derivative is zero, the error term will vanish. Therefore, Simpson rule gives exact
result for all polynomials of degree three or less.
Remark
The Trapezoidal and Simpson rules are subclasses of methods know as Newton-Cotes
Methods.
Sec:4.3 Elements of Numerical Integration
∫ 𝑥 2 𝑑𝑥
0
2
𝑓 ( 𝑥 ) =𝑥 , h= 1 , 𝑥 0= 0 , 𝑥 1=1 , 𝑥 2 =2
2
𝒉
∫ 𝑥
2
𝑑𝑥 ≈
𝟑
[ 𝒇 ( 𝒙 𝟎 ) +𝟒 𝒇 ( 𝒙 𝟏) + 𝒇 ( 𝒙 𝟐 ) ]
0
𝟏
≈
𝟑
[ 𝒇 ( 𝟎 ) +𝟒 𝒇 ( 𝟏 ) + 𝒇 ( 𝟐 ) ]
𝑥 0=0 𝑥1 =1 𝑥 2=2
𝟏
≈ [ 𝟎+ 𝟒+𝟒 ]
𝟑
𝟖
≈ It is exact
𝟑
Sec:4.3 Elements of Numerical Integration
h=𝑏 − 𝑎
Simpson’s Rule
𝑏 𝟓
𝒉 𝒉 ( 𝟒)
∫ 𝑓 ( 𝑥 ) 𝑑𝑥=
𝟑
[ 𝒇 ( 𝒙 𝟎) +𝟒 𝒇 ( 𝒙 𝟏) + 𝒇 (𝒙 𝟐 ]
) −
𝟗𝟎
𝒇 ( 𝝃∗)
𝑎
h=(𝑏 −𝑎 )/ 2
1) How to use
2) MATLAB
3) Derivation
Sec:4.3 Elements of Numerical Integration
Simpson’s Rule
f = @(x) x^2;
𝑏 𝟓 a=0; b=2;
𝒉 𝒉 ( 𝟒)
∫ 𝑓 ( 𝑥 ) 𝑑𝑥=
𝟑
[ ( 𝟎 ) ( 𝟏)
𝒇 𝒙 +𝟒 𝒇 𝒙 + 𝒇 (𝒙 𝟐 ]
) −
𝟗𝟎
𝒇 ( 𝝃∗) h=(b-a)/2;
𝑎 x=a:h:b;
intg = (h/3)*(f(x(1))+4*f(x(2))+f(x(3)))
h=(𝑏 −𝑎 )/ 2