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
Best Answer