15 Numerical Integration
15 Numerical Integration
Numerical Integration
1
Numerical Analysis 0905201
Chapter 21
𝑆𝑡𝑒𝑝 𝑠𝑖𝑧𝑒 ℎ = 𝑏 − 𝑎
1- Trapezoidal Rule
- True error 𝐸𝑡 = 𝑇𝑟𝑢𝑒 𝑣𝑎𝑙𝑢𝑒 𝐼𝑡 − 𝐴𝑝𝑝𝑟𝑜𝑥𝑖𝑚𝑎𝑡𝑒 𝑣𝑎𝑙𝑢𝑒 𝐼
𝐼𝑡 −𝐼
- Relative true error 𝜀𝑡 = . 100 %
𝐼𝑡
𝑓 𝑎 +𝑓 𝑏
𝐼 ≅ 𝑏−𝑎
2
𝑓 0 + 𝑓 0.5
𝐼 ≅ 0.5 − 0
2
1 + 1.6487
𝐼 ≅ 0.5 − 0
2
𝐼 ≅ 0.6621
Solution
𝐼 ≅ 0.6621 , 𝐼𝑡 = 0.6487
𝐼𝑡 −𝐼 0.6487 −0.6621
Relative true error 𝜀𝑡 = . 100 % = . 100 %
𝐼𝑡 0.6487
𝜀𝑡 = 2.06 %
1
The local truncation error 𝐸𝑎 = − 𝑏 − 𝑎 3 𝑓′′
12
1 3
𝐸𝑎 = − 0.5 − 0 1.2974 = −0.0135
12
2- Simpson’s 1/3 Rule
This rule is derived from a 2nd order Lagrange interpolating polynomial.
Basically, this rule uses a parabola of 3 points to approximate the integrand.
𝐼 = න 𝑓 𝑥 𝑑𝑥
𝑎
ℎ
𝐼≅ 𝑓0 + 4𝑓1 + 𝑓2
3
𝑏−𝑎
Where the step size ℎ =
2
2- Simpson’s 1/3 Rule
- True error 𝐸𝑡 = 𝑇𝑟𝑢𝑒 𝑣𝑎𝑙𝑢𝑒 𝐼𝑡 − 𝐴𝑝𝑝𝑟𝑜𝑥𝑖𝑚𝑎𝑡𝑒 𝑣𝑎𝑙𝑢𝑒 𝐼
𝐼𝑡 −𝐼
- Relative true error 𝜀𝑡 = . 100 %
𝐼𝑡
𝑏 − 𝑎 0.5 − 0
ℎ= = = 0.25
2 2
𝑥0 = 0 ⇒ 𝑓0 = 𝑓 0 = 1
𝑥1 = 0.25 ⇒ 𝑓1 = 𝑓 0.25 = 1.2840
𝑥2 = 0.5 ⇒ 𝑓2 = 𝑓 0.5 = 1.6487
Solution
ℎ = 0.25
𝑓0 = 1
𝑓1 = 1.2840
𝑓2 = 1.6487
ℎ
𝐼≅ 𝑓0 + 4𝑓1 + 𝑓2
3
0.25
𝐼≅ 1 + 4(1.2840) + 1.6487
3
𝐼 ≅ 0.6487
Solution
𝐼 ≅ 0.6487 , 𝐼𝑡 = 0.6487
𝐼𝑡 −𝐼 0.6487 −0.6487
Relative true error 𝜀𝑡 = . 100 % = . 100 %
𝐼𝑡 0.6487
𝜀𝑡 = 0 %
Which means that the Simpson’s rule is more accurate than the trapezoidal rule.
3- Simpson’s 3/8 Rule
This rule is derived from a 3rd order Lagrange interpolating polynomial.
Basically, this rule uses a cubic of 4 points to approximate the integrand.
𝐼 = න 𝑓 𝑥 𝑑𝑥
𝑎
3ℎ
𝐼≅ 𝑓0 + 3𝑓1 + 3𝑓2 + 𝑓3
8
𝑏−𝑎
Where the step size ℎ =
3
Example
Consider the following table of data points:
X 0 2 4 6 8 10 12
Y 0 1.9 2 2 2.4 2.6 2.8
12 3
Find the value of 0 𝑓(𝑥) 𝑑𝑥 2.5
2
Using the trapezoidal, Simpson’s 1.5
1
1/3 and 3/8 rules.
0.5
0
0 2 4 6 8 10 12 14
X 0 2 4 6 8 10 12
Solution Y 0 1.9 2 2 2.4 2.6 2.8
𝑓 𝑎 +𝑓 𝑏 𝑓 0 +𝑓 12
1- Trapezoidal Rule : 𝐼 ≅ 𝑏 − 𝑎 = 12 − 0 = 16.8
2 2
ℎ
2- Simpson’s 1/3 rule: 𝐼 ≅ 𝑓0 + 4𝑓1 + 𝑓2
3
𝑏 − 𝑎 12 − 0
ℎ= = =6 𝑥0 = 0 , 𝑥1 = 6 , 𝑥2 = 12
2 2
6
𝐼≅ 𝑓 0 + 4𝑓 6 + 𝑓 12 = 21.6
3
3ℎ
3- Simpson’s 3/8 rule: 𝐼 ≅ 𝑓0 + 3𝑓1 + 3𝑓2 + 𝑓3
8
𝑏−𝑎 12−0
ℎ= = =4 𝑥0 = 0 , 𝑥1 = 4 , 𝑥2 = 8 , 𝑥3 = 12
3 3
3∗4
𝐼≅ 𝑓(0) + 3𝑓(4) + 3𝑓(8) + 𝑓(12 = 24
8
Romberg Integration
- It uses the trapezoidal rule, but much more efficient results are obtained
through iterative refinement techniques.
- There may be cases where the spacing between data points may not be even
(e.g., experimental data points).
- The best technique to use in this case is to use Simpson’s 1/3 rule wherever
two consecutive equal-sized segments are encountered, and use 3/8 rule
wherever three consecutive equal-sized segments are encountered. And when
adjacent segments are unequal-sized , just use trapezoidal rule. Finally, the
total value of the approximate integration is the sum of all these integrations.
Integration With Unequal Segments
Example
Consider the following table of data points:
X 0 0.12 0.22 0.32 0.33 0.34 0.35 0.40
Y 0 1.9 2 2 2.4 2.6 2.8 3.0
𝐼 ≅ 𝐼1 + 𝐼2 + 𝐼3 + 𝐼4
𝑓 𝑎 +𝑓 𝑏 𝑓 0 + 𝑓 0.12
𝐼1 ≅ 𝑏 − 𝑎 = 0.12 − 0 = 0.114
2 2
ℎ 0.1
𝐼2 ≅ 𝑓0 + 4𝑓1 + 𝑓2 = 𝑓 0.12 + 4𝑓 0.22 + 𝑓 0.32 = 0.396
3 3
Solution
X 0 0.12 0.22 0.32 0.33 0.34 0.35 0.40
Y 0 1.9 2 2 2.4 2.6 2.8 3.0
𝐼 ≅ 𝐼1 + 𝐼2 + 𝐼3 + 𝐼4
𝑓 𝑎 +𝑓 𝑏 𝑓 0.35 + 𝑓 0.40
𝐼4 ≅ 𝑏 − 𝑎 = 0.40 − 0.35 = 0.145
2 2
Solution
X 0 0.12 0.22 0.32 0.33 0.34 0.35 0.40
Y 0 1.9 2 2 2.4 2.6 2.8 3.0
𝐼 ≅ 𝐼1 + 𝐼2 + 𝐼3 + 𝐼4
𝐼 ≅ 0.114 + 0.396 + 0.074 + 0.145
𝐼 ≅ 0.729
Composite Trapezoidal Rule
To make the trapezoidal rule more effective and accurate, we divide the area
under the curve into multiple “segments” instead of only one.
𝒙𝟎 𝒙𝟏 𝒙𝟐 𝒙𝟑 𝒙𝟒
Composite Trapezoidal Rule
𝑥4
𝐼 = න 𝑓 𝑥 𝑑𝑥
𝑥0
𝑥1 𝑥2
𝐼 ≅ න 𝑓 𝑥 𝑑𝑥 + න 𝑓 𝑥 𝑑𝑥
𝑥0 𝑥1
𝒙𝟎 𝒙𝟏 𝒙𝟐 𝒙𝟑 𝒙𝟒
𝑥3 𝑥4
+ න 𝑓 𝑥 𝑑𝑥 + න 𝑓 𝑥 𝑑𝑥
𝑥2 𝑥3
𝑓 𝑎 +𝑓 𝑏
Trapezoidal Rule: 𝐼 ≅ 𝑏 − 𝑎
2
𝑓 𝑎 +𝑓 𝑏
Composite Trapezoidal Rule Trapezoidal Rule: 𝐼 ≅ 𝑏 − 𝑎
2
𝑥1 𝑥2 𝑥3 𝑥4
𝐼 ≅ න 𝑓 𝑥 𝑑𝑥 + න 𝑓 𝑥 𝑑𝑥 + න 𝑓 𝑥 𝑑𝑥 + න 𝑓 𝑥 𝑑𝑥
𝑥0 𝑥1 𝑥2 𝑥3
𝑓 𝑥0 + 𝑓 𝑥1 𝑓 𝑥1 + 𝑓 𝑥2
≅ 𝑥1 − 𝑥0 + 𝑥2 − 𝑥1
2 2
𝑓 𝑥2 + 𝑓 𝑥3 𝑓 𝑥3 + 𝑓(𝑥4 )
+ 𝑥3 − 𝑥2 + 𝑥4 − 𝑥3
2 2
𝑓 𝑎 +𝑓 𝑏
Composite Trapezoidal Rule Trapezoidal Rule: 𝐼 ≅ 𝑏 − 𝑎
2
𝑓 𝑥0 + 𝑓 𝑥1 𝑓 𝑥1 + 𝑓 𝑥2 𝑓 𝑥2 + 𝑓 𝑥3
≅ ℎ + ℎ + ℎ
2 2 2
𝑓 𝑥3 + 𝑓(𝑥4 )
+ ℎ
2
ℎ
≅ 𝑓 𝑥0 + 𝑓 𝑥1 + 𝑓 𝑥1 + 𝑓 𝑥2 + 𝑓 𝑥2 + 𝑓 𝑥3
2
+ 𝑓 𝑥3 + 𝑓(𝑥4 )
Composite Trapezoidal Rule
𝑏 𝑛−1
ℎ
න 𝑓 𝑥 𝑑𝑥 ≅ 𝑓 𝑎 +2 𝑓(𝑥𝑖 ) + 𝑓(𝑏)
𝑎 2
𝑖=1
𝑏−𝑎
Where the step size ℎ =
𝑛
2
1
න dx
1 𝑥
Given that 𝐼𝑡 = 0.6931, calculate the true error and the relative true
error.
1
Solution
𝑥
n= 4 segments.
𝑏−𝑎 2−1
ℎ= = = 0.25
𝑛 4
𝑥0 = 1 𝑓 𝑥0 = 𝑓 1 = 1
𝑥1 = 1.25 𝑓 𝑥1 = 𝑓 1.25 = 0.8
𝑥2 = 1.5 𝑓 𝑥2 = 𝑓 1.5 = 0.6
𝑥3 = 1.75 𝑓 𝑥3 = 𝑓 1.75 = 0.5714
𝑥4 = 2 𝑓 𝑥4 = 𝑓 2 = 0.5
Solution
𝑏 𝑛−1
ℎ
න 𝑓 𝑥 𝑑𝑥 ≅ 𝑓 𝑎 +2 𝑓(𝑥𝑖 ) + 𝑓(𝑏)
𝑎 2
𝑖=1
ℎ
≅ 𝑓 𝑥0 + 2 𝑓 𝑥1 + 𝑓 𝑥2 + 𝑓 𝑥3 + 𝑓 𝑥4
2
ℎ
≅ 𝑓 1 + 2 𝑓 1.25 + 𝑓 1.5 + 𝑓 1.75 +𝑓 2
2
0.25
≅ 1 + 2 0.8 + 0.6 + 0.5714 + 0.5 = 0.6803
2
Solution
𝐼 ≅ 0.6803 , 𝐼𝑡 = 0.6931
𝐼𝑡 −𝐼 0.6931 −0.6803
Relative true error 𝜀𝑡 = . 100 % = . 100 %
𝐼𝑡 0.6931
𝜀𝑡 = 1.84 %
Example
Approximate the area beneath 𝑦 = 𝑥 2 + 1 on the interval [0,2] using
the trapezoidal rule with n = 5 segments
Given that 𝐼𝑡 = 4.666 , calculate the true error and the relative true
error.
𝑦 = 𝑥2 + 1
Solution
n = 5 segments.
𝑏−𝑎 2−0
ℎ= = = 0.4
𝑛 5
𝑥0 = 0 𝑓 𝑥0 = 𝑓 0 = 1
𝑥1 = 0.4 𝑓 𝑥1 = 𝑓 0.4 = 1.16
𝑥2 = 0.8 𝑓 𝑥2 = 𝑓 0.8 = 1.64
𝑥3 = 1.2 𝑓 𝑥3 = 𝑓 1.2 = 2.44
𝑥4 = 1.6 𝑓 𝑥4 = 𝑓 1.6 = 3.56
𝑥5 = 2 𝑓 𝑥4 = 𝑓 2 = 5
Solution
𝑏 𝑛−1
ℎ
න 𝑓 𝑥 𝑑𝑥 ≅ 𝑓 𝑎 +2 𝑓(𝑥𝑖 ) + 𝑓(𝑏)
𝑎 2
𝑖=1
ℎ
≅ 𝑓 𝑥0 + 2 𝑓 𝑥1 + 𝑓 𝑥2 + 𝑓 𝑥3 + +𝑓 𝑥4 + 𝑓 𝑥5
2
ℎ
≅ 𝑓 0 + 2 𝑓 0.4 + 𝑓 0.8 + 𝑓 1.2 + 𝑓 1.6 +𝑓 2
2
0.4
≅ 1 + 2 1.16 + 1.64 + 2.44 + 3.56 + 5 = 4.72
2
Solution
𝐼 ≅ 4.72 , 𝐼𝑡 = 4.6666
𝐼𝑡 −𝐼 4.666 −4.72
Relative true error 𝜀𝑡 = . 100 % = . 100 %
𝐼𝑡 4.6666
𝜀𝑡 = 1.15 %
Composite Simpson’s Rule
To make Simpson’s 1/3 rule more effective and accurate, we divide the area
under the curve into multiple “segments” instead of only one.
𝒙𝟎 𝒙𝟏 𝒙𝟐 𝒙𝟑 𝒙𝟒
Composite Simpson’s Rule
𝑥4
𝐼 = න 𝑓 𝑥 𝑑𝑥
𝑥0
𝑥2 𝑥4
𝐼 ≅ න 𝑓 𝑥 𝑑𝑥 + න 𝑓 𝑥 𝑑𝑥
𝑥0 𝑥2
𝒙𝟎 𝒙𝟏 𝒙𝟐 𝒙𝟑 𝒙𝟒
ℎ 𝑏−𝑎
Simpson’s 1/3 Rule 𝐼 ≅ 𝑓0 + 4𝑓1 + 𝑓2 , ℎ=
3 2
ℎ
Simpson’s 1/3 Rule 𝐼 ≅ 𝑓0 + 4𝑓1 + 𝑓2
Composite Simpson’s Rule 3
𝑏−𝑎
ℎ=
2
𝑥2 𝑥4
𝐼 ≅ න 𝑓 𝑥 𝑑𝑥 + න 𝑓 𝑥 𝑑𝑥
𝑥0 𝑥2
𝑥2 − 𝑥0 𝑓 𝑥0 + 4𝑓 𝑥1 + 𝑓(𝑥2 )
≅ .
2 3
𝑥4 − 𝑥2 𝑓 𝑥2 + 4𝑓 𝑥3 + 𝑓(𝑥4 )
+ .
2 3
𝑓 𝑎 +𝑓 𝑏
Composite Simpson’s Rule Trapezoidal Rule: 𝐼 ≅ 𝑏 − 𝑎
2
𝑓 𝑥0 + 4𝑓 𝑥1 + 𝑓(𝑥2 ) 𝑓 𝑥2 + 4𝑓 𝑥3 + 𝑓(𝑥4 )
𝐼 ≅ ℎ. + ℎ.
3 3
ℎ
≅ 𝑓 𝑥0 + 4 𝑓 𝑥1 + 𝑓 𝑥3 + 2 𝑓 𝑥2 + 𝑓 𝑥4
3
Composite Simpson’s Rule
𝑏 𝑛−1 𝑛−2
ℎ
න 𝑓 𝑥 𝑑𝑥 ≅ 𝑓 𝑎 +4 𝑓(𝑥𝑖 ) + 2 𝑓(𝑥𝑖 ) + 𝑓(𝑏)
𝑎 3
𝑖=1 𝑖=2
𝑖=𝑜𝑑𝑑 𝑖=𝑒𝑣𝑒𝑛
𝑏−𝑎
Where the step size ℎ =
𝑛
Given that 𝐼𝑡 = 12 , calculate the true error and the relative true error.
𝑦 = 𝑥2 + 1
Solution
n= 6 segments.
𝑏−𝑎 3−0
ℎ= = = 0.5
𝑛 6
𝑥0 = 0 𝑓 𝑥0 = 𝑓 0 = 1
𝑥1 = 0.5 𝑓 𝑥1 = 𝑓 0.5 = 1.25
𝑥2 = 1 𝑓 𝑥2 = 𝑓 1 = 2
𝑥3 = 1.5 𝑓 𝑥3 = 𝑓 1.5 = 3.25
𝑥4 = 2 𝑓 𝑥4 = 𝑓 2 = 5
𝑥5 = 2.5 𝑓 𝑥5 = 𝑓 2.5 = 7.25
𝑥6 = 3 𝑓 𝑥6 = 𝑓 3 = 10
Solution
𝑛−1 𝑛−2
ℎ
𝐼≅ 𝑓 𝑎 +4 𝑓(𝑥𝑖 ) + 2 𝑓(𝑥𝑖 ) + 𝑓(𝑏)
3
𝑖=1 𝑖=2
𝑖=𝑜𝑑𝑑 𝑖=𝑒𝑣𝑒𝑛
ℎ
≅ 𝑓 𝑥0 + 4 𝑓 𝑥1 + 𝑓 𝑥3 + 𝑓 𝑥5 + 2 𝑓 𝑥2 + 𝑓 𝑥4 + 𝑓 𝑥6
3
0.5
≅ 𝑓 0 + 4 𝑓 0.5 + 𝑓 1.5 + 𝑓 2.5 +2 𝑓 1 +𝑓 2 +𝑓 3
3
𝐼 ≅ 12
Solution
𝐼 ≅ 12 , 𝐼𝑡 = 12
True error 𝐸𝑡 = 𝐼𝑡 − 𝐼 = 12 − 12 = 0
𝐼𝑡 −𝐼 12 −12
Relative true error 𝜀𝑡 = . 100 % = . 100 %
𝐼𝑡 12
𝜀𝑡 = 0 %
Example 8 (solve by yourself)
Solve the previous example using composite trapezoidal rule using n=6
segments for the same interval [0,3].
Answer: 𝐼 ≅ 12.125
Example
Consider the following table of data points:
X 0 2 4 6 8
Y 0 1.9 2 2 2.4
8
Find the value of 0 𝑓(𝑥) 𝑑𝑥 Using the composite trapezoidal and
composite Simpson’s rules, using n=6 segments
Solution X 0 2 4 6 8
Y 0 1.9 2 2 2.4
𝑏−𝑎 8−0
step size ℎ = = =2
𝑛 4
𝑏 𝑛−1
ℎ
න 𝑓 𝑥 𝑑𝑥 ≅ 𝑓 𝑎 +2 𝑓(𝑥𝑖 ) + 𝑓(𝑏)
𝑎 2
𝑖=1
ℎ
≅ 𝑓 𝑥0 + 2 𝑓 𝑥1 + 𝑓 𝑥2 + 𝑓 𝑥3 + 𝑓 𝑥4
2
ℎ
≅ 𝑓 0 +2 𝑓 2 +𝑓 4 +𝑓 6 +𝑓 8
2
2
≅ 0 + 2 1.9 + 2 + 2 + 2.4 = 14.2
2
Solution X 0 2 4 6 8
Y 0 1.9 2 2 2.4
𝑏−𝑎 8−0
step size ℎ = = =2
𝑛 4
𝑛−1 𝑛−2
ℎ
≅ 𝑓 𝑎 +4 𝑓(𝑥𝑖 ) + 2 𝑓(𝑥𝑖 ) + 𝑓(𝑏)
3
𝑖=1 𝑖=2
𝑖=𝑜𝑑𝑑 𝑖=𝑒𝑣𝑒𝑛
ℎ
≅ 𝑓 𝑥0 + 4 𝑓 𝑥1 + 𝑓 𝑥3 + 2 𝑓 𝑥2 + 𝑓 𝑥4
3
2
≅ 𝑓 0 +4 𝑓 2 +𝑓 6 +2 𝑓 4 +𝑓 8
3
𝐼 ≅ 14.66