Monte Carlo

3 views (last 30 days)
Alicia
Alicia on 3 May 2012
I'm trying to figure out how to solve a monte carlo experiment problem. The prompt is write a program that conducts a monte carlo experiment to approximate the average distance between two points in the circle: x^2 + y^2 =1.
To solve this approximation, generate n=2000 random pairs of points (xi,yi) and (vi, wi) in the circle. Then compute 1/n SUM [(xi-vi)^2+(yi-wi)^2]^1/2.

Answers (1)

Image Analyst
Image Analyst on 3 May 2012
Use rand() to get a random angle. Then have a radius of 1, and use pol2cart to get the x,y coordinate. Then do it again for the second point. They use hypot() to calculate the distance, or use the SUM formula you were given. Sum the distance into an accumulator variable. Plot both points. Be sure to use hold on so that later pairs of points don't blow away your prior pairs. Then you'll be able to see all the points being built up on your unit circle. Put all that into a loop to run 2000 times. Then divide the sum of the distances by 2000 to get the average distance.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!