Guest User

MC Pi

a guest
Feb 5th, 2016
876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.28 KB | None | 0 0
  1. clc
  2. clear
  3.  
  4. n = 100000000; %number of MC trials to run
  5.  
  6. pts = bsxfun(@plus,rand(n,2), [-0.5,-0.5]); % generate n,2 points, centered at 0
  7.  
  8. pts_in = sqrt(pts(:,1).^2 + pts(:,2).^2) <= 0.5; % determine which points are in
  9.  
  10. pi = 4*sum(pts_in)/n % pi = ratio(area circle/area square)/r^2
Advertisement
Add Comment
Please, Sign In to add comment