Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Despite originally was a "true" Monte Carlo, now it just uses a regular matrix of points, so it can have a better consistency without any randomness.
- trucnt=0;
- totcnt=0;
- trucord=[0;0];
- falcord=[0;0];
- stepsize=0.002; %Set here the horizontal/vertical step size
- maxval=1; %Set here the maximum value on horizontal/vertical axis
- for xcord=0:stepsize:maxval;
- for ycord=0:stepsize:maxval
- r=sqrt( ycord ^2 + xcord ^2 );
- if r < maxval
- trucnt=trucnt+1;
- trucord(1:2,end+1)=[xcord;ycord];
- else
- falcord(1:2,end+1)=[xcord;ycord];
- end
- totcnt=totcnt+1;
- end
- end
- %Statistics:
- fprintf('Total/True: %u, %u\n', totcnt, trucnt)
- fprintf('Ratio: %0.7g%%\n', 100*trucnt/totcnt)
- fprintf('Pi: %0.6g\n', 4*trucnt/totcnt)
- fprintf('Error: %0.3g%%\n', 100*abs(4*trucnt/totcnt-pi)/pi)
- %Plotting:
- scatter(trucord(1,1:end),trucord(2,1:end),1,'g')
- hold on
- scatter(falcord(1,1:end),falcord(2,1:end),1,'b')
- hold off
- axis square
- title 'sqrt(x^2+y^2)<1'
- xlabel 'x'
- ylabel 'y'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement