Curve fitting - how to find the residuals of new, unfitted data

1 view (last 30 days)
I fit a curve to the first 5 data points in a series using the 'cftool' GUI.
I plotted the entire data series against this curve.
How do I find the residuals of the additional data points that are not included in the curve fitting exercise - the last three data points seen in the figure??
If I can't do this in the GUI, how could I go about modifying the generated code to accomplish this (posted below)?
function createFit(a,b)
%CREATEFIT Create plot of data sets and fits
% 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: 1
% Number of fits: 1
% Data from data set "b vs. a":
% X = a:
% Y = b:
% Unweighted
% Auto-generated by MATLAB on 10-Oct-2014 09:08:49
% Set up figure to receive data sets and fits
f_ = clf;
set(f_,'Units','Pixels','Position',[473 113 688 485]);
% 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]);
hold on;
% --- Plot data that was originally in data set "b vs. a"
a = a(:);
b = b(:);
h_ = line(a,b,'Parent',ax_,'Color',[0.333333 0 0.666667],...
'LineStyle','none', 'LineWidth',1,...
'Marker','o', 'MarkerSize',6);
xlim_(1) = min(xlim_(1),min(a));
xlim_(2) = max(xlim_(2),max(a));
legh_(end+1) = h_;
legt_{end+1} = 'b vs. a';
% Nudge axis limits beyond data limits
if all(isfinite(xlim_))
xlim_ = xlim_ + [-1 1] * 0.01 * diff(xlim_);
set(ax_, 'XLim',[54.369999999999997, 118.63]);
% --- Create fit "fit 1"
% Apply exclusion rule "first5b1"
if length(a)~=8
error( 'GenerateMFile:IncompatibleExclusionRule',...
'Exclusion rule ''%s'' is incompatible with ''%s''.',...
'first5b1', 'a' );
ex_ = false(length(a),1);
ex_([]) = 1;
ex_ = ex_ | (a >= 105);
ok_ = isfinite(a) & isfinite(b);
if ~all( ok_ )
warning( 'GenerateMFile:IgnoringNansAndInfs',...
'Ignoring NaNs and Infs in data.' );
st_ = [0 0 0 0.12822827157509359 ];
ft_ = fittype('fourier1');
% 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''.',...
'fit 1', 'first5b1' );
cf_ = fit(a(ok_),b(ok_),ft_,'Startpoint',st_,'Exclude',ex_(ok_));
% 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_ = { 3124.3437555001524, -5837.8427424305373, 4687.779055680925, 0.020110003724853259};
% cf_ = cfit(ft_,cv_{:});
% Plot this fit
h_ = plot(cf_,'predobs',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} = 'fit 1';
if length(h_)>1
set(h_(2:end),'Color',[1 0 0],...
'LineStyle',':', 'LineWidth',1,'Marker','none');
legh_(end+1) = h_(2);
legt_{end+1} = 'Pred bnds (fit 1)';
% --- Finished fitting and plotting data. Clean up.
hold off;
% Display legend
leginfo_ = {'Orientation', 'vertical', 'Location', 'SouthEast'};
h_ = legend(ax_,legh_,legt_,leginfo_{:});
% Remove labels from x- and y-axes.

Accepted Answer

Mat on 10 Oct 2014
Edited: Mat on 10 Oct 2014
Sorry!! I have found a solution to this.
Incorporated the following into the code. This was the code for the residual plot from the GUI. I just deleted the part where it excluded the residuals for the data excluded from the curve. It gives me the residuals of all of the original data, but from this I can extract the relevant data. Not the most elegant solution, but gets me there.
res_ = b - cf_(a);
[x_,i_] = sort(a);
hold on;
h_ = line(x_,res_(i_),'Parent',ax2_,'Color',[1 0 0],...
'LineStyle','-', 'LineWidth',1,...
'Marker','.', 'MarkerSize',6);
hold on;
legrh_(end+1) = h_;
legrt_{end+1} = 'fit 1';

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!