This section describes how to use Curve Fitting Toolbox™ functions from the command-line or to write programs for curve and surface fitting applications.
The Curve Fitting app 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 the Curve Fitting app 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 the Curve Fitting app.
In MATLAB programming, all workspace variables are objects of
a particular class. Familiar examples of MATLAB classes
You can also create custom MATLAB classes, using object-oriented
Methods are functions that operate exclusively on objects of a particular class. Data types package together objects and methods so that the methods operate exclusively on objects of their own type, and not on objects of other types. A clearly defined encapsulation of objects and methods is the goal of object-oriented programming.
Curve Fitting Toolbox software provides you with new MATLAB data types for performing curve fitting:
fittype — Objects allow
you to encapsulate information describing a parametric model for your
data. Methods allow you to access and modify that information.
Two subtypes of
fittype, for curves and surfaces.
Objects capture information from a particular fit by assigning values
to coefficients, confidence intervals, fit statistics, etc. Methods
allow you to post-process the fit through plotting, extrapolation,
cfit is a subtype of
fittype methods. In other words, you can apply
cfit methods are used exclusively with
As an example, the
islinear, which determines if a model
is linear or nonlinear, would apply equally well before or after a
fit; that is, to both
On the other hand, the
confint, which, respectively, return
fit coefficients and their confidence intervals, would make no sense
if applied to a general
fittype object which describes
a parametric model with undetermined coefficients.
Curve fitting objects have properties that depend on their type,
and also on the particulars of the model or the fit that they encapsulate.
For example, the following code uses the constructor methods for the
two curve fitting types to create a
f = fittype('a*x^2+b*exp(n*x)') f = General model: f(a,b,n,x) = a*x^2+b*exp(n*x) c = cfit(f,1,10.3,-1e2) c = General model: c(x) = a*x^2+b*exp(n*x) Coefficients: a = 1 b = 10.3 n = -100
are evaluated at predictor values
feval. You can call
using the following functional syntax:
y = cfun(x) % cfit objects; y = ffun(coef1,coef2,...,x) % fittype objects;
Curve fitting methods allow you to create, access, and modify
curve fitting objects. They also allow you, through methods like
to perform operations that uniformly process the entirety of information
encapsulated in a curve fitting object.
The methods listed in the following table are available for
fittype objects, including
|Fit Type Method||Description|
Get input argument names
Get fit category
Get coefficient names
Get dependent variable name
Evaluate model at specified predictors
Get formula string
Get independent variable name
Determine if model is linear
Get number of input arguments
Get number of coefficients
Get problem-dependent parameter names
Set model fit options
Get name of model
The methods listed in the following table are available exclusively
|Curve Fit Method||Description|
Get coefficient values
Get confidence intervals for fit coefficients
Get prediction intervals
Get problem-dependent parameter values
A complete list of methods for a curve fitting object can be
obtained with the MATLAB
f = fittype('a*x^2+b*exp(n*x)'); methods(f) Methods for class fittype: argnames dependnames fittype islinear probnames category feval formula numargs setoptions coeffnames fitoptions indepnames numcoeffs type
Note that some of the methods listed by
not appear in the tables above, and do not have reference pages in
the Curve Fitting
Toolbox documentation. These additional methods
are generally low-level operations used by the Curve Fitting app,
and not of general interest when writing curve fitting applications.
There are no global accessor methods, comparable to
fittype objects. Access is limited
to the methods listed above. This is because many of the properties
fittype objects are derived from other properties,
for which you do have access. For example,
f = fittype('a*cos( b*x-c )') f = General model: f(a,b,c,x) = a*cos( b*x-c ) formula(f) ans = a*cos( b*x-c ) argnames(f) ans = 'a' 'b' 'c' 'x'
You construct the
giving the formula, so you do have write access to that basic property
of the object. You have read access to that property through the
formula method. You also have read access
to the argument names of the object, through the
argnames method. You don't, however,
have direct write access to the argument names, which are derived
from the formula. If you want to set the argument names, set the formula.
The surface fit object (
sfit) stores the
results from a surface fitting operation, making it easy to plot and
analyze fits at the command line.
are a subclass of
fittype objects, so they inherit
all the same methods of
fittype listed in Curve Fitting Methods.
sfit objects also provide methods exclusively
sfit objects. See
One way to quickly assemble code for surface fits and plots into useful programs is to generate a file from a session in the Curve Fitting app. In this way, you can transform your interactive analysis of a single data set into a reusable function for command-line analysis or for batch processing of multiple data sets. You can use the generated file without modification, or edit and customize the code as needed. See Generate Code and Export Fits to the Workspace.