Creative Commons License Copyright © Michael Richmond. This work is licensed under a Creative Commons License.

Numerical Integration: Basic

Suppose that you are given a function f(x) and asked to integrate it. Sometimes, you can find an exact answer analytically:
         f(x) = 2*x         integral = x

         f(x) = 1/x         integral = ln(x)

Frequently, however, an analytic solution doesn't exist; in that case, it's necessary to integrate numerically. What does that mean?

If we draw a graph showing f(x) as a function of x,

then the integral is just the area under the curve:

How can we calculate this area? There are many ways, varying in sophistication, efficiency, and accuracy.

The easiest way is to use a Newton-Cotes method of order zero. That's a fancy way to say,

Graphically, that means

As the figure shows, this method only approximates the true area under function. One can improve the method's accuracy by making the intervals smaller, but that will take longer.

How small should the intervals be?

The smaller you make the pieces, the better the approximation. How small should they be? It depends on your purpose. One way to gauge the accuracy of the integration is to

  1. calculate the sum with pieces of a given size,
  2. cut the size in half calculate the sum again
  3. compare the two sums

One can use the absolute size of the change, or the fractional size of the change, as an indication of the accuracy of the integration. For example, suppose that a numerical integration of some function with different sizes for the pieces yields

                                 absolute change  fractional change
    size of slice      sum        from previous    from previous
        1.0           523.4            
        0.5           537.8         + 14.4             0.0268          
        0.25          540.3         +  2.5             0.0046
	0.1           541.9         +  1.6             0.0030
	0.05          542.4         +  0.5             0.0009

The difference between the sums with slices of width 0.1 and 0.05 is less than 0.1%, which probably means that the latest value is within about 0.1% of the true (but unknown) integral.

Given a termination criterion based on some desired accuracy, one can do an integral by iterating: start with wide slices, calculate the sum under the curve, then decrease the slices in width until the absolute or fractional change in the sum satisfies the criterion.

Last modified 3/30/2001 by MWR.

Creative Commons License Copyright © Michael Richmond. This work is licensed under a Creative Commons License.