approximate the value of π using the following algorithm based on geometric probability. This algorithm is an example of the so called monte carlo method
古いコメントを表示
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
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Graphics Performance についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!