% Two Chips and the Unit Circle [#10.33] % The big circle has radius 1. % If we generate x1 in [-1,1) and y1 in [-1, 1), % we will determine if (x1,y1) is in the big circle. % Similarly, we generate (x2,y2) and check. % If both centers are in the big circle, then how do we determine if the % two chips overlap? % % Initialize the overlap counter. overlap = 0; % You can change this value for the radius! % Note that MATLAB is case sensitive. Use upper case for "R". R = 0.5; % It turns out that we need the diameter squared! DiamSq = (2*R)^2; for i=1:1000 % Initialize x1 & y1 to stupid values. x1 = 10; y1 = 10; % Generate the random variates and test. while (x1^2 + y1^2 > 1) x1 = 2.*rand - 1.; y1 = 2.*rand - 1.; end; % Do the same for x2 & y2. x2 = 10; y2 = 10; while (x2^2 + y2^2 > 1) x2 = 2.*rand - 1.; y2 = 2.*rand - 1.; end; % Now both centers are in the big circle. % Write an appropriate test (if) and increment % the overlap counter. % HERE! % Use the constant DiamSq. This will speed up the program. end; % Divide by the total number of attempts. fprintf('The fraction overlapping is %6.4f.\n',overlap/1000.);