function [fitresult, gof] = cell_growth_rate_fit(im_time_scale, totalNumCells)
%CREATEFIT(IM_TIME_SCALE,TOTALNUMCELLS)
%  Create a fit.
%
%  Data for 'gr_rate_fit' fit:
%      X Input : im_time_scale
%      Y Output: totalNumCells
%  Output:
%      fitresult : a fit object representing the fit.
%      gof : structure with goodness-of fit info.
%
%  See also FIT, CFIT, SFIT.

%  Auto-generated by MATLAB on 05-Nov-2019 23:13:25


%% Fit: 'gr_rate_fit'.
[xData, yData] = prepareCurveData( im_time_scale, totalNumCells );

% Set up fittype and options.
ft = fittype( 'a*exp(x*b)+c', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [0.001 0.01   -2000];
opts.StartPoint = [0.843381020491254 0.071480866304485 0.529009537187819];
opts.Upper = [1000  0.1    2000];

% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );

% Plot fit with data.
fig = figure( 'Name', 'cell growth rate fit' );
h = plot( fitresult, xData, yData );
legend( h, 'totalNumCells vs. time', 'cell growth rate fit', 'Location', 'NorthEast' );
txt = ['Average cell growth rate: ' num2str(1/fitresult.b) ' min' 'fit a*exp(x*b)+c'];
text(4,4000,txt);
txt = ['fit growth_rate = a*exp(x*b)+c'];
text(4,3500,txt);
% Label axes
xlabel time[min]
ylabel totalNumCells
grid on
saveas(fig,'cell growth rate fit.png');


