Monte Carlo Statistics is a very popular and wel-used approach for studying many
ID: 3887944 • Letter: M
Question
Monte Carlo Statistics is a very popular and wel-used approach for studying many problems. The essential idea is that expected value of the variable by taking a simple mean of the samples. Along the way, one can get a reasonably good approximation of the complete distribution by taking a large enough number samples. Consider a square defined by -2 S (x, y)S2 with a cirele inside it with radius R-2. The area of the circle is 'obviously) R2 and the area of the square is 4R2. however, let's assume that you don't know . Instead, try to approximately calculate by estimating the area ofthe circle using a large number of random points (x, y) and using the ratio of points inside and outside the circle to be a rough indication of ratios ofthe areas { = 4 X AcircielAsquare. Give your answer with S significant digits, and give your answer when using both 100 and 10000 random points. Hints by randomly sampling a statistical variable, one can estimate the Generate random poits(x, y)that are uniformly distributed over the square i.e, that are equally likely to fall anywhere within that square). And then count how many of those points fall within the circle. In Matlab, you can generate uniformly distributed random points using the function rand(N,1), which creates an array of N random numbers between 0 and 1. We need x and y between -2 and 2, so we need to do x-4*(rand(N,1)-0.5) y-4'(rand(N,1)-0.5)Explanation / Answer
N=10000; %you can take random points to be 100 in second case
CIR_IN=0; % variable to hold no. of points INSIDE THE CIRCLE
CIR_OUT=0; %variable to hold no. of points OUTSIDE THE CIRCLE
x=4*(rand(N,1)-0.5); %random values Array for -2<=x<=2
y=4*(rand(N,1)-0.5); % random values Array for -2<=y<=2
for i=1:N % loop running for 1 to 10000
val=x(i)^2+y(i)^2-4; % equation of circle centered at origin is x^2+y^2=r^2
if(val>0) % if value comes out to be positive this means point is outside circle
CIR_OUT++; % if point lies outside count it as outside_circle point
else
CIR_IN++; % otherwise count this point as lying on the circle or inside_circle point
end %if endes here
end % for ends here
answer=4*CIR_IN/(CIR_OUT+CIR_IN); % the answer would be 4*inside points(circle area) divided by total points(area of square)
fprintf('the value of pi so calculated is %0.4f',answer);% printing the value of answer
%****** IF YOU LIKE MY GUIDANCE DO APPRECIATE US SBY COMMENTING AND DO LIKE PLEASE