%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% mcint1d.m
%
% demonstration: Monte Carlo integration in 1D
%
% Wei Cai
% for LLNL CCMS Summer Institute Lectures Aug.1-3, 2005
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%integrate g(x)=sqrt(1-x^2) over [0,1] to estiamte pi/4
%using random variables uniformly distributed over [0,1]
%plot the error as a function of N (number of random variables)

MAXN = 1e6;
%N = [10,20,50,100,200,500,1000];
N = [1:MAXN]';
nplot = [1:100,100:10:1000,1000:100:MAXN]';

x=rand(MAXN,1);
g=sqrt(1-x.^2);
G=cumsum(g)./[1:MAXN]';
G0=pi/4;
err=abs(G-G0);
sig1=sqrt(0.05);
errest=sig1./sqrt(N);
%plot(N,G);
loglog(nplot,err(nplot),'b.',nplot,errest(nplot),'r-');

fs=18;
set(gca,'FontSize',fs);
xlabel('N');
ylabel('error');
