fit - Fit model to data

Syntax

cfun = fit(xdata,ydata,libname)
cfun = fit(...,PropName,PropVal,...)
cfun = fit(xdata,ydata,libname,options)
cfun = fit(xdata,ydata,ffun,...)
cfun = fit(...,'problem',vals)
[cfun,gof] = fit(...)
[cfun,gof,output] = fit(...)

Description

cfun = fit(xdata,ydata,libname) fits the data in the column vectors xdata and ydata with the library model specified by libname. xdata and ydata cannot contain Inf or NaN. Only the real parts of complex data are used in the fit. You can display library model names with the cflibhelp function. The fit result is returned as a cfit object cfun.

cfun = fit(...,PropName,PropVal,...) fits the data using specified property name/value pairs. You can display the supported property names and values for specific library models with the fitoptions function.

cfun = fit(xdata,ydata,libname,options) fits the data using the options specified by the fit options structure options. Fit options structures are created with the fitoptions function.

cfun = fit(xdata,ydata,ffun,...) fits the data with the fittype object ffun. fittype objects are created with the fittype function.

cfun = fit(...,'problem',vals) assigns vals to the problem-dependent parameters of the model before fitting. vals is a scalar or a cell array with one element per parameter.

[cfun,gof] = fit(...) returns goodness-of-fit statistics to the structure gof. The gof structure has the fields shown in the table below.

Field

Value

sse

Sum of squares due to error

rsquare

Coefficient of determination

dfe

Degrees of freedom

adjrsquare

Degree-of-freedom adjusted coefficient of determination

rmse

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

Remarks

For some nonlinear library models (rational and Weibull), and all custom nonlinear models, default initial values for coefficients are selected 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, initial values for coefficients can be specified through a fitoptions structure or a vector value for the StartPoint property. Alternatively, the initial state of the random number generator rand can be set before fitting.

All other nonlinear library models automatically compute reasonable initial values. These initial values depend on the data, and are based on model-specific heuristics.

Example

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(xb)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.5994

Fit 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.4944

Plot the fit results with the data:

plot(c2,'m')
plot(c3,'c')

See Also

cflibhelp, fitoptions, fittype, feval, plot

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS