Consider the function graph in figure
. Suppose we wish
to know the area between the x axis, the fat lines labeled a and b,
and the graph. Determining the area under the graph of a function for some
specific interval is called integrating a function.
Since engineers had to solve this kind of problem before computers were available,
mathematicians have studied it extensively. For a small class of functions,
it is indeed possible to determine the area exactly. For the other cases,
mathematicians have developed several methods to determine close estimates.
Since these methods involve lots of mechanical calculations, they are
natural candidates for computer functions.
A general integration function must consume three inputs: a, b, and the function f. The fourth part, the x axis, is implied. This suggests the following contract:
;; integrate : (number -> number) number number -> number ;; to compute the area under the graph of f between a and b (define (integrate f a b) ...)
Kepler suggested one simple integration method. It consists of three steps:
Exercise 23.4.1
Develop the function integrate-kepler. It computes the area under
some the graph of some function f between left and
right using Kepler's rule. Solution
Another simple method is to think of the area as a sequence of many small
rectangles. Each rectangle is as tall as the function graph in, say, the
middle of the rectangle. Figure
shows two examples. By
adding up the area of the rectangles, we get a good estimate at the area
under the graph. The more rectangles we consider, the closer the estimate
is to the actual area.
Let us agree that R stands for the number of rectangles that we wish to consider. To determine how large these rectangles are, we need to figure out how large their sides are. The length of the side on the x axis is the length of the interval divided by the number of rectangles:
For the height of the rectangle, we need to determine its midpoint and then the value of f at the midpoint. The first midpoint is clearly at a plus half of the width of the rectangle, that is, if
the area is
where W stands for width and S for step from now on.
To get from the rectangle starting at a to the next one on the
right, we must add the width of one rectangle. That is, the next midpoint
(called
in figure
) is at
the third one at
and so on. The following table explains the three sequences that are involved in the usual manner:
In the second row, M stands for midpoint. The first rectangle has index 0, the last one R - 1.
Using this sequence of rectangles, we can now determine the area under the graph as a series:
Develop the function integrate. It computes the area under some the graph of some function f between left and right using the rectangle-series method.
Use test cases for f, a, and b for which one can
determine the area exactly and easily by hand, for example, (define (id x) x). Compare the results with those of integrate from
exercise
.
Make R a top-level constant:
;; R : number of rectangles to approximate integral (define R ...)Test integrate on sin and increase R gradually from 10 to 10000. What happens to the result? Solution