Interactive Educational Modules in
Scientific Computing

Monte Carlo Integration

This module illustrates the Monte Carlo method for approximating the integral of a function of one variable over a given interval. In the Monte Carlo method for approximating an integral, the integrand function is evaluated at n points randomly distributed in the domain of integration. The average of the resulting function values provides an estimate of the mean of the function, which is then multiplied by the size of the domain (e.g., the length of the interval of integration in one dimension) to estimate the integral. Because the error in this approximation of the integral converges to zero rather slowly, proportional to n−½, a very large number of sample points is required to attain good accuracy, and hence the Monte Carlo method is not competitive with conventional quadrature rules for approximating integrals in one or two dimensions. Its convergence rate is independent of the number of dimensions, however, so the Monte Carlo method is often used effectively for computing integrals in higher dimensions. This module illustrates how the Monte Carlo method for integration works in one dimension and gives a feel for its convergence rate.

The user first selects from a menu of integrand functions provided. The number of points at a time for which the integrand function will be evaluated can also be selected, if desired (1 is the default). The user then clicks Sample Function repeatedly to sample the integrand at an additional set of randomly chosen points. The sample points and corresponding sample function values are indicated in the graph, with the most recent samples drawn in a darker shade. Numerical values are printed below for the correct integral I, the cumulative number of samples N, and the current estimate of the integral Q along with its error. The error can be seen to converge very slowly to zero as the number of samples increases.

Reference: Michael T. Heath, Scientific Computing, An Introductory Survey, 2nd edition, McGraw-Hill, New York, 2002. See Section 8.4.4 and Chapter 13.

Developers: Evan VanderZee and Michael Heath