# how do I scale a histogram in Matlab to fit a pdf? [closed]

Math people:

I would post this in Computational Science Stack Exchange, but I think it is simply too easy to post there. People post easier questions than this here all the time. I want to superimpose the pdf of a normal distribution on a histogram to show that the numbers generating the histogram appear to be drawn from a near-normal distribution. I am using Matlab, and I'm using Matlab "hist" command with no options (at the moment). When I plot the histogram and the normal curve, they don't match. I want to scale the histogram (not the pdf) so that they agree. I'm not sure how to do it. I know I have to use the number of "samples" in the histogram but I'm not sure if I need to use the bin width, or even how to find what bin width Matlab is using. There is a way to draw histograms in Matlab where you tell Matlab what the bins are, and I can do that if I need to.

I got "histfit" to work. This plots the histogram together with the scaled normal curve that best fits the data. This is almost but not exactly what I want. I want to specify the parameters of the normal curve myself directly.

Below is the Matlab code. In the resulting histogram, the bars are way taller than the pdf, and the total area of the bars is much bigger than 1.

I would like a simple solution, one that does not involve GUI's, toolboxes, etc.

    N=2;
numsamples=100;
mat=rand(N, numsamples);
avg=sum(mat)/N; %produces row vector from averages of columns
hist(avg) %draws histogram
%Below is an attempt to graph the pdf of the normal distribution on top of
%the histogram.  If they are properly scaled, they should match up.  I
%don't know how to properly scale it.
hold on
x=-1:0.001:1;
y=normpdf(x,0.5,sqrt(1/12));
%numbers in vector avg should be roughly normally distributed
%w/ mean 0.5 and std.dev'n sqrt(1/12)

%Below is an attempt to graph the pdf of the normal distribution on top of
%the histogram.  If they are properly scaled, they should match up.  I
%don't know how to properly scale it

hold on
x=0:0.01:1; %row vector of values from 0 to 1 in increments of 0.01
y=normpdf(x,0.5,sqrt(1/12));%row vector of pdf of normal random variable
%w/ mean 0.5 and
%std. dev'n 1/sqrt(12)
%evaluated at the numbers in x
plot(x,y)

-

## closed as off-topic by Ramanujan, amWhy, rschwieb, no identity, Daniel Robert-NicoudOct 29 '13 at 18:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question is not about mathematics, within the scope defined in the help center." – Ramanujan, amWhy, rschwieb, no identity, Daniel Robert-Nicoud
If this question can be reworded to fit the rules in the help center, please edit the question.

Now that I think about it, I have to agree with you that this question is not really about mathematics, just about Matlab. The questions at Computational Science Stack Exchange are usually about interesting applied math questions. The Mathworks online documentation is frequently not that good. The answer I accepted came from StackOverflow. I just visited their site and it seems like a good place for Matlab questions. –  Stefan Smith Oct 30 '13 at 0:41

[c,x] = hist(avg);

trapz calculates the area under curve, by which we divide.
At least in this case, Scilab's hist command is easier to use: it has an explicit normalization flag (true/false), which is true by default. You only have to decide on the number of bins with Scilab: hist(n,avg)