I want to know how to model the script for calculating pi using Monte-Carlo simulation with using logical vectors
I already know the method using for/if, but does not know the method with logical vector
Please let me know how to do it
the below script is for the method using for/if
clearclcinside = 0;nmax = 10000;for n = 1:nmax x = rand; y = rand; x1=x-0.5; y1=y-0.5; if sqrt(x1^2+y1^2) <= 0.5 plot(x1,y1,'b.'); inside = inside + 1; else plot(x1,y1,'r.'); end if ( mod(n,1000) == 0 ) pi = 4 * inside / n; fprintf('%8.4f\n',pi); end hold on; end hold off;pi = 4 * inside / nmax;fprintf('%8.4f\n',pi);
Best Answer