6760, Math 21a, Fall 2009
Week 8 Problems F, Math 21a, Multivariable Calculus
Monte Carlo and Riemann Integration
Course head: Oliver Knill
Office: SciCtr 434
Problem F: In this exercise, we compute the double integral of the function f(x,y) = ( 1+ x e-y3)1/2 over the rectangle [0,1] x [0,1]. There is no expression using known functions and numbers to get the answer, but we do it numerically:
1) Find a Riemann Sum with dx=1/M, dy =1/M, where M is an integer like 100. Call this sum "RiemannSum".
2) Find a Monte Carlo Sum by choosing two random numbers x,y in [0,1] and compute the mean f(x,y) over 10'000 experiments. Call this average of random variables "MonteCarlo".
3) Find a numerical integral on the square [0,1] x [0,1]. Call this integral "Numerical"
4) Find the fraction |RiemannSum-Numerical|/|MonteCarlo-Numerical| This tells us by which factor the Riemann Sum error is larger than the Monte Carlo Sum error. To get full credit, we need to have the Mathematica lines and the numerical answers. Below is Mathematica sample code for f(x,y) = exp(x*y) + sqrt(x): Note that all built in functions like Exp[x]=E^x are capitalized.
 M=100; X:=Random[]; Y:=Random[]; 

 f[x_,y_]:=N[Exp[x*y] + Sqrt[x]]; 

 RiemannSum =Sum[f[x,y],{x,0,1,1/M},{y,0,1,1/M}]/M^2

 MonteCarlo=Sum[f[X,Y],{M^2}]/M^2

 Numerical =NIntegrate[ f[x,y],{x,0,1},{y,0,1}]

 Abs[RiemannSum-Numerical]/Abs[MonteCarlo-Numerical]
If you should not have Mathematica yet, you can do the problem without Mathematica with a calculator

1) Take M=6 and add the sum of M2=36 numbers
RiemannSum = [ f(1/M,1/M) + f(2/M,1/M) + ....+ f(M/M,M/M) ]/M2 
This number "RiemannSum" is a Riemann approximation of the integral.
2) Now use a random number generator to generate 36 random numbers x(i),y(i) in the interval [0,1]. Then produce the average:
  MonteCarlo = [f(x(1),y(1)) + ... + f(x(36),y(36))]/36
You can do this with a friend. One generates random numbers, the other punches in the numbers.
3) Finally, use the integrator in the calculator to find the integral
  Numerical = integrate  f(x,y) , (x,0,1), (y,0,1) 
on the interval [0,1] x [0,1].
4) Find |RiemannSum-Numerical|/|MonteCarlo-Numerical| as before

Questions and comments to knill@math.harvard.edu
Math21b | Math 21a | Fall 2009 | Department of Mathematics | Faculty of Art and Sciences | Harvard University