Curve Fitting Toolbox | ![]() ![]() |
Fit data using a library or custom model, a smoothing spline, or an interpolant
Syntax
fresult = fit(xdata,ydata,'ltype
') fresult = fit(xdata,ydata,'ltype
','PropertyName
',PropertyValue,...) fresult = fit(xdata,ydata,'ltype
',opts) fresult = fit(xdata,ydata,'ltype
',...,'problem
',values) fresult = fit(xdata,ydata,ftype,...) [fresult,gof] = fit(...) [fresult,gof,output] = fit(...)
Arguments
Description
fresult = fit(xdata,ydata,'
fits the data specified by ltype
')
xdata
and ydata
to the library model, interpolant, or smoothing spline specified by ltype
. The fit result is returned to fresult
. You can display the library fit type names with the cflibhelp
function. xdata
and ydata
cannot contain Inf
s or NaN
s. Additionally, only the real part of a complex value is used.
fresult = fit(xdata,ydata,'
fits the data using the options specified by ltype
','PropertyName
',
PropertyValue,...)
PropertyName
and PropertyValue
. You can display the fit options available for the specified library fit type with the fitoptions
function.
fresult = fit(xdata,ydata,'
fits the data using options specified by the fit options object ltype
',opts)
opts
. You create a fit options object with the fitoptions
function. This is an alternative syntax to specifying property name/property value pairs.
fresult = fit(xdata,ydata,'
assigns ltype
',...,'problem
',values)
values
to problem parameters. values
is a cell array with one element per parameter. Problem parameters are problem-dependent constants that you define as part of your model. See fittype
for more information on problem parameters.
fresult = fit(xdata,ydata,ftype,...)
fits the data to the fit type object specified by ftype
. You create a fit type object with the fittype
function.
[fresult,gof] = fit(...)
returns goodness of fit statistics to the structure gof
. The gof
structure includes the fields shown below.
[fresult,gof,output] = fit(...)
returns the structure output
, which contains information that is associated with the fitting procedure used. Supported fitting procedures include linear least squares, robust nonlinear least squares, and so on. Some information applies to all fitting procedures, while other information is relevant only for particular fitting procedures. For example, the information returned for nonlinear least squares fits is given below.
Remarks
For rationals and Weibull library models, the coefficient starting values are randomly selected in the range [0,1]. Therefore, if you perform multiple fits to a data set using the same equation, you might get different coefficient results due to different starting values. To avoid this situation, you should pass in a vector of starting values each time you fit, or define a specific state for the random number generator, rand
or randn
, before fitting.
For all other library models, optimal starting points are automatically calculated. These values depend on the data, and are based on model-specific heuristics.
Example
Fit the census data with a second-degree polynomial library model and return the goodness of fit statistics and the output structure.
Normalize the data and fit with a third-degree polynomial.
Fit the data with a single-term exponential library model.
Create a fit options object, and try to find a better fit by overriding the default starting points for the fit coefficients.
opts = fitoptions('exp1','Norm','on','start',[100 0.1]); [fit3,gof3,out3] = fit(cdate,pop,'exp1',opts);
Fit the data to a custom model that contains the problem parameter n
.
mymodel = fittype('a*exp(b*n*x)+c','problem','n'); opts = fitoptions(mymodel); set(opts,'normalize','on') [fit4,gof4,out4] = fit(cdate,pop,mymodel,opts,'problem',{2}); Warning: Start point not provided, choosing random start point.
The warning occurs whenever you fit data with a custom nonlinear model and do not provide starting points.
See Also
cflibhelp
, fitoptions
, fittype
![]() | feval | fitoptions | ![]() |