GIT repositories physdays / master figures / rdf_dh_createFit.m
master

Tree @master (Download .tar.gz)

rdf_dh_createFit.m @masterraw · history · blame

function f=createFit(r2,na2)
%CREATEFIT Create plot of data sets and fits
%   CREATEFIT(R2,NA2)
%   Creates a plot, similar to the plot in the main Curve Fitting Tool,
%   using the data that you provide as input.  You can
%   use this function with 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 data sets:  3
%   Number of fits:  1

% Data from data set "na2 vs. r2":
%     X = r2:
%     Y = na2:
%     Unweighted

% Data from data set "movingavg":
%     X = r2:
%     Y = na2:
%     Unweighted

% Data from data set "rlowess":
%     X = r2:
%     Y = na2:
%     Unweighted

% Auto-generated by MATLAB on 19-Nov-2013 11:29:01

% Set up figure to receive data sets and fits
f_ = clf;
figure(f_);
set(f_,'Units','Pixels','Position',[2 53 825 890]);
% Line handles and text for the legend.
legh_ = [];
legt_ = {};
% Limits of the x-axis.
xlim_ = [Inf -Inf];
% Axes for the plot.
ax_ = axes;
set(ax_,'Units','normalized','OuterPosition',[0 0 1 1]);
set(ax_,'Box','on');
axes(ax_);
hold on;

% --- Plot data that was originally in data set "na2 vs. r2"
r2 = r2(:);
na2 = na2(:);
% This data set does not appear on the plot. To add it to the plot, uncomment
% the following lines and select the desired color and marker.
%    h_ = line(r2,na2,'Color','r','Marker','.','LineStyle','none');
%    xlim_(1) = min(xlim_(1),min(r2));
%    xlim_(2) = max(xlim_(2),max(r2));
%    legh_(end+1) = h_;
%    legt_{end+1} = 'na2 vs. r2';

% --- Plot data that was originally in data set "movingavg"
sm_.y2 = smooth(r2,na2,5,'moving',0);
h_ = line(r2,sm_.y2,'Parent',ax_,'Color',[0.333333 0 0.666667],...
    'LineStyle','none', 'LineWidth',1,...
    'Marker','.', 'MarkerSize',12);
xlim_(1) = min(xlim_(1),min(r2));
xlim_(2) = max(xlim_(2),max(r2));
legh_(end+1) = h_;
legt_{end+1} = 'movingavg';

% --- Plot data that was originally in data set "rlowess"
sm_.y3 = smooth(r2,na2,40,'rlowess',0);
% This data set does not appear on the plot. To add it to the plot, uncomment
% the following lines and select the desired color and marker.
%    h_ = line(r2,sm_.y3,'Color','r','Marker','.','LineStyle','none');
%    xlim_(1) = min(xlim_(1),min(r2));
%    xlim_(2) = max(xlim_(2),max(r2));
%    legh_(end+1) = h_;
%    legt_{end+1} = 'rlowess';

% Nudge axis limits beyond data limits
if all(isfinite(xlim_))
    xlim_ = xlim_ + [-1 1] * 0.01 * diff(xlim_);
    set(ax_,'XLim',xlim_)
else
    set(ax_, 'XLim',[4.9800000000000004263, 9.059999999999998721]);
end

% --- Create fit "DH"

% Apply exclusion rule "6.2"
ex_ = (r2 <= 6.2);
fo_ = fitoptions('method','NonlinearLeastSquares','Lower',[0 0 0]);
ok_ = isfinite(r2) & isfinite(na2);
if ~all( ok_ )
    warning( 'GenerateMFile:IgnoringNansAndInfs',...
        'Ignoring NaNs and Infs in data.' );
end
st_ = [0.070116253362939917615 0.037423804606359301062 0.81509376029769176064 ];
set(fo_,'Startpoint',st_);
set(fo_,'Exclude',ex_(ok_));
ft_ = fittype('a*exp(-b*x)/x+c',...
    'dependent',{'y'},'independent',{'x'},...
    'coefficients',{'a', 'b', 'c'});

% Fit this model using new data
if sum(~ex_(ok_))<2
    % Too many points excluded.
    error( 'GenerateMFile:NotEnoughDataAfterExclusionRule',...
        'Not enough data left to fit ''%s'' after applying exclusion rule ''%s''.',...
        'DH', '6.2' );
else
    cf_ = fit(r2(ok_),na2(ok_),ft_,fo_);
end
% Alternatively uncomment the following lines to use coefficients from the
% original fit. You can use this choice to plot the original fit against new
% data.
%    cv_ = { 1127.1729936945791906, 1.2792135417896850491, 1.0510686611723416561};
%    cf_ = cfit(ft_,cv_{:});
f=cf_
% Plot this fit
h_ = plot(cf_,'fit',0.95);
set(h_(1),'Color',[1 0 0],...
    'LineStyle','-', 'LineWidth',2,...
    'Marker','none', 'MarkerSize',6);
% Turn off legend created by plot method.
legend off;
% Store line handle and fit name for legend.
legh_(end+1) = h_(1);
legt_{end+1} = 'DH';

% --- Finished fitting and plotting data. Clean up.
hold off;
% Display legend
leginfo_ = {'Orientation', 'vertical', 'Location', 'NorthEast'};
h_ = legend(ax_,legh_,legt_,leginfo_{:});
set(h_,'Interpreter','none');
% Remove labels from x- and y-axes.
xlabel(ax_,'');
ylabel(ax_,'');