% Copyright 2009 The MathWorks, Inc.
function myfit(hours,sulfate)
%MYFIT Create plot of datasets and fits
% MYFIT(HOURS,SULFATE)
% Creates a plot, similar to the plot in the main curve fitting
% window, using the data that you provide as input. You can
% apply this function to the same data you used with cftool
% or with different data. You may want to edit the function to
% customize the code and this help message.
%
% Number of datasets: 1
% Number of fits: 1
% Data from dataset "sulfate vs. hours":
% X = hours:
% Y = sulfate:
% Unweighted
%
% This function was automatically generated
% Set up figure to receive datasets and fits
f_ = clf;
figure(f_);
legh_ = []; legt_ = {}; % handles and text for legend
xlim_ = [Inf -Inf]; % limits of x axis
ax_ = subplot(2,1,1);
ax2_ = subplot(2,1,2);
set(ax2_,'Box','on');
legrh_ = []; legrt_ = {};
set(ax_,'Box','on');
axes(ax_); hold on;
% --- Plot data originally in dataset "sulfate vs. hours"
hours = hours(:);
sulfate = sulfate(:);
h_ = line(hours,sulfate,'Parent',ax_,'Color',[0.333333 0 0.666667],...
'LineStyle','none', 'LineWidth',1,...
'Marker','.', 'MarkerSize',12);
xlim_(1) = min(xlim_(1),min(hours));
xlim_(2) = max(xlim_(2),max(hours));
legh_(end+1) = h_;
legt_{end+1} = 'sulfate vs. hours';
% Nudge axis limits beyond data limits
if all(isfinite(xlim_))
xlim_ = xlim_ + [-1 1] * 0.01 * diff(xlim_);
set(ax_,'XLim',xlim_)
set(ax2_,'XLim',xlim_)
end
% --- Create fit "fit 3"
st_ = [19.5850335451 -0.3042418560216 ];
ft_ = fittype('power1' );
% Fit this model using new data
cf_ = fit(hours,sulfate,ft_ ,'Startpoint',st_);
% Or use coefficients from the original fit:
if 0
cv_ = {16.68316791478, -0.2626481521025};
cf_ = cfit(ft_,cv_{:});
end
% Plot this fit
h_ = plot(cf_,'fit',0.95);
legend off; % turn off legend from plot method call
set(h_(1),'Color',[1 0 0],...
'LineStyle','-', 'LineWidth',2,...
'Marker','none', 'MarkerSize',6);
legh_(end+1) = h_(1);
legt_{end+1} = 'fit 3';
res_ = sulfate - cf_(hours);
[x_,i_] = sort(hours);
axes(ax2_); hold on;
h_ = line(x_,res_(i_),'Parent',ax2_,'Color',[1 0 0],...
'LineStyle','-', 'LineWidth',1,...
'Marker','.', 'MarkerSize',6);
axes(ax_); hold on;
legrh_(end+1) = h_;
legrt_{end+1} = 'fit 3';
hold off;
legend(ax_,legh_, legt_);
legend(ax2_,legrh_, legrt_);