% teorema del limite centrale % sommiamo nv variabili indipendenti con distribuzione uniforme ottenute % con np prove e confrontiamo la frequenza relativa della distribuzione % della loro somma con la previsione teorica, vale a dire con una gaussiana % di media e varianza note dalla teoria clear all; close all; clc; rng('default') np=10000; a=0.2; b=3.0; media = (a+b)/2; varianza = (b-a)^2/12; nbins=101; ub=b+0.3; %sqrt(varianza) + media; lb=a-0.3; %-sqrt(varianza) + media; x=linspace(lb,ub,nbins); dx=(ub-lb)/(nbins-1); updf = unifpdf(x,a,b); figure; hold on; plot(x,updf,'r') title('Uniform') nv=1; y=unifrnd(a,b,nv,np); sy=sum(y,1)/nv; freq=hist(sy,x)/(dx*np); dist=1/sqrt(2*pi*varianza/nv)*exp(-(x-media).^2/(2*varianza/nv)); figure; hold on; bar(x,freq); plot(x,dist,'r') title('n=1') nv=2; y=unifrnd(a,b,nv,np); sy=sum(y,1)/nv; freq=hist(sy,x)/(dx*np); dist=1/sqrt(2*pi*varianza/nv)*exp(-(x-media).^2/(2*varianza/nv)); figure; hold on; bar(x,freq); plot(x,dist,'r') title('n=2') nv=5; y=unifrnd(a,b,nv,np); sy=sum(y,1)/nv; freq=hist(sy,x)/(dx*np); dist=1/sqrt(2*pi*varianza/nv)*exp(-(x-media).^2/(2*varianza/nv)); figure; hold on; bar(x,freq); plot(x,dist,'r') title('n=5') nv=30; y=unifrnd(a,b,nv,np); sy=sum(y,1)/nv; freq=hist(sy,x)/(dx*np); dist=1/sqrt(2*pi*varianza/nv)*exp(-(x-media).^2/(2*varianza/nv)); figure; hold on; bar(x,freq); plot(x,dist,'r') title('n=30') nv=50; y=unifrnd(a,b,nv,np); sy=sum(y,1)/nv; freq=hist(sy,x)/(dx*np); dist=1/sqrt(2*pi*varianza/nv)*exp(-(x-media).^2/(2*varianza/nv)); figure; hold on; bar(x,freq); plot(x,dist,'r') title('n=50') nv=100; y=unifrnd(a,b,nv,np); sy=sum(y,1)/nv; freq=hist(sy,x)/(dx*np); dist=1/sqrt(2*pi*varianza/nv)*exp(-(x-media).^2/(2*varianza/nv)); figure; hold on; bar(x,freq); plot(x,dist,'r') title('n=100') nv=500; y=unifrnd(a,b,nv,np); sy=sum(y,1)/nv; freq=hist(sy,x)/(dx*np); dist=1/sqrt(2*pi*varianza/nv)*exp(-(x-media).^2/(2*varianza/nv)); figure; hold on; bar(x,freq); plot(x,dist,'r') title('n=500')