我在對函式的隨機資料進行曲線擬合時遇到問題

這是我的代碼
N = 100;
mu = 5; stdev = 2;
x = mu stdev*randn(N,1);
bin=mu-6*stdev:0.5:mu 6*stdev;
f=hist(x,bin);
plot(bin,f,'bo'); hold on;
x_ = x(1):0.1:x(end);
y_ = (1./sqrt(8.*pi)).*exp(-((x_-mu).^2)./8);
plot(x_,y_,'b-'); hold on;
似乎我遇到了矢量大小問題,因為它給了我錯誤
Error using plot
Vectors must be the same length.
請注意,我簡化了 y_ 因為 mu 和標準偏差是已知的。
陰謀:

uj5u.com熱心網友回復:
首先對您的問題進行一些調整:
您不是在嘗試進行

最后一步:在直方圖上疊加概率密度函式
直方圖已經與概率密度函式一致,只需覆寫密度函式就足夠了:
x_ = linspace(min(r),max(r),100); y_ = (1./sqrt(2*sigma^2*pi)).*exp(-((x_-mu).^2)./(2*sigma^2)); plot(x_,y_,'b-');和
N = 150
和
N = 1500
N = 150.000和_Nbins = 50
如果出于某種晦澀的原因,您想使用舊的 hist() 函式
舊
hist()函式無法處理歸一化,因此您必須手動完成,通過對密度函式進行歸一化以適合您的直方圖:N = 1500; % rng('default') % For reproducibility mu = 5; sigma = 2; r = random('Normal',mu,sigma,1,N); Nbins = 50; [~,centers]=hist(r,Nbins); hist(r,Nbins); hold on % Width of bins Widths = diff(centers); x_ = linspace(min(r),max(r),100); y_ = N*mean(Widths)*(1./sqrt(2*sigma^2*pi)).*exp(-((x_-mu).^2)./(2*sigma^2)); plot(x_,y_,'r-');
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/489880.html下一篇:跨單獨mat檔案的平均雙精度陣列
