| Contents | Index |
fitobject = fit(x,y,fitType)
fitobject = fit([x,y],z, fitType)
fitobject = fit(..., Name, Value,...)
fitobject = fit(x,y,libname,options)
fitobject = fit(...,'problem',vals)
fitobject = fit(x,y,fitType,...,'Weight',
Weights)
[fitobject,gof] = fit(...)
[cfun,gof,output] = fit(...)
fitobject = fit(x,y,fitType) fits the data in x and y with the library model, anonymous function or fittype object specified by fitType.
x must be a matrix with either one (curve fitting) or two (surface fitting) columns. For surface fitting, if your data is in separate vectors, then you can use the syntax: fitobject = fit([x,y],z, fitType).
y must be a column vector with the same number of rows as x.
x and y cannot contain Inf or NaN. Only the real parts of complex data are used in the fit.
fitType can be a string, anonymous function, or a fittype object specifying the model to fit. If a string, you can specify library model names. String choices include:
LIBNAME DESCRIPTION 'poly1' Linear polynomial curve 'poly11' Linear polynomial surface 'poly2' Quadratic polynomial curve 'linearinterp' Piecewise linear interpolation 'cubicinterp' Piecewise cubic interpolation 'smoothingspline' Smoothing spline (curve) 'lowess' Local linear regression (surface)
or any of the names of library models described in cflibhelp. Type cflibhelp to display names and descriptions of all library models.
To fit custom models, use an anonymous function or create a fittype with the fittype function and use this as the fitType argument.
fitobject is the fit result, a cfit (for curves) or sfit (for surfaces) object. See Fit Postprocessing for functions for plotting, evaluating, calculating confidence intervals, integrating, differentiating, or modifying your fit object.
fitobject = fit(..., Name, Value,...) fits the data using the problem and algorithm options specified in the name-value pair arguments. You can display the supported property names and default values for specific library models with the fitoptions function. For example:
fitoptions( 'cubicinterp' ) fitoptions( 'poly1' )
fitobject = fit(x,y,libname,options) fits the data using the algorithm options specified by the fitoptions object options. This is an alternative syntax to specifying the property-value pairs. For help on constructing options, see the fitoptions function.
fitobject = fit(...,'problem',vals) assigns vals to the problem-dependent constants. vals is a cell array with one element per problem dependent constant. See fittype for more information on problem dependent constants.
fitobject = fit(x,y,fitType,...,'Weight', Weights) creates a weighted fit using the given Weights. Weights must be a vector the same size as y.
[fitobject,gof] = fit(...) returns goodness-of-fit statistics to the structure gof. The gof structure includes the fields shown in the table below.
Field | Value |
|---|---|
sse | Sum of squares due to error |
R2 | Coefficient of determination |
adjustedR2 | Degree-of-freedom adjusted coefficient of determination |
stdError | Root mean squared error (standard error) |
[cfun,gof,output] = fit(...) returns the structure output, which contains information associated with the fitting algorithm. Fields depend on the algorithm. For example, the output structure for nonlinear least-squares algorithms has the fields shown in the table below.
Field | Value |
|---|---|
numobs | Number of observations (response values) |
numparam | Number of unknown parameters (coefficients) to fit |
residuals | Vector of residuals |
Jacobian | Jacobian matrix |
exitflag | Describes the exit condition of the algorithm. Positive flags indicate convergence, within tolerances. Zero flags indicate that the maximum number of function evaluations or iterations was exceeded. Negative flags indicate that the algorithm did not converge to a solution. |
iterations | Number of iterations |
funcCount | Number of function evaluations |
firstorderopt | Measure of first-order optimality (absolute maximum of gradient components) |
algorithm | Fitting algorithm employed |
For rational and Weibull models, and all custom nonlinear models, the toolbox selects default initial values for coefficients uniformly at random from the interval (0,1).
As a result, multiple fits using the same data and model may lead to different fitted coefficients. To avoid this, specify initial values for coefficients with fitoptions structure or a vector value for the StartPoint property.
Fit a cubic interpolating spline through x and y:
[curve, goodness] = fit( x, y, 'pchipinterp' );
Fit a polynomial surface of degree 2 in x and degree 3 in y using the least absolute residual robust (LAR) method:
sf = fit( [x, y], z, 'poly23', 'Robust', 'LAR' );
Fit the 1st equation in the curve fitting library of exponential models (a single-term exponential), overriding the starting point to be p0:
curve = fit( x, y, 'exp1', 'StartPoint', p0 );
Load data and fit using an anonymous function:
Load data and set Emax to 1 before defining your anonymous function:
data = importdata( 'OpioidHypnoticSynergy.txt' ); Propofol = data.data(:,1); Remifentanil = data.data(:,2); Algometry = data.data(:,3); Emax = 1;
Define the model equation as an anonymous function:
Effect = @(IC50A, IC50B, alpha, n, x, y) ...
Emax*( x/IC50A + y/IC50B + alpha*( x/IC50A )...
.* ( y/IC50B ) ).^n ./(( x/IC50A + y/IC50B + ...
alpha*( x/IC50A ) .* ( y/IC50B ) ).^n + 1);Use the anonymous function Effect as an input to the fit function, and plot the results:
AlgometryEffect = fit( [Propofol, Remifentanil], Algometry, Effect, ...
'StartPoint', [2, 10, 1, 0.8], ...
'Lower', [-Inf, -Inf, -5, -Inf], ...
'Robust', 'LAR' )
plot( AlgometryEffect, [Propofol, Remifentanil], Algometry )See Custom Nonlinear Surface Fitting Examples for more information on this example.
Load and plot data, create fit options and fit type, then create and plot fit:
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')

cflibhelp | confint | feval | fitoptions | fittype | plot
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |