approximate the value of π using the following algorithm based on geometric probability. This algorithm is an example of the so called monte carlo method
Show older comments
I have written the code for the estimation of pi but I don't understand ow to put hits and misses in my coding and how to ake the code loop.
Code:
clf
clear all
% MC estimate of pi
n=10000
x=rand([1 n])
y=rand ([1 n])
figure(1)
for i = 1:n
plot(x(i), y(i), 'b+')
hold on
end
% plot(x, y, 'r+')
c=0
s=0
for i=1:n
s=s+1
if x(i)^2 +y(i)^2 <=1 % inside circle
c=c+1
% hold on
else % else outside circle
plot (x(i), y(i), 'r+')
end
end
p=c/s
pi_=p*4
Accepted Answer
More Answers (0)
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!