W9 - Numerical Integration - Course Notes
W9 - Numerical Integration - Course Notes
Week 9 Overview
Most of you will know that integrating a function between two limits will give you the area
enclosed by the function, the limits, and your independent variable axis. Most of you will also
know how to integrate a range of functions. In many situations, however, you may have to deal
with functions that are extremely difficult or impossible to integrate exactly. In other situations,
as scientists and engineers there may not be a function that describes the area that you need to
determine. In situations like these, you will need numerical integration.
1
ENG1014 Engineering Numerical Analysis Course Notes
Integration is the inverse of differentiation. This means that we can analytically calculate simple
integrations.
For example, we know how to compute the integral of 𝑓(𝑥) = 0.4𝑥 3 − 3𝑥 2 + 2𝑥 + 100:
We also know how to compute the definite integral of 𝑓(𝑥) = 0.4𝑥 3 − 3𝑥 2 + 2𝑥 + 100 between
𝑎 and 𝑏:
𝑏 𝑏
𝐼 = ∫ 𝑓(𝑥) 𝑑𝑥 = ∫ 0.4𝑥 3 − 3𝑥 2 + 2𝑥 + 100 𝑑𝑥
𝑎 𝑎
= [0.1𝑥 4 − 𝑥 3 + 𝑥 2 + 100𝑥 + 𝑐]𝑏𝑎
= (0.1𝑏4 − 𝑏3 + 𝑏2 + 100𝑏 + 𝑐 ) − (0.1𝑎4 − 𝑎3 + 𝑎2 + 100𝑎 + 𝑐 )
Note that we use 𝐼 to denote the value of the integral between two bounds.
However, there is not always an analytical solution to the integral we would like to determine.
The function might be complex to solve analytically or there might not be a function that
describes what we are dealing with at all; for example, a set of discrete measurements taken in
an experiment or in the field. This is where numerical integration is useful.
2
ENG1014 Engineering Numerical Analysis Course Notes
3
ENG1014 Engineering Numerical Analysis Course Notes
(𝑓(𝑎) + 𝑓(𝑏))
𝐼 = (𝑏 − 𝑎) Equation 9.2
2
Practically, we would almost never use only a single segment for numerical integration; a more
accurate method is to use multiple trapezoids, as discussed in the next section.
4
ENG1014 Engineering Numerical Analysis Course Notes
Figure 9.3 – Composite trapezoidal integration with increasing number of segments. Successive
estimates of the final integral are shown above each image. One can clearly see that these
converge towards the final value (408) as the number of segments becomes larger.
5
ENG1014 Engineering Numerical Analysis Course Notes
Uniform segments
The composite trapezoidal integration rule for uniform segments of width ℎ simply extends the
approach above by subdividing the integrand into multiple segments of equal width, ℎ.
The simplest case is two trapezoidal segments, as illustrated in Figure 9.4 below. These two
𝑎+𝑏 𝑎+𝑏
trapezoidal segments will share a vertex at ( ,𝑓( )).
2 2
Figure 9.4 – The composite of two trapezia Figure 9.5 – The composite of four trapezia
𝑏−𝑎
Since there are two segments, the width of each segment is ℎ = .
2
6
ENG1014 Engineering Numerical Analysis Course Notes
Figure 9.6 – The composite trapezoidal method for uniform segments of width ℎ
If we look at the generic case illustrated in Figure 9.6 above, we can obtain the general form of
the estimate of the integral for 𝑛 points. Note that we have replaced 𝑎 and 𝑏 with 𝑥1 and 𝑥𝑛 ,
respectively, and maintained a uniform width ℎ for all trapezoidal segments:
ℎ
𝐼= [𝑓(𝑥1 ) + 2𝑓(𝑥2 ) + 2𝑓(𝑥3 ) + ⋯ + 2𝑓(𝑥𝑛−1 ) + 𝑓(𝑥𝑛 )]
2
Thus, the composite trapezoidal rule for uniform segments of width ℎ and 𝑛 points is:
𝑛−1
ℎ
𝐼 = [𝑓(𝑥1 ) + 2 (∑ 𝑓(𝑥𝑖 )) + 𝑓(𝑥𝑛 )] Equation 9.3
2
𝑖=2
7
ENG1014 Engineering Numerical Analysis Course Notes
Arguments: Returns:
• 𝑓: lambda function to be solved • 𝐼: estimated integral
• 𝑎: lower bound of the integral
• 𝑏: upper bound of the integral
• 𝑛: number of points
Algorithm:
1. Create a 1D array for 𝑥𝑖 points so that 𝑓(𝑥𝑖 ) can be calculated.
2. Create a 1D array for 𝑓(𝑥𝑖 ) points, let this be known as 𝑦.
3. Determine the width of each segment, ℎ.
4. Calculate the middle summation, ∑𝑛−1
𝑖=2 𝑦𝑖 .
Complete Steps 1-3 from the W9 – Numerical Integration ENG1014 Module Instructions
Non-uniform segments
The composite trapezoidal integration rule for uniform segments of width ℎ is applied to a
known function, as we can sample that function in any way that we like. However, sometimes,
like in an experiment, we may not be able to have equally spaced data. As such, we will have
non-uniform segments of different widths.
Essentially, the composite trapezoidal rule for non-uniform segments is the summation of every
trapezoidal segment between each pair of adjacent points (𝑥𝑖−1 and 𝑥𝑖 ). Thus, from Equation
9.2, which estimated the integral using a single trapezoidal segment, we can obtain the
composite trapezoidal rule for non-uniform segments and 𝑛 points by replacing 𝑎 and 𝑏 with
𝑥𝑖−1 and 𝑥𝑖 , respectively:
𝑛
𝑥𝑖 − 𝑥𝑖−1
𝐼 = ∑( ) (𝑓(𝑥𝑖−1 ) + 𝑓(𝑥𝑖 )) Equation 9.4
2
𝑖=2
8
ENG1014 Engineering Numerical Analysis Course Notes
Arguments: Returns:
• 𝑥: 1D array of discrete 𝑥𝑖 observations • 𝐼: estimated integral
• 𝑦: 1D array of discrete 𝑦𝑖 observations
Algorithm:
1. Calculate the width of each segment, 𝑥𝑖 − 𝑥𝑖−1 , let this be known as ℎ𝑖 .
2. Calculate 𝑓(𝑥𝑖−1 ) + 𝑓(𝑥𝑖 ), let this be known as 𝑏𝑎𝑠𝑒𝑠𝑖 .
3. Calculate the integral estimate, 𝐼, according to Equation 9.4.
Complete Steps 4-5 from the W9 – Numerical Integration ENG1014 Module Instructions
Refer to Examples 1-2 from the W9 – Numerical Integration – Course Notes Examples
The local truncation error is also dependent on the nature of the function, as illustrated in Figure
9.7. A trapezium will give the exact integral when 𝑓(𝑥) is a linear function. Truncation errors start
to appear when the second derivative of the function is non-zero and become worse as the
second and higher order derivatives of the function increase in magnitude.
9
ENG1014 Engineering Numerical Analysis Course Notes
It is possible to show that the truncation error, 𝐸𝑇,𝑙𝑜𝑐𝑎𝑙 , in the trapezoidal rule is:
1
𝐸𝑇,𝑙𝑜𝑐𝑎𝑙 = − 12 𝑓′′(𝜉). ℎ3
where
It is sufficient to say that the local truncation error “scales with ℎ3 ”. Mathematically, this is
expressed as 𝐸𝑇,𝑙𝑜𝑐𝑎𝑙 = 𝑂(ℎ3 ), where 𝑂 is “order”. Thus, the statement 𝐸𝑇,𝑙𝑜𝑐𝑎𝑙 = 𝑂(ℎ3 ) should
be interpreted as, “the local truncation error changes as a function of order ℎ 3”.
Note that if a function has a second derivative that is 0, trapezoidal integration methods will
have zero error, i.e., linear functions will be integrated exactly.
The “global” truncation error, 𝐸𝑇,𝑔𝑙𝑜𝑏𝑎𝑙 , over the entire integrand is the sum over all of the
truncation errors. Note that these can be both positive and negative, and so the errors are often
cancellative instead of cumulative. The number of segments also increases based on the value
of ℎ. You do not need to know the derivation for ENG1014, but it is possible to show that:
𝐸𝑇,𝑔𝑙𝑜𝑏𝑎𝑙 = 𝑂(ℎ 2 )
In a similar fashion to the “big-O notation” for computational complexity, discussed in week
7, this analysis will not tell you the absolute value of the error in an integration.
However, because the equation for the local error only contains a ℎ3 term, without any lower
order terms, we can find the factor by which the local error will change if the step size is
changed. The global error can also be approximately found. For example:
• If the step size is halved (i.e. twice as many trapezoids are used), then 𝐸𝑇,𝑙𝑜𝑐𝑎𝑙 will be
1/8 of its original value and 𝐸𝑇,𝑔𝑙𝑜𝑏𝑎𝑙 will be around 1/4 of its original value
• If the step size is tripled, 𝐸𝑇,𝑙𝑜𝑐𝑎𝑙 will be 27 times larger than its original value and
𝐸𝑇,𝑔𝑙𝑜𝑏𝑎𝑙 will be around 9-fold larger.
10
ENG1014 Engineering Numerical Analysis Course Notes
Complete Practice Questions 1-2 from the W9 – Numerical Integration – Course Notes
Practice Questions
ℎ
𝐼= (𝑓(𝑥1 ) + 4𝑓(𝑥2) + 𝑓(𝑥3 )) Equation 9.5
3
11
ENG1014 Engineering Numerical Analysis Course Notes
Figure 9.9 – Composite Simpson’s 1/3 rule with increasing number of segments
12
ENG1014 Engineering Numerical Analysis Course Notes
Consider the case of two parabolas, as shown in Figure 9.10, where we have five equally spaced
points where we sample the function 𝑓(𝑥). These points are (𝑥1 , 𝑓(𝑥1)), (𝑥2, 𝑓(𝑥2 )), (𝑥3 , 𝑓(𝑥3 ))
for the first parabola, and (𝑥3 , 𝑓(𝑥3)), (𝑥4 , 𝑓(𝑥4)), and (𝑥5 , 𝑓(𝑥5)) for the second parabola. Note
that (𝑥3 , 𝑓(𝑥3)) is considered twice in determining both parabolas.
13
ENG1014 Engineering Numerical Analysis Course Notes
Consider the case of three parabolas, as shown in Figure 9.11, where we have seven equally
spaced points where we sample the function 𝑓(𝑥). These points are (𝑥1 , 𝑓(𝑥1)), (𝑥2, 𝑓(𝑥2 )),
(𝑥3 , 𝑓(𝑥3 )) for the first parabola, (𝑥3 , 𝑓(𝑥3)) , (𝑥4 , 𝑓(𝑥4)) , and (𝑥5 , 𝑓(𝑥5 )) for the second
parabola, and (𝑥5 , 𝑓(𝑥5 )) , (𝑥6 , 𝑓(𝑥6 )) , and (𝑥7 , 𝑓(𝑥7 )) for the third parabola. Note that
(𝑥3, 𝑓(𝑥3 )) is considered twice in determining the first two parabolas, and (𝑥5 , 𝑓(𝑥5 )) is
considered twice in determining the second and third parabolas.
Our estimate of the integral for the case of three parabolas is therefore:
ℎ
[𝑓(𝑥1 ) + 4𝑓(𝑥2 ) + 2𝑓(𝑥3) + 4𝑓(𝑥4) + 2𝑓(𝑥5) + 4𝑓(𝑥6 ) + 𝑓(𝑥7)]
𝐼 = 𝐼1 + 𝐼2 + 𝐼3 =
3
We can see from this that there is a general pattern emerging as illustrated in Figure 9.12.
𝑛−1 𝑛−2
ℎ
𝐼 = 𝑓(𝑥1 ) + 4 ( ∑ 𝑓(𝑥𝑖 )) + 2 ∑ 𝑓(𝑥𝑗 ) + 𝑓(𝑥𝑛 ) Equation 9.6
3
𝑖=2 𝑗=3
[ 𝑖 𝑖𝑠 𝑒𝑣𝑒𝑛 ]
(𝑗 𝑖𝑠 𝑜𝑑𝑑 )
There are some conditions that apply:
• To fit a parabola, three points are needed. That means that the smallest number of
points for this method to work is three, or a minimum of two segments of equal width.
• Using the composite approach, where at least two parabolas are fitted, the point
between any two neighbouring parabolas is shared between them. Thus, the number of
points with increasing number of parabolas is 3, 5,7... The composite approach will
only work with an odd number of points (and thus an even number of segments).
14
ENG1014 Engineering Numerical Analysis Course Notes
Arguments: Returns:
• 𝑓: lambda function to be solved • 𝐼: estimated integral
• 𝑎: lower bound of the integral
• 𝑏: upper bound of the integral
• 𝑛: number of points
Algorithm:
1. Create a 1D array for 𝑥𝑖 points so that 𝑓(𝑥𝑖 ) can be calculated
2. Create a 1D array for 𝑓(𝑥𝑖 ) points, let this be known as 𝑦
3. Determine the width of each segment, ℎ
4. Calculate the even summation, ∑𝑛−1 𝑛−2
𝑖=2 𝑦𝑖 and odd summation, ∑ 𝑗=3 𝑦𝑗
𝑖,even 𝑗,odd
Arguments: Returns:
• 𝑥: 1D array of discrete 𝑥𝑖 observations • 𝐼: estimated integral
• 𝑦: 1D array of discrete 𝑦𝑖 observations
Algorithm:
1. Determine the width of each segment, ℎ
2. Calculate the even summation, ∑𝑛−1 𝑛−2
𝑖=2 𝑦𝑖 and odd summation, ∑ 𝑗=3 𝑦𝑗
𝑖,even 𝑗,odd
Complete Steps 6-9 from the W9 – Numerical Integration ENG1014 Module Instructions
15
ENG1014 Engineering Numerical Analysis Course Notes
Non-uniform segments
It becomes computationally cumbersome to implement the fitting of parabolas to unevenly
spaced points. It is theoretically possible, but the mathematics becomes much more
complicated and the expressions for the area between each parabola and the independent
variable axis do not simplify to something as concise as Equation 9.6 above. Thus, we do not
generally consider a composite Simpson’s 1/3 rule for unevenly spaced points (unlike the
composite trapezoidal method).
Refer to Examples 3-4 from the W9 – Numerical Integration – Course Notes Examples
where 𝜉 is somewhere in the integrand and 𝑓 (4) is the 4th derivative of the function. In other
words:
𝐸𝑇,𝑙𝑜𝑐𝑎𝑙 = 𝑂(ℎ5 )
It can also be shown that:
𝐸𝑇,𝑔𝑙𝑜𝑏𝑎𝑙 = 𝑂(ℎ4 ).
Note that if a function has a fourth derivative that is 0, Simpson’s 1/3 methods will have no error.
In other words, cubic functions (and polynomials of lower order) will be integrated exactly.
Complete Practice Questions 3-4 from the W9 – Numerical Integration – Course Notes
Practice Questions
16
ENG1014 Engineering Numerical Analysis Course Notes
Figure 9.13 – Fitting a cubic function to a set of four evenly spaced points.
Consider Figure 9.13; we can see that three equally spaced points (𝑥1, 𝑓(𝑥1 )), (𝑥2, 𝑓(𝑥2 )),
(𝑥3 , 𝑓(𝑥3 )) , and (𝑥4 , 𝑓(𝑥4 )) , separated by the interval ℎ = 𝑥2 − 𝑥1 = 𝑥3 − 𝑥2 = 𝑥4 − 𝑥3 , are
fitted with a cubic function. The area under the cubic between 𝑥1 and 𝑥4 is the estimate, 𝐼, of the
integral of 𝑓(𝑥) from 𝑥1 to 𝑥4.
It can be shown that Simpson’s 3/8 rule for uniform segments of width ℎ is:
3ℎ
𝐼= (𝑓(𝑥1 ) + 3𝑓(𝑥2 ) + 3𝑓(𝑥3 ) + 𝑓(𝑥4 )) Equation 9.7
8
Refer to Examples 5-6 from the W9 – Numerical Integration – Course Notes Examples
17
ENG1014 Engineering Numerical Analysis Course Notes
Figure 9.14 – Composite Simpson’s 3/8 rule with five applications of the Simpson’s 3/8 rule
We can see from Figure 9.15 that a pattern emerges, as was the case for the composite
Simpson’s 1/3 method. Thus, we can apply the same process as we did earlier; we can sum the
overlapping coefficients for each application of Simpson’s 3/8 rule for each of the concatenated
cubic functions to obtain an estimate for the integral.
18
ENG1014 Engineering Numerical Analysis Course Notes
The composite Simpson’s 3/8 rule for equal segments of width ℎ is:
𝑛−2 𝑛−1 𝑛−3
3ℎ
𝐼= [𝑓(𝑥1 ) + 3 ( ∑ 𝑓(𝑥𝑖 )) + 3 ( ∑ 𝑓(𝑥𝑖 )) + 2 ( ∑ 𝑓(𝑥𝑖 )) + 𝑓(𝑥𝑛 )]
8
𝑖=2,5,8,… 𝑖=3,6,9,… 𝑖=4,7,10,…
Equation 9.8
There are some conditions that apply:
• To fit a cubic, four points are required. Thus, the smallest number of points required for
this method is four, which corresponds to three segments of equal width.
• Using the composite approach, where at least two cubic functions are fitted, the point
between any two neighbouring cubic functions is shared between them. Thus, the
number of points with increasing number of cubic functions is 4, 7, 10, etc. The
composite approach will only work when the number of points minus 1 is a multiple of
3, i.e., the number of segments is a multiple of 3.
Non-uniform segments
It is computationally fiendish to implement the fitting of cubic functions to non-uniform
segments. It can be done, but the mathematics becomes much more complicated and the
expressions for the area between each cubic function and the independent variable axis do not
simplify to something as concise as Equation 9.8. Thus, in ENG1014, we do not consider a
composite Simpson’s 3/8 rule for non-uniform segments.
i.e.,
𝐸𝑇,𝑙𝑜𝑐𝑎𝑙 = 𝑂(ℎ5 ).
It can also be shown that:
𝐸𝑇,𝑔𝑙𝑜𝑏𝑎𝑙 = 𝑂(ℎ4 ).
The local truncation error changes as a function of order ℎ5 , and the global truncation error
changes as a function of order ℎ4, just like for the Simpson’s 1/3 rule. In other words, nothing is
gained from Simpson’s 3/8 rule over the 1/3 rule with regard to truncation error. It is worth noting
that both methods will also integrate cubic functions exactly.
19
ENG1014 Engineering Numerical Analysis Course Notes
Figure 9.16 – Composite Simpson’s 1/3 rule combined with Simpson’s 3/8 rule to integrate
over 9 segments (10 points)
As a result of all the above, we will usually only apply a single application of Simpson’s 3/8 rule.
It will be used where we have a discrete datasets {(𝑥𝑖 , 𝑦𝑖 )} with four datapoints – either:
• There is only four datapoints in the entire set, or
• There is an even number of points in the dataset and we wish to combine Simpson’s 3/8
with Simpson’s 1/3 due to the reduced size of the expected error.
20
ENG1014 Engineering Numerical Analysis Course Notes
Arguments: Returns:
• 𝑓: lambda function to be solved • 𝐼: estimated integral
• 𝑎: lower bound of the integral
• 𝑏: upper bound of the integral
Algorithm:
1. Create a 1D array for 𝑥𝑖 points so that 𝑓(𝑥𝑖 ) can be calculated
2. Create a 1D array for 𝑓(𝑥𝑖 ) points, let this be known as 𝑦
3. Determine the width of each segment, ℎ
4. Calculate the integral estimate, 𝐼, according to Equation 9.7.
Arguments: Returns:
• 𝑥: 1D array of discrete 𝑥𝑖 observations • 𝐼: estimated integral
• 𝑦: 1D array of discrete 𝑦𝑖 observations
Algorithm:
1. Determine the width of each segment, ℎ
2. Calculate the integral estimate, 𝐼, according to Equation 9.7.
Complete Steps 10-13 from the W9 – Numerical Integration ENG1014 Module Instructions
21
ENG1014 Engineering Numerical Analysis Course Notes
Complete Practice Questions 5-8 from the W9 – Numerical Integration – Course Notes
Practice Questions
22
ENG1014 Engineering Numerical Analysis Course Notes
Refer to Examples 5-6 from the W9 – Numerical Integration – Course Notes Examples
23
ENG1014 Engineering Numerical Analysis Course Notes
Composite Simpson’s 3/8 rule, we discussed that these methods can only be used with uniform
segments. Thus, another input validation test needs to be added that checks that 𝑥𝑖 is evenly
spaced.
Input testing for numerical integration - set of discrete data {(𝒙𝒊, 𝒚𝒊 )} version
Before step 1 of each method, check the following conditions. If they are true, continue. If they
are false, raise an error.
All methods:
• 𝑥𝑖 is 1D
• 𝑦𝑖 is 1D
• 𝑥𝑖 and 𝑦𝑖 have the same number of points
Composite trapezoidal method:
• Number of points is greater than or equal to 2
Composite Simpson’s 1/3 method:
• Number of points is greater than or equal to 3
• Number of points is odd
• Segments are uniform
Simpson’s 3/8 method:
• Number of points is 4
• Segments are uniform
Complete Steps 15-16 from the W9 – Numerical Integration ENG1014 Module Instructions
24