%script to show convergence of monte carlo integration.
%by Nasser Abbasi. For math 502 CSUF probability and statistics 10/18/07

%----- configuration section ----------%

close all;
number_of_simulations     = 2000;
s = 1:1:number_of_simulations;

a = 0;         %left end of integration interval
b = 2*pi;     %right end of integration interval
domain_length = (b-a);

function_to_integrate = @cos;
randn('state',010101);

%------ algorithm start --------------------%

x  = a + (b-a).*rand(number_of_simulations,1);
cs = domain_length*cumsum(feval(function_to_integrate,x))./s';
plot(cs,'.','MarkerSize',5);
hold on;
plot(s,quad(function_to_integrate,a,b),'r');
axis([1 number_of_simulations -2 1]);
title('Showing convergence of monteCarlo integration to real value');
xlabel('Number of random variables');
ylabel('integral value');
legend('MonteCarlo','Exact');




