Results 1 to 7 of 7

Math Help - Matlab - hist plot with gaussian overlay

  1. #1
    Junior Member
    Joined
    Aug 2008
    Posts
    38

    Matlab - hist plot with gaussian overlay

    Hi, I'm having problems with this:

    1. Use the random # generator, randn, and an initial state of 0 to create a vector of 1000 normally distributed random numbers.

    a. Plot a histogram of these numbers with 30 bins, appropriately normalized such that the total area under the bars is 1. Set the axes to run from -4 to +4 on the x-axis and from 0 to 0.5 on the y-axis.

    b. Overlay on top of this normalized histogram a plot of the continuous gaussian distribution.

    c. Calculate the mean and standard deviation of this vector, and add these numbers to the title of the plot.

    So far, I've only been able to do this:

    randn('state',0)
    Y = randn(size(1000));
    binedges=linspace(-4,4,30)
    histc(y,binedges)

    and don't really know where to go from here.

    Your help is greatly and forever appreciated!!!
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by 2clients View Post
    Hi, I'm having problems with this:

    1. Use the random # generator, randn, and an initial state of 0 to create a vector of 1000 normally distributed random numbers.

    a. Plot a histogram of these numbers with 30 bins, appropriately normalized such that the total area under the bars is 1. Set the axes to run from -4 to +4 on the x-axis and from 0 to 0.5 on the y-axis.
    >>help randn

    >>help hist

    Now tell us where you are having problems using these.

    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    Aug 2008
    Posts
    38
    I understand the 'help' capability, but I'm brand new to programming and Matlab. It didn't help.

    For the first part, part (a)

    I've gotten this far:

    randn('state',0)
    data = randn([1,1000]);
    binedges = linspace(-4,4,30);
    n = histc(data,binedges);

    But I don't understand how to normalize the data (so the area is 1), plot the data, or to set the x and y-axes

    Thank you.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by 2clients View Post
    I understand the 'help' capability, but I'm brand new to programming and Matlab. It didn't help.

    For the first part, part (a)

    I've gotten this far:

    randn('state',0)
    data = randn([1,1000]);
    binedges = linspace(-4,4,30);
    n = histc(data,binedges);

    But I don't understand how to normalize the data (so the area is 1), plot the data, or to set the x and y-axes

    Thank you.
    You are going to plot a bar-graph of the n data and you want to to normalise it to have area 1. So you will plot N=(n./sum(n))./h, where h is the bin width.

    (Alternativly search for the Octave file histn.m)

    CB
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Junior Member
    Joined
    Aug 2008
    Posts
    38
    Thank you! That makes much more sense.

    So I define the bin width is (30/8):


    randn('state',0)
    Y = randn(size(1000));
    binedges=linspace(-4,4,30);
    histc(y,binedges);
    h=(8/30);
    N=(n./sum(n))./h;
    bar(N)

    It doesn't look the integral will be 1...

    Lastly, how would I make the mean at zero and adjust the axes for -4 to 4 for x , 0 - 0.5 for y?

    Cheers.
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Junior Member
    Joined
    Aug 2008
    Posts
    38
    Woops, bin width is 8/30.
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Junior Member
    Joined
    Aug 2008
    Posts
    38
    I think I figured out the first part:

    randn('state',0)
    data = randn([1,1000]);
    binedges = linspace(-4,4,30);
    n = histc(data,binedges);
    binwidth = (8/30);
    N = (n./sum(n))./binwidth;
    bar(binedges,N);
    hold on;
    axis([-4 4 0 0.5])


    but now I need to somehow overlay a plot of the continuous gaussian:

    First thoughts are to break the equation into these pieces:

    fac = 1/(sigma*sqrt(2*pi));
    num = -(x-xo).^2;
    den = 2*sigma*sigma;
    out = (fac)*exp(num/den);
    Last edited by 2clients; April 15th 2010 at 11:20 PM.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. 3D plot of z = (Matlab)
    Posted in the Math Software Forum
    Replies: 4
    Last Post: July 14th 2011, 08:22 PM
  2. loglog plot in MATLAB
    Posted in the Math Software Forum
    Replies: 2
    Last Post: March 31st 2011, 07:43 PM
  3. matlab plot function
    Posted in the Math Software Forum
    Replies: 4
    Last Post: May 26th 2010, 07:03 AM
  4. matlab 3D-4D graphic plot
    Posted in the Math Software Forum
    Replies: 6
    Last Post: October 25th 2008, 02:53 AM
  5. MatLab Plot
    Posted in the Math Software Forum
    Replies: 1
    Last Post: August 2nd 2008, 10:59 AM

Search Tags


/mathhelpforum @mathhelpforum