how to plot pi estimation using two for loops

N = 10000;
count=0;
for i=1:N
x=rand();
y=rand();
if x^2+y^2<=1
count=count+1;
else
%for j=1:M
% A(i,j)=I+J;
%end
end
end
count = 4*count; % circle iteration
value = count/N % total iterations
error = pi-value
%plot(value,count)
%plot(x,pi,x,error)
%figure (1)
%scatter (x,y,'r')

回答 (1 件)

Ashraf Mekki
Ashraf Mekki 2023 年 1 月 31 日

0 投票

2 件のコメント

Ashraf Mekki
Ashraf Mekki 2023 年 1 月 31 日
i want to get this result
Torsten
Torsten 2023 年 2 月 1 日
I don't understand the figure.
If the number of Monte-Carlo iterations to approximate pi increases, the estimated value for pi should more and more approach the true value of pi. According to your graphics, the number of iterations doesn't influence the exactness of the approximation. That's unlogical.

サインインしてコメントする。

質問済み:

2023 年 1 月 31 日

コメント済み:

2023 年 2 月 1 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by