% MCAS Question 9
% MCASq9.m
% Solves question 9, 10th grade math, 2001 MCAS
% Written by E Gallagher 9/5/01
% Eugene.Gallagher@umb.edu
% Last revised 9/15/01
hold on; % no graphs will be erased;
theta = 0:pi/50:2*pi;
% first draw a circle of radius 3, centered at 0
r=3;
x = r * cos(theta) + 0;
y = r * sin(theta) + 0;
plot(x, y);axis('square');
% Now draw a circle of radius 1, centered at 0
r=1;
x = r * cos(theta) + 0;
y = r * sin(theta) + 0;
plot(x, y);
% Now fill this circle with yellow;
h =fill(x,y,'y');
hold on
figure(gcf)
% Modify dart throwing code from Nahin's pisim.m
rand('state',100*sum(clock))  %set new seed for generator;
trials=0;              % initialize trials
success=0;             % initialize successes
tic
for i=1:1000;
   x=rand*6-3;         % generate a uniform random number on the interval -3 3
   y=rand*6-3;         % generate a uniform random number on the interval -3 3
   r=sqrt(x^2+y^2);    % compute Euclidean distance from origin to dart;
   if r<3
       trials=trials+1;      % don't count darts outside the outer circle
       plot(x,y,'.r','MarkerSize',4);
       if r<1                % is dart inside quarter-circle region?
         success=success+1;  % yes
       end                   %yes
   end
end
figure(gcf)
p_est=success/trials
disp('Deviation from the expected value of 1/9')
1/9-p_est
t=toc;
fprintf('The program took %3.1f seconds to run\n',t)
S=sprintf('The program took %3.1f seconds to run\n',t);S