% =========================================================================
%  ------  Raindrop Experiment - Computation of Number pi -------------
%
% This is an m-file that performs the raindrop experiment in order to
% compute the number pi. 
%
% =========================================================================

% Clears history and command window 
close all; clear all;

% number of points to be created 
m=10000;
% m=10;
% creation of 
x=rand(1,m);
y=rand(size(x));
%x(5)=0.5;
%y(5)=0;
theta=linspace(0,2*pi);
xc=0.5*cos(theta)+0.5;
yc=0.5*sin(theta)+0.5;
in = inpolygon(x,y,xc,yc);
s=sum(in);
P=4*s/m;

plot(xc,yc,x(in),y(in),'.b',x(~in),y(~in),'.r')
%h=title({['pi approximation = ',num2str(P)]});
xlabel(['m =',num2str(m)])
axis equal
disp([P,abs(P-pi)])
disp(['m = ',num2str(m)])