Académique Documents
Professionnel Documents
Culture Documents
Mon 10000 25 09 18
Mon 10000 25 09 18
ins_pt=0;n_total=10000;
rad=2; figure
for i=1:n_total
x=rad*rand;
y=rad*rand;
z= sqrt((x-1)^2+(y-1)^2);
if(z<=1)
plot(x,y,'r.')
ins_pt=ins_pt+1;
else
plot(x,y,'g.')
end
hold on
end
espi=4*ins_pt/n_total;
disp('estimated value of pi')
disp(espi)
hold off
estimated value of pi
3.1404
1
Published with MATLAB® R2018b