I have produced a histogram with normal distribution fit using the histfit
function in MATLAB and included error bars as follows:
h = histfit(data);
[nelements,bincenters] = hist(data);
hold on
err = sqrt(nelements);
errorbar( bincenters, nelements, err);
hold off
where "data" is a vector of m data points to make a distribution, and "dataerror" is a vector of the errors on each of those points. The normal distribution derived from histfit
results in a standard deviation that may be different from the average error bar in "dataerror." I want to visually inspect this difference by over plotting a separate normal distribution that has a mean found with avg = mean(data);
and a standard deviation given by my known errors stdev = mean(dataerrors);
. I do this in the following way:
x = sort(data);
y = exp(- 0.5 * ((x - avg) / stdev) .^ 2) / (stdev * sqrt(2 * pi));
plot(x, y,'k')
When I do this, the mean and standard deviation look close, but the height of the distribution does not match since the histfit
distribution is not normalised, but the second distribution is. I can do ad hoc corrections by inventing a scaling factor or the y
points, but I need this to be automated, so the heights need to be known and scaled accordingly.
If I can somehow measure the height of the distribution from histfit
, I can scale the second distribution to match. Alternatively, I could fit the second distribution in a way that allows me to input a known mean and standard deviation, and then fit for the height of the distribution.
The ideal method would be to fit the second normal distribution to the data separately. In this way, the height may be different, but the fit will be better for the tails of the distribution.
So the questions are:
How can these distributions be scaled to match in height? OR How can I separately fit a Gaussian distribution (using height as a parameter) with known mean and standard deviation to this data in a similar way to histfit?