Solve MATLAB Question: Plot Normal Distribution on Histogram

  • Thread starter Thread starter coffeem
  • Start date Start date
  • Tags Tags
    Matlab
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 2K views
coffeem
Messages
91
Reaction score
0

Homework Statement



Plot a suitably scaled normal distribution on top of a histogram made with 100 random values.


The Attempt at a Solution



x = -2,9:0.1:2.9;

y = randn(100,1);

nd = exp(-x.^2/2)/sqrt(2*pi);

hist(y,x);

hold on;

plot(x,nd);

plot(-x,nd);

holf off;

This is not working. What am I doing wrong? Also how do I ensure that it is suitably scaled?

Thanks
 
Physics news on Phys.org
Hey
I think you have to change "plot(y,x)" to "plot(x,y)". Remove also "hold off;" (written as "holf off" but I suppose it is an type error). I don't understand why you first plot nd as a function of x and then as a function of -x. As nd is an even function you only need to plot it as a function of x.
 
Umm i'll try and explain my thoughts then.

I plotted fn as both a function and a function of -x so that I got your average bell shaped curve. for this I need to go back to -3 as well as +3 on the x-axis. When I tried changing it to just x it did not work sadly.

Also I need to plat a histogram so changing it to plot would not work. the only way I can get the historgram to work is by plotting (y,x). thanks
 
I have not got this far:

%%
x = 0:0.1:3;

nd = 10.*exp(-x.^2/2)/sqrt(2*pi);

plot(x,nd);

hold on;

plot(-x,nd);
%%
x = -2.9:0.1:2.9;

y = randn(100,1);

hist (y,x);

hold off

This will give me what I want. So all I need to do is suitably scale it. How do I do this? Is there some way I can tell how well it matches?

Thanks