Skip to Main Content Skip to Search
Product Documentation

fit - Fit curve or surface to data

Syntax

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

Description

fitobject = fit(x,y,fitType) fits the data in x and y with the library model, anonymous function or fittype object specified by fitType.

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

Remarks on Starting Points

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.

Examples

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:

  1. 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;
  2. 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);
  3. 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:

  1. Load and plot the data in census.mat:

    load census
    plot(cdate,pop,'o')
    hold on
    

  2. 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);
  3. 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
  4. 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
  5. Plot the fit results with the data:

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

See Also

cflibhelp | confint | feval | fitoptions | fittype | plot

How To

  


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