Numerical Integration
Numerical Integration
LL Jewell
Analytical Integration
In your maths modules you have learnt to integrate analytically. For example,
1 3
−3 = − +
3 2
Substituting the limits of integration as shown below, we can evaluate the integration constant, c.
−3 = 2 − 2 - 1 − 1
8 12 1 3
= − − −
3 2 3 2
16 − 36 2−9
= −
6 6
−20 + 7
=
6
−13
=
6
= −2,16667
But what do you do when you have to integrate a function, and you cannot integrate it analytically?
In doing reactor technology, you will frequently encounter functions that you cannot integrate from
the rules of integration for analytical solutions that you have learnt in maths.
There are various techniques that will allow you to approximate an integral and get an answer,
without being able to integrate your function analytically. The purpose of these notes is to introduce
you to three of these techniques.
The simplest of these techniques is called the Trapezoidal Rule. When you use this technique you are
assuming that any points that you consider can be joined by straight lines, which effectively means
that you are integrating your function, by adding up the areas of many trapeziums.
If we want to evaluate:
( )
Where f(x) is some function, which is plotted in the graph on the next page.
1
f(x)
a b
If we could integrate f(x) analytically, we would get the exact answer. But we do not know what the
integral of f(x) is, so we will approximate the integral using the trapezoidal rule.
Trapezoidal Rule
ℎ
( ) = ( ) + 2 ( ) + 2 ( ) + ⋯+ 2 ( )+ ( )
2
−
ℎ=
Where = + .ℎ
For n= 3
f(x)
f(x3)
f(x2)
f(x1)
f(x0)
x0 x1 x2 x3
a b
h h h
2
Examples
1.1 Approximate the following integral using the trapezoidal rule and n = 2.
−3
a = 1; b = 2
h = (2 - 1) / 2 = 0.5
a = x0 = 1 f(x0) = 12 – 3(1) = -2
x1 = 1.5 f(x1) = (1.5)2 – 3(1.5) = - 2.25
B= x2 = 2 f(x2) = (2)2 – 3 (2) = -2
ℎ
−3 = ( )+2 ( )+ ( )
2
= −2,125
Note that the analytical answer, which is exact is – 2,16667, whereas the answer that we got from
the trapezoidal rule is -2,125 and is therefore accurate only to two significant figures.
1.2 Approximate the following integral using the trapezoidal rule and n = 3.
1
1+
a = 0; b = 3
for n = 3, h = (3 - 0) / 3 = 1
1 ℎ
= ( )+2 ( )+2 ( )+ ( )
1+ 2
1 2 2 1
= 1+ + +
2 2 33 244
= 1.032352
3
1.3 Approximate the following integral using the trapezoidal rule and n = 12.
1
1+
a = 0; b = 3
1 ℎ
= ( )+2 ( )+2 ( )+2 ( )+2 ( )+2 ( )+2 ( )+2 ( )
1+ 2
+2 ( )+2 ( )+2 ( )+2 ( )+ ( )
= 1.065845
You can see that the answer is the same in the first two significant figures, but varies in the third
significant figure. In general, as h decreases the answer becomes more accurate. But there is a limit
to this. I have used Excel to calculate the function values in the table above. Because Excel only uses
16 significant figures, as h gets closer to 1 x 10-16, the error in the function values becomes more
significant and can make the answer less accurate.
In general, you need to balance the amount of work required to do the calculations against the gain
in accuracy. Another factor in the accuracy is the numerical technique. The Trapezoidal Rule is not
very accurate, Simpson’s rule is better, and Gaussian quadrature is usually better than Simpson’s
Rule.
Simpson’s Rule
ℎ
( ) = ( ) + 4 ( ) + 2 ( ) + 4 ( ) + 2 ( ) + ⋯2 ( )+4 ( )
3
+ ( )
n is the number of strips and must be even in order to use Simpson’s rule.
= + .ℎ
−
ℎ=
4
Examples
−3
a = 1; b = 2
h = (2 - 1) / 2 = 0.5
a = x0 = 1 f(x0) = 12 – 3(1) = -2
x1 = 1.5 f(x1) = (1.5)2 – 3(1.5) = - 2.25
B= x2 = 2 f(x2) = (2)2 – 3 (2) = -2
ℎ
−3 = ( )+4 ( )+ ( )
3
= −2,16667
Note that the analytical answer, which is exact, is – 2,16667, and the answer that we got from the
trapezoidal rule is -2,125 (which has a percentage of error). However the answer that we got from
Simpson’s rule is the same as the answer that we got analytically. The reason for this is that
Simpson’s rule fits a quadratic to the function values and in this examples we are integrating a
quadratic, so Simpson’s rule will always be spot on for a quadratic function.
For an explanation of the derivation of Simpson’s rule and the effect of changing Δx or h, watch this
link:
https://www.youtube.com/watch?v=uc4xJsi99bk
1
1+
a = 0; b = 3
For n = 3…
STOP! We said that for Simpson’s Rule n had to be even. So we cannot use Simpson’s rule and make
n= 3. Let us take n = 4 instead.
h = (3 – 0) / 4 = 0.75
a = x0 = 0 f(x0) = 1
x1 = 0.75 f(x1) = 0,808208
x2 = 1.5 f(x2) = 0,116364
x3 = 2.25 f(x3) = 0,017046
b = x4 = 3 f(x4) = 0,004098
5
1 ℎ
= ( )+4 ( )+2 ( )+4 ( )+ ( )
1+ 3
= 1.134461
1
1+
Answer: 1.074915
For an explanation and examples of the Trapezoidal Rule and Simpson’s Rule, watch the lectures in
these links: https://www.youtube.com/watch?v=l2SjUREZk0c
https://www.youtube.com/watch?v=RTX-ik_8i-k
Gaussian Quadrature
The last technique that I would like to show you is Gaussian Quadrature. It is more computationally
demanding, but in general yields more accurate answers. For the Trapezoidal and Simpson’s Rules,
we selected n, and calculated function values for equidistant x values. For Gaussian quadrature, the
function values are calculated at specific x values, which were determined by Gauss using some
complicated maths.
( ) = ( )
+/- n
2
0,5774 1,000
3
0,7746 0,5556
0,0000 0,8889
4
0,8611 0,3478
0,3400 0,6521
5
0,9062 0,2369
0,5385 0,4786
0,0000 0,5689
6
0,9325 0,1713
0,6612 0,3608
0,2386 0,4679
6
Example
−3
We have been asked to approximate the integral from 1 to 2, but the Gaussian Quadrature formula
is for an integral from -1 to 1.
Thus we need to transform x in our equation to u as used in the technique. This is a linear
transformation.
x: 1 x1 x2 2
u: -1 -0,5774 0,5774 1
Find x1 and x2. You can use the linear interpolation technique that you learnt in your National
Diploma.
−1 2−1
=
−0,5774 − (−1) 1 − (−1)
1
= 0,4226 × +1
2
= 1,2113
= 1,7887
Now we evaluate the function at x1 and x2.
( ) = −2,16665
( ) = −2,16665
The function values are coincidentally the same. This is because we are integrating a quadratic about
its axis of symmetry. The answers will not always be the same.
Now evaluating the integral, taking into account the transformation of the limits:
−
( ) = ( )
2
7
= −2,16665
Looking back to the analytical answer you will see that the exact answer is -2,16667. So the answers
are the same to 5 significant figures.
You can watch the video in this link for another example of how to use two point Gaussian
Quadrature: https://www.youtube.com/watch?v=sefcWt9cd9s
1
1+
We have been asked to approximate the integral from 0 to 3, but the Gaussian quadrature formula is
for an integral from -1 to 1.
Thus we need to transform x in our equation to u as used in the technique. This is a linear
transformation.
x: 0 x1 x2 x3 3
( ) = 0,9956
( ) = 0,1164
( ) = 0,0074
Now evaluating the integral, taking into account the transformation of the limits:
−
( ) = ( )
2
8
The Ht values for n = 3 are = 0,5556; = 0,8889 and = 0,5556.
3
= 0,5556 × (0,9956) + 0,8889 × (0,1164) + 0,5556 × (0,0074)
2
= 0,9912
Exercise Repeat example 1.7 using 6 point Gaussian Quadrature.
Answer: 1,0560
Method n Answer
1.2 Trapezoidal Rule 3 1,032352
1.3 Trapezoidal Rule 12 1,065845
1.5 Simpson’s rule 4 1,134461
Exercise Simpson’s Rule 6 1,074915
1.7 3-point Gaussian Quadrature 3 0,9912
Exercise 6-point Gaussian Quadrature 6 1,0560
Thus, the answers for the larger n values are converging for the three techniques. The Trapezoidal
Rule and Gaussian Quadrature under estimated the answer for n = 3 and Simpson’s Rule over
estimated the answer for n = 4. Note also that we added many strips for the Trapezoidal Rule (12),
but only 2 or 3 strips for Simpson’s rule and Gaussian Quadrature (6), to get a better estimate of the
answer. Every time n increases by 1, there is an additional function evaluation and so the calculation
will take longer and be more expensive.
Summary
Three different numerical integration techniques have been introduced: Trapezoidal Rule, Simpson’s
Rule and Gaussian Quadrature. You have worked through 7 examples and done two exercises. You
are now ready to use these techniques to approximate the integrals that you need to evaluate for
your reactor technology module.
Sources consulted