1. 程式人生 > >產生高斯分佈的隨機變數

產生高斯分佈的隨機變數

for j=1:1:1000
    aa(j) = sqrt(-2*log(rand(1,1)))*cos(2*pi*rand(1,1));
end
bb=randn(1,1000) ;

figure(1)
histfit(aa);
normplot(aa);
figure(2)
 y=aa;
x=linspace(min(y),max(y),100) ;
yy=hist(y,x) ;
yy=yy/length(y); %計算各個區間的個數
bar(x,yy) %畫出概率密度分佈圖

figure(3)
histfit(bb);

figure(4)
normplot(bb);

 [muhat,sigmahat,muci,sigmaci]=normfit(aa);
 muhat
 sigmahat
 

參考: