Search code examples
matlabgraphplotnormal-distribution

normalize 2 histogram and plot


I'm new in matlab and i tried to normalize two normal distributions according to How to normalize a histogram in MATLAB?, but i couldn't. Can someone tell me how to normalize the two normal distribution below:

[f,x]=hist(normrnd(25,2.5),50);%# create histogram from a normal distribution.
g=1/sqrt(2*pi)*exp(-0.5*x.^2);%# pdf of the normal distribution

figure(1)
bar(x,f/trapz(x,f));hold on
plot(x,g,'r');hold off

Thanks!


Solution

  • Two suggestions:

    1. Use randn instead of normrnd.
    2. Increase the number of numbers you are trying to generate.

    Code:

    [f,x]=hist(randn(10000,2.5),50);%# create histogram from a normal distribution.
    g=1/sqrt(2*pi)*exp(-0.5*x.^2);%# pdf of the normal distribution
    
    figure(1)
    bar(x,f/trapz(x,f));hold on
    plot(x,g,'r');hold off