Monte Carlo in Matlab
Loading...
13,645
Loading...
Uploader Comments (Maartenvb1987)
see all
All Comments (7)
-
If you have problems accessing the link above, simply copy and paste the following into Google and click on the first link that appears: /support/solutions/en/data/1-
1AP1X/index.html?product=ML&s olution -
Thanks for the quick reply. I was able to find this page earlier today where the m-file is laid out for a circle of radius 0.5 within a square of length 1. Really neat Monte Carlo simulation and easy-enough to grasp the concept of Monte Carlo Analysis :) "h t t p ://w w w .mathworks. com/support/solutions/en/data/
1-1AP1X/index.html?product=ML& solution=1-1AP1X" -
I guess the number of points inside the circle was counted and divided by the total number of points. The ratios should be very close to pi.
Loading...
I want to ask if you could shed some light on how exactly you coded Matlab to count the amount of dots inside the circle?
jmendoz2army 1 year ago
@jmendoz2army
Just generate random coordinates (x,y) for all points. Then these fall within the circle if it holds that x^2 + y^2 < 1 (for a circle with radius 1). Create an array with x and y-coordinates of all points and perform the elementwise operation x.^2 + y.^2. Then compute sum(x.^2 + y.^2 < 1) to see how many points obey this condition. Alternatively, you could do this in a loop, but the vectorized solution using logical expressions that I proposed is much faster.
Maartenvb1987 1 year ago
nice,
but can you explain that , how did you make it?
thanks
ecocharly100 2 years ago
Indeed, use a random number generator to generate point coordinates (x,y) in the plane. Then use the ratio of the number points within the circle and outside the circle to approximate pi, knowing Area Circle = Pi*R^2.
Maartenvb1987 2 years ago