%g_1(x) = exp(-randn) and g_2(x)=U
clear all
close all
clc

eps=1;
n=1;
while(eps>10^(-6))
	G_1(n) = exp(-rand);
%	g_1=exponential
%	g_2 = uniform RV
	X_2(n) = exponential_RV(rand);

	if (X_2(n)<1 && X_2(n) >0)
		G_2(n) =1;
	else
		G_2(n) =0;
	end;

meanG_1(n) = mean(G_1');
meanG_2(n) = mean(G_2');

varG_1(n) = mean((G_1'-meanG_1(n)).^2);

	if(n==1)
		eps=1;
	else
		eps=abs(varG_1(n)-varG_1(n-1));
	end;
n=n+1;
end

figure(1)
subplot(2,2,1)
plot(meanG_1,'r','LineWidth',2)
title('Convergence of expected value exp(-x) as a function of n')
ylabel('expected value exp(-x)');
axis([1 n 0 1])

subplot(2,2,3)
plot(meanG_2,'r','LineWidth',2)
title('Convergence of expected value Uniform RV as a function of n')
ylabel('expected value Uniform RV');
axis([1 n 0 1])

subplot(2,2,2)
hold on
subplot(2,2,4)
hold on
for i=1:n
	subplot(2,2,2)
	plot(i,var(meanG_1(1:i-1)'),'*');
	subplot(2,2,4)
	plot(i,var(meanG_2(1:i-1)'),'*');	
end
subplot(2,2,2)
ylabel('variance of expected value of exp(-x)');
xlabel('n');
hold off

subplot(2,2,4)
ylabel('variance of expected value of Uniform RV');
xlabel('n');
hold off


