function h = histfit(data,nbins)
%HISTFIT Histogram with superimposed fitted normal density.
%   HISTFIT(DATA,NBINS) plots a histogram of the values in the vector DATA.
%   using NBINS bars in the histogram. With one input argument, NBINS is set 
%   to the square root of the number of elements in DATA. 

%   H = HISTFIT(DATA,NBINS) returns a vector of handles to the plotted lines.
%   H(1) is a handle to the histogram, H(2) is a handle to the density curve.

%   B.A. Jones 2-14-95
%   Copyright (c) 1993-96 by The MathWorks, Inc.
%   $Revision: 2.3 $  $Date: 1996/02/21 18:34:00 $

[row,col] = size(data);
if min(row,col) > 1
   error('Requires a vector first argument.');
end

if row == 1
  row = col;
  data = data(:);
end

if nargin < 2
  nbins = ceil(sqrt(row));
end

[n,xbin]=hist(data,nbins);

mr = mean(data); % Estimates the parameter, MU, of the normal distribution.
sr = std(data);  % Estimates the parameter, SIGMA, of the normal distribution.

x=(-3*sr+mr:0.1*sr:3*sr+mr);% Evenly spaced samples of the expected data range.

[xx,yy] = bar(xbin,n);
hh = plot(xx,yy,'y-'); % Plots the histogram.      

np = get(gca,'NextPlot');    
set(gca,'NextPlot','add')    
                             
xd = get(hh,'Xdata'); % Gets the x-data of the bins.

rangex = max(xd) - min(xd); % Finds the range of this data.
binwidth = rangex/nbins;    % Finds the width of each bin.


y = normpdf(x,mr,sr);  
y = row*(y*binwidth);   % Normalization necessary to overplot the histogram.

hh1 = plot(x,y,'r-');         % Plots density

if nargout == 1
  h = [hh; hh1];
end

set(gca,'NextPlot',np) 
