Module 3 Numerical Integration
Module 3 Numerical Integration
2. Close form solutions exist but these solutions are complex and difficult
to use for calculations.
3. Data for variables are available in the form of a table, but no mathe-
matical relationship between them is known, as is often the case with
experimental data.
Rx
We know that a definite integral of the form for example in 1D x01 f (x) dx
can be treated as the area under the curve y = f (x), enclosed between the
limits x = x0 to x = x1 . If you were to perform the integration manually, one
approach is to superimpose a grid on a graph of the function to be integrated,
and simply count the squares, counting only those covered by 50% or more of
the function, see Figure 1. Provided the grid is sufficiently fine, a reasonably
accurate estimate may be obtained. Figure 1 demonstrates how this may be
achieved.
Although the grid methods and other such graphical approaches can pro-
vide us with rough estimates, they are cumbersome and time-consuming and
the final results are far from satisfactory limits. Also, they are too cumber-
some to be applicable when we go for higher dimensions. A better alternative
approach could be to use a technique that uses simple arithmetic operations to
compute the area. Such an approach can be easily implemented on a computer
to carry out the integration (hence the term numerical integration or numeri-
cal quadrature). Numerical integration techniques are similar to the graphical
or in 2D Z x2 Z y2
I(x, y) = f (x, y) dxdy,
x1 y1
or in 3D Z x2 Z y2 Z z2
I(x, y, z) = f (x, y, z) dxdydz
x1 y1 z1
in 1D,
nj
ni X
X
wi wj f (xi , yj )
i=0 j=0
in 2D,
nj nk
ni X
X X
wi wj wk f (xi , yj , zk )
i=0 j=0 k=0
in 3D - to approximate Z x2
I(x) = f (x) dx
x1
in 1D, Z x2 Z y2
I(x, y) = f (x, y) dx dy
x1 y1
in 2D, and Z x2 Z y2 Z z2
I(x, y, z) = f (x, y, z) dx dy dz
x1 y1 z1
∆x
I ≈ It = (f (x0 ) + f (x1 )). (3)
2
The truncation error in the trapezoidal rule involve f ′′ , so that the
rule gives the exact result when applied to any function whose sec-
ond derivative is identically zero, that is, any polynomial of degree
one or less.
If the range to be integrated is large, the trapezoidal rule can be
improved by dividing the interval x0 , x1 into a number of small
intervals and applying the rule discussed above to each of these
subintervals. The sum of areas of all the subintervals is the integral
of the interval x0 , x1 . This is known as composite trapezoidal rule.
This is illustrated in Figure 3. The integral given by Eq. (2) can
be approximated as
" n−1
#
∆x X
I ≈ Ict = f (x0 ) + 2 f (xi ) + f (x1 ) . (4)
2 i=1
where xi = x0 + i∆x.
• Midpoint Rule: In the midpoint rule the function is evaluated at
1
midpoint of the interval i.e. at xmid = (x0 + x1 ). The integral
2
given by Eq. (2) can be approximated as
as
n−1
X
I ≈ Ic,mid = ∆x f (xi ). (6)
i=0
1
where xi = x0 + i + ∆x. This is illustrated in Figure 5 which
2
provides the graphical interpretation of the method. There are two
further advantages of the Mid-point Rule over the trapezoidal rule.
The first is that is requires one fewer function evaluations for a given
number of subintervals, and the second that it can be used more ef-
fectively for determining the integral near an integrable singularity.
The reasons for this are clear from Figure 6.
∆x
I ≈ Is, 1 = (f (x0 ) + 4f (xmid ) + f (x1 )) , (7)
3 3
1
where ∆x = (x1 − x0 ). It is important to note that the Simp-
2
sons’s 1/3 rule is exact up to degree 3, although it is based on
quadratic equation. Similar to the composite trapezoidal rule,we
can construct a composite Simpson’s 1/3 rule to improve the es-
timate of the integral. Here again, the integration interval is di-
vided into n number of segments of equal width, where n is an
1
even number. Them the step size is ∆x = (x1 − x0 ). Again
n
Wednesday, 11.09.2024 2024-25, Ist Sem.
ME 502 Engineering Computing Lab Page 7 of 10
3∆x
I ≈ Ib = (7f (x0 ) + 32f (xm ) + 12f (xn ) + 32f (xo ) + 7f (x1 )) ,
45
(10)
1
where ∆x = (x1 − x0 ). Here, xm = x0 + ∆x, xn = x0 + 2 ∆x,
4
and xo = x0 + 3 ∆x.
where ξi are the sampling points (better known as Gauss points) and
wi = w(ξi ) is called the Gauss point weight corresponding to the Gauss
point ξi . Here, n is the called the order of the Gauss integration method.
Hence, Eq. (11) is called the n-point formula. The problem is to com-
pute the locations of the sampling points and to choose the appropriate
”weights” wi for each Gauss point. Gauss integration method assumes
that x0 = −1 and x1 = +1. In doing so the Gauss point location and
weights are be readily available1 . A MatLab script file lgwt.m is available
on the web which produces the Legendre-Gauss weights and nodes for
computing the definite integral of a continuous function on some interval
(x0 , x1 ) 2 . Table below shows the Gauss point coordinates (locations)
and weight for up to n = 3.
n i or j or k wi or wj or wk ξi or ηj or ζk
1 1 2 0
2 1 1.00000 -0.57735
2 1.00000 +0.57735
3 1 0.55556 +0.77460
2 0.88889 0.00000
3 0.55556 -0.77460
sufficient to give the exact result. The use of more than two Gauss points
will still give the same results, but takes more computational time. The
Gauss integration procedure is by no means limited to one dimensions.
In finite element analysis, for example, integrals of the form
Z +1 Z +1
I = f (ξ, η)dξdη (12)
−1 −1
Z +1 Z +1 Z +1
I = f (ξ, η, ζ)dξdηdζ (13)
−1 −1 −1
are frequently encountered. For such cases the Gauss integration is sam-
pled in two or three directions, as follows
Z +1 Z +1 nξ nη
X X
f (ξ, η)dξdη ≈ wξ wη f (ξi , ηj ) (14)
−1 −1 i=1 j=1
Z +1 Z +1 nξ nη nζ
XXX
f (ξ, η, ζ)dξdηdζ ≈ wξ w(ξi ) wη wζ f (ξi , ηj , ζ(15)
k)
−1 −1 i=1 j=1 k=1
IMPORTANT INSTRUCTIONS
(a) Please write a separate function in C for each integration rule. Also, make
a dedicated function for polynomial evaluation at the sampling point.
This will help you in using the same programme for different polynomials
by just rewriting the function only without the need to change the whole
code.
(b) During the evaluation of this assignment, for each problem please report
the error with respect to the exact answer, where ever applicable. So, it
is preferred that you make tabulate all your results in a single sheet.
(c) Also, it will be desirable that you also carry out a parametric study like
increasing the number of intervals, or increasing the number of Gauss
integration points and report your observation.
Rx
Question 1. Evaluate the integral I = x01 (x3 + 1)dx using the trapezoidal
rule for the intervals (a) (1, 2) and (b) (1, 1.5). Also compare the results
with the exact answer in each case. (Note: Make a function by the name
trapezoidal in C for this.)
R1
Question 2. Compute the integral I = −1 ex dx using the composite trape-
zoidal rule for (a) n = 2 and (b) n = 4. Compare your result with
exact result Iexact = 2.35040. (Note: Make a function by the name
trapezoidalC in C for this.)
R 10
Question 3. Compute the integral I = 0 1 − e−x/2 dx using the com-
posite trapezoidal rule for ∆x = 0.5. Compare your result with exact
result. (Note: Make a function by the name trapezoidalC in C for this.)