Normalized histogram with gaussian fit

13 ビュー (過去 30 日間)
Miquel Vega Paredes
Miquel Vega Paredes 2021 年 4 月 8 日
コメント済み: Alan Stevens 2021 年 10 月 29 日
Hi, I know how to make an histogram and make it so it is normalized according to the probability
histogram(x, nbits, 'Normalization','probability')
and also how to fit a gaussian curve to the histogram: histfit (x)
But if I use the command histfit I don't know how to normalize it according to the probability.
I would like to have both, a normalized histogram with the probability, that also has the plot of the gaussian distribution that fits to my data set.
I hope my question is clear and that you can help me :)

採用された回答

Alan Stevens
Alan Stevens 2021 年 4 月 8 日
Here's an example showing one possibility:
gaussian = @(x, m,s) exp(-0.5*((x-m)/s).^2)/(s*sqrt(2*pi));
mu = 5; % Arbitrary mean
sigma = 1; % arbitrary standard deviation
x = mu + sigma*randn(1,400); % Arbitrary random normal data
h = histogram(x);
dh = h.BinWidth;
lo = min(x);
hi = max(x);
dxx = (hi-lo)/100;
xx = linspace(lo,hi,101);
pdf = gaussian(xx, mu, sigma);
scalefactor = sum(h.Values*dh)/(trapz(pdf)*dxx);
pdf = scalefactor*pdf;
hold on
plot(xx,pdf)
  3 件のコメント
studentmatlaber
studentmatlaber 2021 年 10 月 29 日
@Alan Stevens I ran the code you wrote. But shouldn't the normalized histogram be between 0 and 1?
Alan Stevens
Alan Stevens 2021 年 10 月 29 日
The AREA under the curve should be 1.

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by