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

조회 수: 5 (최근 30일)
nyasa
nyasa 2020년 4월 6일
댓글: darova 2020년 4월 10일
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
nyasa
nyasa 2020년 4월 10일
not sure i follow but thanks for your reply anyway

댓글을 달려면 로그인하십시오.

채택된 답변

KSSV
KSSV 2020년 4월 7일
  댓글 수: 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개)

Community Treasure Hunt

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

Start Hunting!

Translated by