1. Monte Carlo integration.
The basic idea of Monte Carlo integration is to use probability to estimate the value of an integral. Suppose that you have a curve , which for simplicity we will assume is nonnegative, and you want to find the area underneath the curve from to . Choose some number M such that 0 <= <= for all x in the interval
. Pick n points at random in the rectangle bounded by on the x -axis and on the y -axis. Count how many of these points satisfy . If there are m such points, then estimate that the value of the integral is , where is the area of the rectangle containing the curve..
Since the purpose of the activities here is to apply technology to solve problems in mathematics, you do not have to pay close attention to the manner in which the function below is defined. What is important is that you know how to use the function to do Monte Carlo integration. That we will describe below after defining a Maple proc that will do the random selection of points and the evaluation of the Monte Carlo estimate of the integral for us.
local i,x,y, MM, hits, pointsin, rf, pointsout,pin,pout,pf,actual,estimate,err,percenterror;
> for i to n do
> if (y<=f(x)) then
> hits:=hits +1;
> pin:=plots[listplot](pointsin,style = POINT, symbol=CIRCLE,color=blue):
> pf:=plot(f('x'),'x'=a..b,'y'=0..M, color=GREEN):
> actual := evalf(int(f(z), z=a..b));
> estimate := evalf((b-a)*M*hits/n);
> err:= estimate-actual;
percenterror := err*100/actual:
printf("Actual Value: %f\n",actual);
printf("Estimated Value: %f\n",estimate);
> printf("Relative error: %f%%\n",percenterror);
Let us choose a simple function to perform Monte Carlo integration on. The example has the nice feature that we can compute the exact value of the integral by hand. Of course, in practice, we would want to use the Monte Carlo method on functions that we cannot integrate exactly. It should be clear to the reader that in the input to the mcint proc, f is the function, a and b are the left and right endpoints of the interval, M is a number which should be chosen by the reader to be greater than or equal to the maximum value of the function on the interval [a,b] and n is the number of points to use for the estimate. Because the routine will print a picture showing the points chosen, I would not recommend using too large a value for n.
Try executing the routine below several times. You will notice that the estimate of error changes each time.
Actual Value: .333333
Estimated Value: .330000
Relative error: -1.000000%
For each of the integrals below, do the following.
a) Define the function and the endpoints a and b in Maple.
b) Determine a reasonable value of M.
c) Apply the Monte Carlo routine to get an estimate of the integral.