| Curve Fitting Toolbox™ | ![]() |
| On this page… |
|---|
The Curve Fitting Tool is a graphical user interface that allows convenient, interactive use of Curve Fitting Toolbox functions, without programming. You can, however, access Curve Fitting Toolbox functions directly, and write programs that combine curve fitting functions with MATLAB functions and functions from other toolboxes. This allows you to create a curve fitting environment that is precisely suited to your needs.
Models and fits in Curve Fitting Tool are managed internally as curve fitting objects. Objects are manipulated through a variety of functions called methods. You can create curve fitting objects, and apply curve fitting methods, outside of Curve Fitting Tool.
For example, the following code, using Curve Fitting Toolbox methods, reproduces an analysis of the census data that was carried out interactively in Curve Fitting Tool in Interactive Curve Fitting.
Load and plot the data in census.mat:
load census plot(cdate,pop,'o') hold on

Create a fit options structure and a fittype object for the custom nonlinear model y = a(x–b)n, where a and b are coefficients and n is a problem-dependent parameter:
s = fitoptions('Method','NonlinearLeastSquares',...
'Lower',[0,0],...
'Upper',[Inf,max(cdate)],...
'Startpoint',[1 1]);
f = fittype('a*(x-b)^n','problem','n','options',s);Fit the data using the fit options and a value of n = 2:
[c2,gof2] = fit(cdate,pop,f,'problem',2)
c2 =
General model:
c2(x) = a*(x-b)^n
Coefficients (with 95% confidence bounds):
a = 0.006092 (0.005743, 0.006441)
b = 1789 (1784, 1793)
Problem parameters:
n = 2
gof2 =
sse: 246.1543
rsquare: 0.9980
dfe: 19
adjrsquare: 0.9979
rmse: 3.5994Fit the data using the fit options and a value of n = 3:
[c3,gof3] = fit(cdate,pop,f,'problem',3)
c3 =
General model:
c3(x) = a*(x-b)^n
Coefficients (with 95% confidence bounds):
a = 1.359e-005 (1.245e-005, 1.474e-005)
b = 1725 (1718, 1731)
Problem parameters:
n = 3
gof3 =
sse: 232.0058
rsquare: 0.9981
dfe: 19
adjrsquare: 0.9980
rmse: 3.4944Plot the fit results with the data:
plot(c2,'m') plot(c3,'c')

Curve fitting code can be assembled into an M-file by hand, as shown in Curve Fitting Objects and Methods, or it can be generated automatically from an interactive session in Curve Fitting Tool, as described in Generate an M-File. In practice, automatically generated code, giving the broad outlines of an analysis, can be combined with hand-coded refinements. This allows you to write functions that are customized to your data and your analysis, without having to write all of the basic programming structures.
For example, the following M-file was generated from a session in Curve Fitting Tool that imported the data from census.mat and fit a custom nonlinear model of the form y = a(x–b)3:
function myfit(cdate,pop)
%MYFIT Create plot of datasets and fits
% MYFIT(CDATE,POP)
% 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 "pop vs. cdate":
% X = cdate:
% Y = pop:
% Unweighted
%
% This function was automatically generated on 11-Sep-2007 01:07:11
% Set up figure to receive datasets and fits
f_ = clf;
figure(f_);
set(f_,'Units','Pixels','Position',[439.6 193.6 814.4 576.8]);
legh_ = []; legt_ = {}; % handles and text for legend
xlim_ = [Inf -Inf]; % limits of x axis
ax_ = axes;
set(ax_,'Units','normalized','OuterPosition',[0 0 1 1]);
set(ax_,'Box','on');
axes(ax_); hold on;
% --- Plot data originally in dataset "pop vs. cdate"
cdate = cdate(:);
pop = pop(:);
h_ = line(cdate,pop,'Parent',ax_,'Color',[0.333333 0 0.666667],...
'LineStyle','none', 'LineWidth',1,...
'Marker','.', 'MarkerSize',12);
xlim_(1) = min(xlim_(1),min(cdate));
xlim_(2) = max(xlim_(2),max(cdate));
legh_(end+1) = h_;
legt_{end+1} = 'pop vs. cdate';
% 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',[1788, 1992]);
end
% --- Create fit "fit 1"
ok_ = isfinite(cdate) & isfinite(pop);
if ~all( ok_ )
warning( 'GenerateMFile:IgnoringNansAndInfs', ...
'Ignoring NaNs and Infs in data' );
end
st_ = [0.51510504095942344 0.35210694524343056 ];
ft_ = fittype('a*(x-b)^3',...
'dependent',{'y'},'independent',{'x'},...
'coefficients',{'a', 'b'});
% Fit this model using new data
cf_ = fit(cdate(ok_),pop(ok_),ft_,'Startpoint',st_);
% Or use coefficients from the original fit:
if 0
cv_ = { 1.3594203554767276e-005, 1724.6959436137356};
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 1';
% Done plotting data and fits. Now finish up loose ends.
hold off;
leginfo_ = {'Orientation', 'vertical', 'Location', 'NorthEast'};
h_ = legend(ax_,legh_,legt_,leginfo_{:}); % create legend
set(h_,'Interpreter','none');
xlabel(ax_,''); % remove x label
ylabel(ax_,''); % remove y label
A quick look through the code shows that it has automatically assembled for you many of the Curve Fitting Toolbox curve fitting methods, such as fitoptions, fittype, fit, and plot.
A natural modification of the M-file would be to edit the function declaration at the top of the file to return the cfit object created by the fit, as follows:
function cf_ = myfit(cdate,pop)
You might also modify the code to produce a variety of different plots, or to return goodness-of-fit statistics.
Coding with curve fitting objects and methods is given complete treatment in Programmatic Curve Fitting.
![]() | Interactive Curve Fitting | User's Guide | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |