0
votes

I am trying to add Gaussian fit to the histogram in MATLAB, but I do not know how to apply the fit to one particular peak only.

http://postimg.org/image/phms61rdh/ first plot

http://postimg.org/image/sindrn8er/ second plot

I am also posting part of the code I have been using:

    data_Rb = (importdata('path\name.txt'));
    counts_Rb = data_Rb.data(:,3);
    figure
    hist(counts_Rb, nbins);
    xlim([0 120]);
    title('Rubidium');
    histfit(counts_Rb,1000);

    pd=fitdist(counts_Rb(:),'normal')
    x=0:0.001:120;  
    PDF=pdf(pd,x); %PDF is a vector of y values: it's our fit
    PDF=PDF/max(PDF); %nor
    y=ylim;
    PDF=PDF*y(2);
    hold on
    plot(x,PDF,'r-','LineWidth',2);
    hold off

These two blocks give me two different Gaussians as shown in the first picture. I do not understand why they fit data so badly: is it because of the tail on the RHS?

In the second plot I need to apply Gaussian fit to the last peak only. How should I do it?

And finally, after applying the fit, fit results are outputed onto the screen. Is there a function to save them into an array or so for later use?

Thanks in advance!

1
You should look into MATLAB's Gaussian mixture model, described on mathworks.com/help/stats/gmdistribution.fit.html - s.bandara

1 Answers

0
votes

Regarding your last question, see How can I access the fitted distribution parameters from the HISTFIT function in Statistiics Toolbox ?

There is a workaround for this isssue by making a copy of the HISTFIT function and modifying the code to pass out the "pd" variable. One way to do this is to change the "h" output on the first line of the code to "varargout" and add the following to the end of the file:

h = [hh; hh1];
argout={h,pd};
if nargout > length(argout)
error('Too many output arguments.');
end
[varargout{1:nargout}]=argout{1:nargout};