
clc
close all
clear 


%% PUNTO a)
N = 2;
for i = 1:1000
 M(i) = mean(rand(1,N));
end

figure(1) 
hist(M) 

axis([0 1 0 250])
grid on
xlabel('Medie campionarie')
ylabel('f') 


%% PUNTO b) Legge dei grandi numeri 
N = [];
M = [];

N = [2,10];
for j = 1:length(N)
    for i = 1:1000
        M(i) = mean(rand(1,N(j)));
    end
    
    figure(2)
    subplot(2,1,j)
    hist(M)
    
    grid
    xlabel('Medie campionarie')
    ylabel('f')
    title(['N = ' num2str(N(j))]);
    axis([0 1 0 260])
end

%% PUNTO c) Teorema centrale del limite
N = [];
M = [];

N = [2,3,5,10];
for j = 1:length(N)
    for i = 1:1000
        M(i) = mean(rand(1,N(j)));
%         M(i) = mean(exprnd(0.1,[1,N(j)]));       
    end
    
    figure(3)
    subplot(2,2,j)
    hist(M)
    
    grid
    xlabel('Medie campionarie')
    ylabel('f')
    title(['N = ' num2str(N(j))]);
    axis([0 1 0 260])
end


%% bonus
N = [];
M = [];

N = [2,3,5,10];
for j = 1:length(N)
    for i = 1:1000
        M(i) = mean(rand(1,N(j)));
    end
    
    E = mean(M);
    V = std(M);
    x = 0:0.01:1;
    y = normpdf(x,E,V);

    figure(4)
    subplot(2,2,j)
    
    %NORMALIZZARE ISTOGRAMMA
    %opt 1
%     [num,cent] = hist(M);
    %num_norm = num./(1000*(cent(2)-cent(1)));
    %bar(cent,num_norm)
     
    %opt 2
    histogram(M,'Normalization','pdf')

    hold on
    plot(x,y,'r')
    grid
    xlabel('Medie campionarie')
    ylabel('f')
    title(['N = ' num2str(N(j))]);
    axis([0 1 0 5])

end
