I am trying to fit a curve to a histogram of some data and found that Weibull distributions do a really good job. I would like to report the Weibull parameters that fit my data but am having issues. I can use histfit() to plot the histogram and the best fit line together, but it doesn't return the correct parameters, so I used fitdist() to get them. However, when I try to plot my Weibull distribution from these parameters obtained from fitdist(), it never looks very good. Here is some sample code I use to do this with fitdist():
[counts1,bins1] = hist(data,numel(data)^0.5);
weibull_p1 = fitdist(data,'weibull');
a_ = weibull_p1.A;
b_ = weibull_p1.B;
weibull_fit1 = (b_/a_)*(bins1./a_).^(b_-1).*exp((-bins1./a_).^b_);
So I plugged in the locations of the bin centers as x-values to compute the y-values of the Weibull function. However, the fit is terrible and is not even close to what histfit() plots. I thought histfit might use a different method of fitting, but I opened it in the editor and discovered that all it does is call fitdist() exactly as I did. The difference is apparently how it calculates y-values (copied from the histfit() function):
xd = get(hh,'Xdata');
rangex = max(xd(:)) - min(xd(:));
binwidth = rangex/nbins;
area = n * binwidth;
y = area * pdf(pd,x);
Why doesn't Matlab use the equation for the Weibull distribution to calculate y-values? Am I doing this wrong? I want to be able to report the Weibull parameters that best fit my data so that anyone can plug them in and get the same best fit curve that I found. But when I try to check this myself, it doesn't match up.
Thank you!