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
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
2020-4-7
darova
2020-4-7
What about this?

nyasa
2020-4-10
darova
2020-4-10
Please?
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!