MATLAB: Monte Carlo Simulation Code

estimationmonte carlopisimulation

Hello! Can anyone help me out please!!!
I just started using matlab and I have to create a program that caculates the value of pi using the equation ? = ?/ ? 2 . First I need to calculate the value of A within a given radius of 4, and then im given this information: to estimate the area of a circle, we first note that we have a way to determine whether or not a point (x, y) is inside a circle that is centered at the origin and has radius r. Specifically, the distance from the origin to (x, y) is given by: ???????? = √? 2 + ? 2. If this distance is less than or equal to a radius r, then the point (x, y) is within the circle.
My program has to calculate the amount of sqaures that are within the circle because that eqauls the are. Can anyone help??

Best Answer

You need to set x and y to rand(), not zero.
You should know that all Monte Carlo simulations use random numbers, and nowhere in your program do you call rand().
You should also do the loop some number of times, not just once like r==4 would do
for counter = 1 : 10000000 % Try 10 million times.
x = r * rand
y = r * rand
% etc.
% DO NOT increment counter in the loop!
end