% MC estimate of pi
clear all;
clc;
%%initialize random points
n=10000;
x=rand([1 n]);
y=rand ([1 n]);
%%compute using vectorization
radii = sqrt(x.^2+y.^2);
hits = sum(radii<=1);
misses = n-hits;
pi_mc = 4*(hits/n);
fprintf('\nUsing Vectorization:: HITS = %d, MISSES = %d, PI =
%f\n',hits,misses,pi_mc);
%%compute using a loop
c=0;
s=0;
i=1;
while i<=n
s=s+1;
if x(i)^2 +y(i)^2 <=1 % inside circle
c=c+1;
end
i+=1;
end
p=c/s;
pi_=p*4;
fprintf('\nUsing Loop:: HITS = %d, MISSES = %d, PI =
%f\n',c,s-c,pi_);
Get Answers For Free
Most questions answered within 1 hours.