ES 361 - Computing
Methods in Engineering
Lecture 7
Numerical Integration
We need numerical integration if analytical integration is not possible:
2
Some functions cannot be integrated 𝑒 𝑥 𝑑𝑥
The function is unknown (i.e. it is in the form of data points)
The Newton-Cotes formulas are the most common numerical integration schemes. They are based on the
strategy of replacing a complicated function or tabulated data with an approximating function that is easy
to integrate:
𝑏 𝑏
𝐼= 𝑓 𝑥 𝑑𝑥 ≅ 𝑓𝑛 𝑥 𝑑𝑥
𝑎 𝑎
where fn(x) is a polynomial of the form:
𝑓𝑛 𝑥 = 𝑎0 + 𝑎1 𝑥 + ⋯ + 𝑎𝑛−1 𝑥 𝑛−1 + 𝑎𝑛 𝑥 𝑛
Trapezoidal Rule
The trapezoidal rule is the first of the Newton-Cotes closed integration formula
𝑏 𝑏
𝐼= 𝑓 𝑥 𝑑𝑥 ≅ 𝑓1 𝑥 𝑑𝑥 f(x)
𝑎 𝑎
Error !
f1(x) is a first order polynomial. Using Lagrange’s interpolation:
𝑥−𝑏 𝑥−𝑎 f1(x)
𝑓1 𝑥 = 𝑓 𝑎 + f(b)
𝑎−𝑏 𝑏−𝑎
𝑓 𝑏 − 𝑓(𝑎)
𝑓1 𝑥 = 𝑓 𝑎 + (x − a)
𝑏−𝑎
The area under the straight line defined by fn(x) is an estimate of the integral of f(x) between the
limits a and b:
𝑏
𝑓 𝑏 − 𝑓(𝑎)
𝐼≅ 𝑓 𝑎 + (x − a) 𝑑𝑥
𝑎 𝑏 − 𝑎
The result of the integration is then:
𝑓 𝑎 + 𝑓(𝑏)
𝐼 ≅ (𝑏 − 𝑎)
2 𝒇(𝒙)
Trapezoidal Rule
Note that this method gives poor results if a and b are far apart.
Error of the Trapezoidal Rule
The true error of this approximation is formulated by using the following expression:
1 ′′
𝐸𝑡 = − 𝑓 (ξ)(𝑏 − 𝑎)3
12
𝑑 2 𝑓(𝑥)
where ξ lies somewhere in the interval from a to b and 𝑓 ′′ 𝑥 = . For derivation of this error
𝑑𝑥 2
expression please visit box 21.2 of the textbook.
In order to minimize the error in case of b>>a, dividing the range [a-b] into multiple segments is
recommended.
Multiple Application of Trapezoidal Rule
One way to improve the accuracy of the trapezoidal rule is to divide the integration interval from a to b
into a number of segments and apply the method to each segment.
The areas of individual segments can then be added to yield the integral for the entire interval.
If there are n + 1 equally spaced base points (x0, x1, x2, . . . , xn),
there are n segments of equal width:
𝑏−𝑎
ℎ=
𝑛
If a and b are designated as x0 and xn, respectively, the total
integral can be represented as
𝑥1 𝑥2 𝑥𝑛
I= 𝑓 𝑥 𝑑𝑥 + 𝑓 𝑥 𝑑𝑥 + ⋯ + 𝑓 𝑥 𝑑𝑥
𝑥0 𝑥1 𝑥𝑛−1
Multiple Application of Trapezoidal Rule
𝑓 𝑥0 + 𝑓 𝑥1 𝑓 𝑥1 + 𝑓 𝑥2 𝑓 𝑥2 + 𝑓 𝑥3
I ≅ 𝑥1 − 𝑥0 + 𝑥2 − 𝑥1 + 𝑥3 − 𝑥2 + ⋯+
2 2 2
𝑓 𝑥𝑛−1 + 𝑓 𝑥𝑛
(𝑥𝑛 − 𝑥𝑛−1 )
2
If we group the terms, it will yield:
𝑛−1
ℎ
I ≅ 𝑓 𝑥0 + 2 𝑓 𝑥𝑖 + 𝑓 𝑥𝑛
2
𝑖=1
Or
Multiple Application of Trapezoidal Rule
The error for multiple application of the trapezoidal rule is:
𝑛 3 𝑛
(𝑏 −𝑎)
(𝐸𝑡 )𝑖 = − 𝑓 ′′ (ξ𝑖 )
12𝑛3
𝑖=1 𝑖=1
′′ ξ
𝑑 2 𝑓(𝑥)
𝑓 =
where 𝑖
𝑑𝑥 2 ξand 𝑥𝑖−1 ≤ ξ𝑖 ≤ 𝑥𝑖
𝑖
Let the average second derivative be:
𝑛 ′′
𝑖=1 𝑓 (ξ𝑖 )
𝑓 ′′ ≅
𝑛
Then:
(𝑏 − 𝑎)3 ′′
𝐸𝑎 = − 2
𝑓
12𝑛
Multiple Application of Trapezoidal Rule
Calculation of 𝑓 ′′ together with estimation of higher order derivatives for determining the error is
unnecessary. This will be illustrated below:
Let 𝐼 = 𝐼𝑛1 + 𝐸𝑛1
𝐼 : the exact solution for integral
𝐼𝑛1 : the approximation by using trapezoidal rule and n1 intervals
𝐸𝑛1 : error for approximation using n1 intervals
Then considering n2 intervals 𝐼 = 𝐼𝑛2 + 𝐸𝑛2
By subtracting these equations from one another:
𝐸𝑛1 − 𝐸𝑛2 = 𝐼𝑛2 − 𝐼𝑛1
3
𝑏−𝑎 1 1
− 𝑓 ′′ − = 𝐼𝑛2 − 𝐼𝑛1
12 𝑛12 𝑛22
𝑏−𝑎 3 𝑛 2 2
𝑛2
1
− ′′
𝑓 = 2
1
𝐼 − 𝐼𝑛1 so 𝐸𝑛1 ≅ 𝐼𝑛2 − 𝐼𝑛1 2
12 𝑛2 − 𝑛12 𝑛2 𝑛
1− 1
𝑛2
Simpson’s 1/3 Rule
This is an improvement over trapezoidal rule.
It is obtained when a second order interpolating polynomial is
f(x) used.
f2(x) 𝑏 𝑏
Error !
𝐼= 𝑓 𝑥 𝑑𝑥 ≅ 𝑓2 𝑥 𝑑𝑥
𝑎 𝑎
x0 (a) x1 x2 (b)
Use a second order Lagrange Interpolating Polynomial:
𝑥2
(𝑥 − 𝑥1 )(𝑥 − 𝑥2 ) (𝑥 − 𝑥0 )(𝑥 − 𝑥2 ) (𝑥 − 𝑥0 )(𝑥 − 𝑥1 )
𝐼≅ 𝑓 𝑥0 + 𝑓 𝑥1 + 𝑓 𝑥2 𝑑𝑥
𝑥0 (𝑥0 − 𝑥1 )(𝑥0 − 𝑥2 ) (𝑥1 − 𝑥0 )(𝑥1 − 𝑥2 ) (𝑥2 − 𝑥0 )(𝑥2 − 𝑥1 )
Simpson’s 1/3 Rule
Solution of this simple calculus problem is:
The error of this approximation is (see box 21.3 of textbook and equation 21.16 for its derivation):
𝑑4 𝑓(𝑥)
where ξ lies somewhere in the interval from a to b and 𝑓 (4) 𝑥 = .
𝑑𝑥 4
Multiple Application of Simpson’s 1/3 Rule
Just as with the trapezoidal rule, Simpson’s rule can be
improved by dividing the integration interval into a number
of segments of equal width
𝑏−𝑎
ℎ=
𝑛
x0 x1 x2 xn-2 xn-1 xn
2 intervals for each approximation of Simpson’s Rule
The total integral can be represented as:
𝑏 𝑥2 𝑥4 𝑥𝑛
I= 𝑓 𝑥 𝑑𝑥 = 𝑓 𝑥 𝑑𝑥 + 𝑓 𝑥 𝑑𝑥 + ⋯ + 𝑓 𝑥 𝑑𝑥
𝑎 𝑥0 𝑥2 𝑥𝑛−2
Multiple Application of Simpson’s 1/3 Rule
Substituting Simpson’s 1/3 rule for the individual integral yields:
𝑓 𝑥0 + 4𝑓 𝑥1 + 𝑓 𝑥2 𝑓 𝑥2 + 4𝑓 𝑥3 + 𝑓 𝑥4 𝑓 𝑥𝑛−2 + 4𝑓 𝑥𝑛−1 + 𝑓 𝑥𝑛
𝐼 ≅ 2ℎ + 2ℎ + ⋯ + 2ℎ
6 6 6
An error estimate for the multiple-application Simpson’s rule is obtained in the same fashion as for the
trapezoidal rule by summing the individual errors for the segments and averaging the derivative to yield:
where 𝑓 (4) is the average fourth derivative for the interval [a,b].
Error of Multiple Application of Simpson’s 1/3 Rule
Note that, calculation of 𝑓 (4) can be avoided by using the difference between two approximations, as
shown for the trapezoidal rule. Hence the error for n=n1 segments is:
1
𝐸𝑛1 ≅ 𝐼𝑛2 − 𝐼𝑛1 4
𝑛
1− 1
𝑛2
Note that, n should be an even number for Simpson’s 1/3 Rule if the data set involves an odd number of
f(x) evaluations then:
• The trapezoidal rule can be used for the last segment
• Simpson’s 3/8 rule can be used for the last 3 segments
Simpson’s 3/8 Rule
In Simpson’s 3/8 rule, a third order Lagrange polynomial can be fit
to four points and integrated:
f3(x)
𝑏 𝑏
𝐼= 𝑓 𝑥 𝑑𝑥 ≅ 𝑓3 𝑥 𝑑𝑥
Error ! 𝑎 𝑎
f(x) Use a third order Lagrange Interpolating Polynomial:
3ℎ
𝐼≅ 𝑓 𝑥0 + 3𝑓 𝑥1 + 3𝑓 𝑥2 + 𝑓(𝑥3 )
8
x0 (a) x1 x2 x3 (b)
Error of Simpson’s 3/8 Rule
𝑏−𝑎
Since ℎ=
3
Note that:
• Simpson’s 3/8 Rule uses 4 points and Simpson’s 1/3 Rule uses 3 points, hence 1/3 is preferred.
• Simpson’s 3/8 Rule is used when there exist even number of points (odd number of segments).
• Therefore, Simpson’s 1/3 and 3/8 Rules are used together to handle even number of points.
Gaussian Quadrature (Gauss-Legendre Formula)
Trapezoidal Rule connects two points on a function with a line to
calculate the integral.
𝑓 𝑎 + 𝑓(𝑏)
𝐼 ≅ (𝑏 − 𝑎)
2
An improved integral estimate obtained by taking the area under
the straight line passing through two intermediate points. By
positioning these points wisely, the positive and negative errors
are balanced, and an improved integral estimate results.
Derivation of the Two-Point Gauss-Legendre Formula
The object of Gauss quadrature is to determine the coefficients
of an equation of the form
𝐼 ≅ 𝑐0 𝑓 𝑥0 + 𝑐1 𝑓 𝑥1
where the c’s are the unknown coefficients.
However, in contrast to the trapezoidal rule that used fixed end points a and b, the function arguments
x0 and x1 are not fixed at the end points, but are unknowns. Thus, we now have a total of four
unknowns that must be evaluated, and consequently, we require four conditions to determine them
exactly.
Derivation of the Two-Point Gauss-Legendre Formula
These equations will be obtained by assuming that Gauss-Legendre Formula calculates integrals of constant,
linear, parabolic and cubic functions exactly.
𝑐0 𝑓 𝑥0 + 𝑐1 𝑓 𝑥1 = 1 𝑑𝑥 = 2 Solving these equations simultaneously yield:
−1
𝑐0 = 1
1
𝑐0 𝑓 𝑥0 + 𝑐1 𝑓 𝑥1 = 𝑥 𝑑𝑥 = 0
−1 𝑐1 = 1
1
2 1
𝑐0 𝑓 𝑥0 + 𝑐1 𝑓 𝑥1 = 𝑥 2 𝑑𝑥 = 𝑥0 = −
3 3
−1
1 1
𝑥1 =
𝑐0 𝑓 𝑥0 + 𝑐1 𝑓 𝑥1 = 𝑥 3 𝑑𝑥 =0 3
−1
−1 1
𝐼≅𝑓 +𝑓
3 3
Derivation of the Two-Point Gauss-Legendre Formula
This approximation will yield to exact results for polynomials with orders up to 3. Accurate approximations
for higher order polynomials are possible by using more points on the x-axis such that:
𝐼 ≅ 𝑐0 𝑓 𝑥0 + 𝑐1 𝑓 𝑥1 + 𝑐2 𝑓 𝑥2
𝐼 ≅ 𝑐0 𝑓 𝑥0 + 𝑐1 𝑓 𝑥1 + 𝑐2 𝑓 𝑥2 + 𝑐3 𝑓 𝑥3
In general form:
𝐼≅ 𝑐𝑖 𝑓 𝑥𝑖 Gauss Legendre Formula
𝑖=0
Higher Point Formulas
The solutions for higher number of points (n≥7) can be found on the web, in textbooks or can be calculated
using MATLAB.
Derivation of the Two-Point Gauss-Legendre Formula
The approximation for I in case the range of integration is not [-1,1] can be calculated by using
𝑏
transformation. If the original integration is 𝑎 𝑔 𝑡 𝑑𝑡 we need to transform t ϵ 𝑎, 𝑏 into x ϵ −1,1 .
This can be done by mapping [a,b] to [-1, 1] by t=m+nx , such that
if x= -1 , then t=a , so m-n = a
𝑎+𝑏 𝑏−𝑎
if x= 1 , then t=b , so m+n = b 𝑡= + 𝑥
2 2
𝑎+𝑏 𝑏−𝑎
𝑔 𝑡 =𝑔 + 𝑥 𝑏−𝑎
2 2 𝑑𝑡 = 𝑑𝑥
2
𝑏 1
𝑎+𝑏 𝑏−𝑎 𝑏−𝑎
𝑔 𝑡 𝑑𝑡 = 𝑔 + 𝑥 𝑑𝑥
2 2 2
𝑎 −1
Error of Two-Point Gauss-Legendre Formula
The error of this approximation is (Carnahan et al. 1969):
2𝑛+1 𝑛! (2𝑛)
𝐸𝑡 = 𝑓 (ξ)
(2𝑛 + 1) 2𝑛 ! 3
𝑑2𝑛 𝑓(𝑥)
where ξ lies somewhere in the interval from -1 to 1 and 𝑓 (2𝑛) 𝑥 = .
𝑑𝑥 2𝑛
Usually, the calculation of this derivative can be impractical so a comparison of a higher order solution with
that of a lower order solution can be considered as a reasonable estimation of error :
𝐼𝑛2 −𝐼𝑛1
𝐸𝑎 = 100% where n2>n1
𝐼𝑛2
Note that an n point Gauss-Legendre Formula will yield to exact solution (Et=0) for integration of a
polynomial of order 2n-1. Hence, the method will be useful if the derivatives of function do not yield to
higher figures by increasing the order of derivation.
Improper Integrals
These integrals can be solved numerically after transformation. For example, consider the improper
integral:
𝑏
𝐼= 𝑓 𝑥 𝑑𝑥 𝑏<0
−∞
1 1
Let 𝑥 = , then 𝑑𝑥 = − 𝑑𝑡. This gives:
𝑡 𝑡2
0 1
𝐼= 𝑓 1/𝑡 𝑑𝑡
1/𝑏 𝑡2
which can be solved approximately by a numerical method.