Numerical Integration Trapezium Rule
Numerical Integration Trapezium Rule
What is integration?
Integration is the process of measuring the area under a function plotted on a graph. Why
would we want to integrate a function? Among the most common examples are finding the
velocity of a body from an acceleration function, and displacement of a body from a velocity
function. Throughout many engineering fields, there are (what sometimes seems like)
countless applications for integral calculus.
Sometimes, the evaluation of expressions involving these integrals can become daunting, if
not indeterminate. For this reason, a wide variety of numerical methods has been
developed to simplify the integral.
Here, we will discuss the trapezoidal rule of approximating integrals of the form
b
I f x dx
a
where
f (x) is called the integrand,
a lower limit of integration
b upper limit of integration
1
Numerical Analysis MATH351/352
the integral of that n th order polynomial. Integrating polynomials is simple and is based on
the calculus formula.
b
b n1 a n 1
a , n 1
n
x dx (1)
n 1
So if we want to approximate the integral
b
I f ( x)dx (2)
a
f ( x)dx f ( x)dx
a a
1
f ( x)dx f ( x)dx
a a
1
b
(a0 a1 x)dx
a
b2 a2
a0 (b a) a1 (5)
2
2
Numerical Analysis Trapezoidal Rule
But what is a 0 and a1 ? Now if one chooses, (a, f (a)) and (b, f (b)) as the two points to
approximate f (x) by a straight line from a to b ,
a f ( x)dx b a (b a) b a 2
(8b)
f (a) f (b)
(b a) (9)
2
a
f ( x)dx f1 ( x)dx
a
f (b) f (a)
b
f (a) ( x a) dx
a
ba
b
f (b) f (a) x 2
f (a) x ax
ba 2 a
f (b) f (a) b
2
a2
f (a)b f (a)a
ab a 2
ba 2 2
f (b) f (a) b a2
2
f (a)b f (a)a ab
ba 2 2
f (b) f (a) 1
f (a)b f (a)a b a
2
b a 2
f (a)b f (a)a f (b) f (a) b a
1
2
3
Numerical Analysis MATH351/352
1 1 1 1
f (a)b f (a)a f (b)b f (b)a f (a)b f (a)a
2 2 2 2
1 1 1 1
f (a)b f (a)a f (b)b f (b)a
2 2 2 2
f (a) f (b)
(b a) (11)
2
This gives the same result as Equation (10) because they are just different forms of writing
the same polynomial.
f ( x)dx Area of
a
trapezoid
1
(Sum of length of parallel sides)(Perpendicular distance between parallel sides)
2
f (b) f (a) (b a)
1
2
f (a) f (b)
(b a) (12)
2
where
ba
c1
2
4
Numerical Analysis Trapezoidal Rule
ba
c2
2
x1 a
x2 b
The interpretation is that f (x) is evaluated at points a and b , and each function
ba
evaluation is given a weight of . Geometrically, Equation (12) is looked at as the area
2
of a trapezoid, while Equation (13) is viewed as the sum of the area of two rectangles, as
shown in Figure 3. How can one derive the trapezoidal rule by the method of coefficients?
Assume
b
f ( x)dx c
a
1 f (a) c2 f (b) (14)
b b
Let the right hand side be an exact expression for integrals of 1dx and xdx , that is, the
a a
formula will then also be exact for linear combinations of f ( x) 1 and f ( x) x , that is, for
f ( x) a0 (1) a1 ( x) .
b
1dx b a c
a
1 c2 (15)
b2 a2
b
5
Numerical Analysis MATH351/352
Hence
ba ba
b
f ( x)dx
a
2
f (a)
2
f (b) (18)
f ( x)dx c
a
1 f (a) c2 f (b) (19)
Let the right hand side be exact for integrals of the form
b
a
a
0 a1 x dx
So
b
b
x2
a a0 a1 x dx a0 x a1 2
a
b2 a2
a0 b a a1 (20)
2
But we want
b
a
a
0 a1 x dx c1 f (a) c2 f (b) (21)
a
a
0 a1 x dx c1 a0 a1a c2 a0 a1b
6
Numerical Analysis Trapezoidal Rule
ba
c2 (24)
2
Therefore
b
f ( x)dx c
a
1 f (a) c2 f (b)
ba ba
f (a) f (b) (25)
2 2
Example 1
The vertical distance covered by a rocket from t 8 to t 30 seconds is given by
30
140000
x 2000 ln 9.8t dt
8 140000 2100t
a) Use the single segment trapezoidal rule to find the distance covered for t 8 to
t 30 seconds.
b) Find the true error, E t for part (a).
c) Find the absolute relative true error for part (a).
Solution
f (a) f (b)
a) I (b a) , where
2
a 8
b 30
140000
f (t ) 2000 ln 9.8t
140000 2100t
140000
f (8) 2000 ln 9.8(8)
140000 2100(8)
177.27 m/s
140000
f (30) 2000 ln 9.8(30)
140000 2100(30)
901.67 m/s
177.27 901.67
I (30 8)
2
11868 m
7
Numerical Analysis MATH351/352
8
Numerical Analysis Trapezoidal Rule
f ( a h ) f ( a 2h )
(a 2h) (a h)
2
f (a (n 2)h) f (a (n 1)h)
…………… a (n 1)h a (n 2)h
2
b a (n 1)h
f ( a ( n 1 ) h ) f (b )
2
f ( a ) f ( a h) f ( a h ) f ( a 2h)
h h ...................
2 2
f (a (n 2)h) f (a (n 1)h) f (a (n 1)h) f (b)
h h
2 2
f (a) 2 f (a h) 2 f (a 2h) ... 2 f (a (n 1)h) f (b)
h
2
h n1
f (a) 2 f (a ih ) f (b)
2 i 1
ba n1
f ( a ) 2 f (a ih ) f (b) (28)
2n i 1
9
Numerical Analysis MATH351/352
Example 2
The vertical distance covered by a rocket from t 8 to t 30 seconds is given by
30
140000
x 2000 ln 9.8t dt
8 140000 2100t
a) Use the two-segment trapezoidal rule to find the distance covered from t 8 to
t 30 seconds.
b) Find the true error, E t for part (a).
c) Find the absolute relative true error for part (a).
Solution
a) The solution using 2-segment Trapezoidal rule is
ba n 1
I f ( a ) 2 f (a ih ) f (b)
2n i 1
n2
a 8
b 30
ba
h
n
30 8
2
11
30 8 21
I f (8 ) 2 f (8 11i) f (30)
2(2) i 1
22
f (8) 2 f (19) f (30)
4
22
177.27 2(484.75) 901.67
4
11266 m
10
Numerical Analysis Trapezoidal Rule
Approximate
n Et t % a %
Value
1 11868 -807 7.296 ---
2 11266 -205 1.853 5.343
3 11153 -91.4 0.8265 1.019
4 11113 -51.5 0.4655 0.3594
5 11094 -33.0 0.2981 0.1669
6 11084 -22.9 0.2070 0.09082
7 11078 -16.8 0.1521 0.05482
8 11074 -12.9 0.1165 0.03560
Example 3
Use the multiple-segment trapezoidal rule to find the area under the curve
300 x
f ( x)
1 ex
from x 0 to x 10 .
Solution
Using two segments, we get
10 0
h 5
2
300(0)
f (0) 0
1 e0
300(5)
f (5) 10.039
1 e5
300(10)
f (10) 0.136
1 e10
ba n 1
I f ( a ) 2 f (a ih ) f (b)
2n i 1
11
Numerical Analysis MATH351/352
10 0 21
f ( 0) 2 f (0 5) f (10)
2(2) i 1
f (0) 2 f (5) f (10)
10
4
0 2(10.039) 0.136 50.537
10
4
So what is the true value of this integral?
10
300 x
0 1 e x dx 246.59
Making the absolute relative true error
246.59 50.535
t 100
246.59
79.506%
Why is the true value so far away from the approximate values? Just take a look at Figure 5.
As you can see, the area under the “trapezoids” (yeah, they really look like triangles now)
covers a small portion of the area under the curve. As we add more segments, the
approximated value quickly approaches the true value.
10
300 x
Table 2 Values obtained using multiple-segment trapezoidal rule for 1 e
0
x
dx .
Approximate
n Et t
Value
1 0.681 245.91 99.724%
2 50.535 196.05 79.505%
12
Numerical Analysis Trapezoidal Rule
Example 4
Use multiple-segment trapezoidal rule to find
2
1
I dx
0 x
Solution
We cannot use the trapezoidal rule for this integral, as the value of the integrand at x 0 is
infinite. However, it is known that a discontinuity in a curve will not change the area under
it. We can assume any value for the function at x 0 . The algorithm to define the function
so that we can use the multiple-segment trapezoidal rule is given below.
Function f (x)
If x 0 Then f 0
If x 0 Then f x^ (0.5)
End Function
Basically, we are just assigning the function a value of zero at x 0 . Everywhere else, the
function is continuous. This means the true value of our integral will be just that—true.
Let’s see what happens using the multiple-segment trapezoidal rule.
Using two segments, we get
20
h 1
2
f (0) 0
1
f (1) 1
1
1
f (2) 0.70711
2
ba n 1
I f ( a ) 2 f (a ih ) f (b)
2n i 1
20 2 1
f (0) 2 f (0 1) f (2)
2(2) i 1
13
Numerical Analysis MATH351/352
2
f (0) 2 f (1) f (2)
4
0 2(1) 0.70711
2
4
1.3536
So what is the true value of this integral?
2
1
0 x dx 2.8284
Thus making the absolute relative true error
2.8284 1.3536
t 100
2.8284
52.145%
2
1
Table 3 Values obtained using multiple-segment trapezoidal rule for
0 x
dx .
Approximate
n Et t
Value
2 1.354 1.474 52.14%
4 1.792 1.036 36.64%
8 2.097 0.731 25.85%
16 2.312 0.516 18.26%
32 2.463 0.365 12.91%
64 2.570 0.258 9.128%
128 2.646 0.182 6.454%
256 2.699 0.129 4.564%
512 2.737 0.091 3.227%
1024 2.764 0.064 2.282%
2048 2.783 0.045 1.613%
4096 2.796 0.032 1.141%
E1
(a h) a3 f " ( ), a a h
1 1
12
14
Numerical Analysis Trapezoidal Rule
h3
f " ( 1 )
12
E2
(a 2h) (a h)3 f " ( ), a h a 2h
2 2
12
h3
f " ( 2 )
12
.
.
.
Ei
(a ih ) (a (i 1)h)
3
f " ( i ), a (i 1)h i a ih
12
h3
f " ( i )
12
.
.
.
En1
a (n 1)h a (n 2)h3 f " ( ), a (n 2)h a (n 1)h
n 1 n 1
12
h3
f " ( n 1 )
12
En
b a (n 1)h3 f " ( ), a (n 1)h b
n n
12
h3
f " ( n )
12
Hence the total error in the multiple-segment trapezoidal rule is
n
Et Ei
i 1
h3 n
f " ( i )
12 i 1
(b a ) 3 n
12n 3
f " ( )
i 1
i
(b a ) 3 f " ( ) i
i 1
12n 2 n
n
f " (
i 1
i )
The term is an approximate average value of the second derivative
n
f " ( x), a x b .
Hence
15
Numerical Analysis MATH351/352
(b a ) 3
f " ( i )
Et i 1
12n 2 n
In Table 4, the approximate value of the integral
30
140000
8 2000 ln 140000 2100t 9.8t dt
is given as a function of the number of segments. You can visualize that as the number of
segments are doubled, the true error gets approximately quartered.
Approximate
n Et t % a %
Value
2 11266 -205 1.853 5.343
4 11113 -52 0.4701 0.3594
8 11074 -13 0.1175 0.03560
16 11065 -4 0.03616 0.00401
For example, for the 2-segment trapezoidal rule, the true error is -205, and a quarter of that
error is -51.25. That is close to the true error of -48 for the 4-segment trapezoidal rule.
Can you answer the question why is the true error not exactly -51.25? How does this
information help us in numerical integration? You will find out that this forms the basis of
Romberg integration based on the trapezoidal rule, where we use the argument that true
error gets approximately quartered when the number of segments is doubled. Romberg
integration based on the trapezoidal rule is computationally more efficient than using the
trapezoidal rule by itself in developing an automatic integration scheme.
16