# Matlab - hist plot with gaussian overlay

• Apr 14th 2010, 10:12 PM
2clients
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!!!
• Apr 15th 2010, 05:43 AM
CaptainBlack
Quote:

Originally Posted by 2clients
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
• Apr 15th 2010, 04:40 PM
2clients
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.
• Apr 15th 2010, 07:34 PM
CaptainBlack
Quote:

Originally Posted by 2clients
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
• Apr 15th 2010, 09:12 PM
2clients
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.
• Apr 15th 2010, 10:29 PM
2clients
Woops, bin width is 8/30.
• Apr 15th 2010, 10:55 PM
2clients
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);