142
6 Computing Integrals and Testing Code
Looking back on the two different solutions, the specific implementation and the
general implementation, you should realize that implementing a general mathematical algorithm in a general function requires somewhat more abstract thinking, but
the resulting code can be used over and over again! Essentially, if you apply the
special-purpose style, you have to retest the implementation of the algorithm after
every change of the program.
The present integral problems result in short code. In more challenging engineering problems, the code quickly grows to hundreds and thousands of lines.
Without abstractions, in terms of general algorithms in general reusable functions,
the complexity of the program grows so fast that it will be extremely difficult to
make sure that the program works properly.
Another advantage of packaging mathematical algorithms in functions, is that a
function can be reused by anyone to solve a problem by just calling the function with
a proper set of arguments. Understanding the function’s inner details is strictly not
necessary to compute a new integral. Similarly, you can find libraries of functions
on the Internet and use these functions to solve your problems without specific
knowledge of every mathematical detail in the functions.
This desirable feature has its downside, of course: the user of a function
may misuse it, and the function may contain programming errors and lead to
wrong answers. Testing the output of downloaded functions is therefore extremely
important before relying on the results.
6.3 The Composite Midpoint Method
The Idea Rather than approximating the area under a curve by trapezoids, we can
use plain rectangles. It may sound less accurate to use horizontal lines and not skew
lines following the function to be integrated, but an integration method based on
rectangles (the midpoint method) is in fact slightly more accurate than the one
based on trapezoids! In the midpoint method, we construct a rectangle for every
sub-interval where the height equals the integrand f at the midpoint of the subinterval.
For the sake of comparison, we may repeat the hand calculation of
1
0 v(t)dt
in (6.7), but this time with the midpoint method. With four rectangles (Fig. 6.3) and
the same sub-intervals that we used with the trapezoidal method, [0, 0.2), [0.2, 0.6),
[0.6, 0.8), and [0.8, 1.0], we get
1
0
v(t)dt ≈ h 1 v
0 + 0.2
2
+ h 2 v
0.2 + 0.6
2
+ h 3 v
0.6 + 0.8
2
+ h 4 v
0.8 + 1.0
2
,
(6.18)
where h 1 , h 2 , h 3 , and h 4 are the widths of the sub-intervals, used previously with
the trapezoidal method and defined in (6.10)–(6.13).
With v(t) = 3t 2 e t 3 , the approximation becomes 1.632. Compared with the true
answer (1.718), this is about 5% too small, but it is better than what we got with
6 Computing Integrals and Testing Code
Looking back on the two different solutions, the specific implementation and the
general implementation, you should realize that implementing a general mathematical algorithm in a general function requires somewhat more abstract thinking, but
the resulting code can be used over and over again! Essentially, if you apply the
special-purpose style, you have to retest the implementation of the algorithm after
every change of the program.
The present integral problems result in short code. In more challenging engineering problems, the code quickly grows to hundreds and thousands of lines.
Without abstractions, in terms of general algorithms in general reusable functions,
the complexity of the program grows so fast that it will be extremely difficult to
make sure that the program works properly.
Another advantage of packaging mathematical algorithms in functions, is that a
function can be reused by anyone to solve a problem by just calling the function with
a proper set of arguments. Understanding the function’s inner details is strictly not
necessary to compute a new integral. Similarly, you can find libraries of functions
on the Internet and use these functions to solve your problems without specific
knowledge of every mathematical detail in the functions.
This desirable feature has its downside, of course: the user of a function
may misuse it, and the function may contain programming errors and lead to
wrong answers. Testing the output of downloaded functions is therefore extremely
important before relying on the results.
6.3 The Composite Midpoint Method
The Idea Rather than approximating the area under a curve by trapezoids, we can
use plain rectangles. It may sound less accurate to use horizontal lines and not skew
lines following the function to be integrated, but an integration method based on
rectangles (the midpoint method) is in fact slightly more accurate than the one
based on trapezoids! In the midpoint method, we construct a rectangle for every
sub-interval where the height equals the integrand f at the midpoint of the subinterval.
For the sake of comparison, we may repeat the hand calculation of
1
0 v(t)dt
in (6.7), but this time with the midpoint method. With four rectangles (Fig. 6.3) and
the same sub-intervals that we used with the trapezoidal method, [0, 0.2), [0.2, 0.6),
[0.6, 0.8), and [0.8, 1.0], we get
1
0
v(t)dt ≈ h 1 v
0 + 0.2
2
+ h 2 v
0.2 + 0.6
2
+ h 3 v
0.6 + 0.8
2
+ h 4 v
0.8 + 1.0
2
,
(6.18)
where h 1 , h 2 , h 3 , and h 4 are the widths of the sub-intervals, used previously with
the trapezoidal method and defined in (6.10)–(6.13).
With v(t) = 3t 2 e t 3 , the approximation becomes 1.632. Compared with the true
answer (1.718), this is about 5% too small, but it is better than what we got with
