Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: MATLAB Question?

  1. Oct 10, 2008 #1
    1. The problem statement, all variables and given/known data

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

    3. The attempt at a solution

    x = -2,9:0.1:2.9;

    y = randn(100,1);

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


    hold on;



    holf off;

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

  2. jcsd
  3. Oct 10, 2008 #2
    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.
  4. Oct 10, 2008 #3
    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
  5. Oct 10, 2008 #4
    I have not got this far:

    x = 0:0.1:3;

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


    hold on;

    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?

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook