MATLAB: Histogram Fit: Scaling and offset

binschichi squaredchi²chi2gofcurve fittingfitfitdistgofgoodness of fithistogramMATLABpdfStatistics and Machine Learning Toolbox

I have one dimensional data (~12500 entries) with values reaching from ~135 to ~1150, yielding 3 peaks (see attachment).
Now I want to create a histogram showing the data distribution, as well as a fitting curve and a goodness of fit (chi squared) test.
Thus far I got the following:
load Data.mat
bins = round(sqrt(length(Data))); % Number of bins
[f, x] = hist(Data,bins); % Calculate histogram
pd = fitdist(x','Kernel'); % Calculate fit
y = pdf(pd,f); % Calculate pdf
figure(1)
dx = diff(x(1:2));
bar(x, f/sum(f*dx)); % Normalizing and plotting
hold on
plot(x,y,'Linewidth',2) % Plot fit
hold off
[h,p] = chi2gof(x,'CDF',pd,'Alpha',0.05); % Chi squared test
While my chi2gof test yields expected results (h=0 ; p = 0.9983) my plot doesn't look to well:
The scale of the fitting curve sems to be way off for all 3 peaks. Additionally I'd expect the curve to get a lot closer to 0 for very low and very high values.
Thanks in advance for any suggestions on how to improve/fix my code!

Best Answer

I think there are a couple of problems. Try this:
load Data.mat
bins = round(sqrt(length(Data))); % Number of bins
[f, x] = hist(Data,bins); % Calculate histogram
pd = fitdist(Data,'Kernel','Width',5); % Calculate fit
y = pdf(pd,x); % Calculate pdf of bin values
figure(1)
dx = diff(x(1:2));
bar(x, f/sum(f*dx)); % Normalizing and plotting
hold on
y = y / sum(y*dx);
plot(x,y,'Linewidth',2) % Plot fit
hold off
[h,p] = chi2gof(x,'CDF',pd,'Alpha',0.05); % Chi squared test