data=normrnd(0,5,[1,500]);
%频数图
histogram(data,30);
%频率图
histogram(data,30,'Normalization','probability');%分布拟合方法一
histfit(data,30); %直方图&正态分布拟合
xlim([-16,16]); %x范围
%单独画拟合曲线(based on:频数直方图)
%分布拟合方法二
hold on
x=-16:0.001:16;
fx=normpdf(x,mean(data),std(data));%生成一个对应的正态分布概率密度函数
plot(x,fx*500,'g-');
值得学:get(histfit(r,20))
%分布拟合方法三 fitdist
r = normrnd(10,1,100,1); % 生成随机数
figure
hf=histfit(r,20); % 20表示将数据均分为20段
get(hf(1)) % properties of the histogram
get(hf(2)) % properties of the normal curve% 柱状图数据
x1=get(hf(1),'XData');
y1=get(hf(1),'YData');
% 拟合曲线数据
x2=get(hf(2),'XData'); %默认num=100
y2=get(hf(2),'YData');figure
subplot(211)
bar(x1,y1)
subplot(212)
plot(x2,y2)pd = fitdist(r,'Normal') % 获得拟合曲线的参数,均值和标准差,r必须要列向量,否则会报错!!!
参考:
两种拟合方式optional
拟合data&分布图