how to do a while loop to estimate pi using monte carlo method

hi guys..sorry very new to matlab. i have been trying to write a while loop code to estimate pi using monte carlo method so it displays number of hits and with a tollerance of about 1e-4. please find attached the code
function [hits]=mypi_tolarance(x,y)
hits=0;
for n=1:10000
x=rand;
y=rand;
hit=x^2+y^2<=1;
hits=hits+hit;
end
while 1
pi_est=hits/10000*4;
if pi_est<3.14||pi_est>3.142
break
end
disp(pi_est)
disp(hits)
end

5 件のコメント

James Tursa
James Tursa 2020 年 4 月 6 日
What is your question? Can you give us the actual wording of your assignment so we know what type of while loop is expected? Use pi in your code instead of 3.14
nyasa
nyasa 2020 年 4 月 7 日
thank you so much ..so the assignment is to write a code that uses the monte carlo method to estimates pi and has a tolerance of 1e-4 and displays the final hits and the estimate as a function code.
darova
darova 2020 年 4 月 7 日
What about this?
nyasa
nyasa 2020 年 4 月 10 日
not sure i follow but thanks for your reply anyway
darova
darova 2020 年 4 月 10 日
Please?

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

 採用された回答

KSSV
KSSV 2020 年 4 月 7 日

0 投票

1 件のコメント

nyasa
nyasa 2020 年 4 月 7 日
wow thats a fancy one....but i was just looking at polishing up mine please.really appreciate your reply though.thanks

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2020 年 4 月 6 日

コメント済み:

2020 年 4 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by