nsample = 1000000;
X = rand(nsample,2);
InsideCircle = ( X(:,1).^2 + X(:,2).^2 ) <= 1;
ApproximatePI = zeros( size(InsideCircle) );
sumInsideCircle = 0.0;
for isample = 1:nsample
    sumInsideCircle = sumInsideCircle + InsideCircle(isample) ;
    ApproximatePI(isample) = 4.0 * sumInsideCircle / isample ;
end
disp( [ 'Approximate value of PI for '...
        , num2str(nsample) ...
        , ' samples: ' ...
        , num2str(ApproximatePI(end)) ...
      ] ...
    );
figure(); hold on; box on;
line( [ 1 nsample ] ...
    , [ pi pi ] ...
    , 'linestyle' , '--' ...
    , 'linewidth' , 3 ...
    , 'color' , 'black' ...
    );
plot( ApproximatePI(2:end) ...
    , 'linewidth' , 2 ...
    , 'color' , 'red' ...
    );
xlabel( 'Number of Sampled Points' ) ;
ylabel( 'Approximate Value of Pi' ) ;
set( gca , 'XScale' , 'log' );
saveas( gcf, 'approximatePI.png' );
