0

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!

Community
  • 1
  • 1
blitzstat
  • 1
  • 3
  • 1
    What do you mean "you couldn't?" What exactly is wrong, and what problems are you encountering? We're here to solve problems, not guess at to what reasons your code may be wrong. – rayryeng Oct 27 '14 at 01:01
  • Sorry. I used this code: `[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` But it gives me a strange thing. I dont know waht i am doing wrong. – blitzstat Oct 27 '14 at 01:13
  • 1
    Please update your post with this code. – rayryeng Oct 27 '14 at 01:15

1 Answers1

1

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
lakshmen
  • 28,346
  • 66
  • 178
  • 276