CS 1 Fall 2004

Lab 4: Supplemental material


Expansion functions for numerically approximating integrals

Hopefully you all know that the integral of f is the area under the curve of f, and we define the integral between a and b as:

limit diagram

When numerically approximating integrals, we cannot make dx->0, so instead we use very small dx's.

The simplest thing to do is to do just this, and sum up the dx*f(x) rectangle around a set of discrete points: a, a+dx, a+2*dx, ... b-dx, b.

This will differ from the real integral as the value of f(x) varies from the f(x) value you use across the distance dx.

As a result, people often consider various approximations which attempt to do a better job. For example, instead of computing the area of the rectangle dx*f(x), maybe we should compute the area of the trapezoid from f(x-dx) to f(dx), assuming that f is mostly linear between x-dx and x:

  A = dx * (f(x-dx)+f(x))/2

Or maybe we should compute the area centered around x, which is more accurate (here we sum up two trapezoids):

   A = dx/2 * (f(x-dx/2) + f(x))/2 + dx/2 * (f(x) + f(x+dx/2))/2
     = dx/4 * (f(x-dx/2) + 2*f(x) + f(x+dx/2))

Simpson's rule (exercise 1.29 in SICP) is just a variant on this where we use the quadratic expansion:

    A = dx/6 * (f(x-dx/2) + 4*f(x) + f(x+dx/2))

When you add up this area across a set of points offset by dx, the -dx/2 term from one point will overlap with the +dx/2 point from the next. This gives us the funny set of alternating 2's and 4's which you saw in exercise 1.29 (and explains why the very end points are weighted by 1 instead of 2).

So, in general, we can define a class of integrators by defining the expansion rule around the discrete points used in the integration. Simpson's rule is one such expansion, the trapezoidal rule is another, and the simple rectangle rule is another.

To provide this generality, the make-integrator function we ask you to write in (lab 4, partC, 2a) should take in an expansion function as its first argument. As you see from the assignment, that expansion function itself takes in two values: x and dx. The example given in the problem is for Simpson's rule, and you should be able to see it is just the Scheme translation of the Simpson's rule equation stated above.

Your make-integrator function should produce an integrator based on such an expansion function. For part C you will write the expansion function in Scheme for the simple rectangular expansion rule which was our first approximation for a discrete integral.