Skip to Main Content Skip to Search
Product Documentation

fittype - Fit type for curve and surface fitting

Syntax

ffun = fittype(libname)
ffun = fittype(expr)
ffun = fittype({expr1,...,exprn})
ffun = fittype(expr, Name, Value,...)
ffun = fittype({expr1,...,exprn}, Name, Value,...)

Description

ffun = fittype(libname) constructs the fittype object ffun for the library model specified by libname. See Using Library Models.

ffun = fittype(expr) constructs ffun for the custom model specified by the expression expr. The expression can be a string, cell array, or anonymous function. See Using Custom Models.

ffun = fittype({expr1,...,exprn}) constructs a custom linear model with terms specified by the cell array of expressions in the strings expr1, expr2, ... , exprn. See Using Linear Models.

ffun = fittype(expr, Name, Value,...) or ffun = fittype({expr1,...,exprn}, Name, Value,...) constructs the fittype using specified name-value pair arguments specifying values other than the default values. For supported names-value pair arguments, see Input Arguments.

Using Library Models

ffun = fittype(libname) constructs the fittype for the library model libname. Choices for libname include any of the names of library models described in List of Library Models for Curve and Surface Fitting. The following table shows some common examples.

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)

Using Custom Models

ffun = fittype(expr) constructs a custom model fittype for the MATLAB expression contained in the string, cell array, or anonymous function expr.

The fittype automatically determines input arguments by searching expr for variable names. The fittype assumes x is the independent variable, y is the dependent variable, and all other variables are coefficients of the model. x is used if no variable exists. See Dependent and Independent Variables.

All coefficients must be scalars. You cannot use the following coefficient names in the expression string expr: i, j, pi, inf, nan, and eps.

If expr is a string or anonymous function, then the toolbox uses a nonlinear fitting algorithm to fit the model to data (see Using Anonymous Functions). To use a linear fitting algorithm, use a cell array of terms (see Using Linear Models).

Using Anonymous Functions

If expr is an anonymous function, then the order of inputs must be correct. The input order enables the fittype class to determine which inputs are coefficients to estimate, problem-dependent parameters and independent variables. The order of the input arguments to the anonymous function must be:

EXPR = @(coefficients, problemparameters, x, y) expression

You need at least one coefficient. The problem parameters and y are optional. The last arguments, x and y, represent the independent variables: just x for curves, but x and y for surfaces. If you don't want to use x and/or y as the names of the independent variables, then specify different names using the 'independent' argument name-value pair. However, whatever name or names you choose, these arguments must be the last arguments to the anonymous function.

Anonymous functions make it easier to pass other data into the fittype and fit functions. For example, to create a fittype using an anonymous function and a variable value (c) from the workspace:

 c = 1;
 g = fittype( @(a, b, x) a*x.^2+b*x+c )

The fittype can use the variable values in your workspace at the time you create the fittype. To pass in new data from the workspace, create the fittype again, e.g.,

c=5 % Change value of c
g = fittype( @(a, b, x) a*x.^2+b*x+c )

In this case the value of c is fixed when you create the fittype. To specify the value of c at the time you call fit, you can use problem parameters. For example, to make a fit with c = 2 and then a new fit with c = 3:

g = fittype( @(a,b,x) a*x.^2+b*x+c, 'problem', 'c' )
f1 = fit( xdata, ydata, g, 'problem', 2 )
f2 = fit( xdata, ydata, g, 'problem', 3 ) 

See Examples.

Using Linear Models

To use a linear fitting algorithm, specify expr as a cell array of terms, as follows: ffun = fittype({expr1,...,exprn}). Specify the model terms by the expressions in the strings expr1, expr2, ... , exprn. Do not include coefficients in the expressions for the terms. If there is a constant term, use '1' as the corresponding expression in the cell array.

To specify a linear model of the following form:

 coeff1 * term1 + coeff2 * term2 + coeff3 * term3 + ...

(where no coefficient appears within any of term1, term2, etc.), use a cell array where each term, without coefficients, is specified in a cell of expr, as follows:

EXPR = {'term1', 'term2', 'term3', ... }

For example, the model

a*x + b*sin(x) + c

is linear in a, b and c. It has three terms x, sin(x) and 1 (because c=c*1) and so expr is

EXPR = {'x','sin(x)','1'}

Dependent and Independent Variables

To determine what are dependent and independent variables and coefficients, consider this equation:

The 'independent' variable is what you control. The 'dependent' variable is what you measure, i.e., it depends on the independent variable. The 'coefficients' are the parameters that the fitting algorithm estimates.

For example, if you have census data, then the year is the independent variable because it does not depend on anything. Population is the dependent variable, because its value depends on the year in which the census is taken. If a parameter like growth rate is part of the model, if the fitting algorithm estimates it, then it is one of the 'coefficients'.

See Examples for how to specify an independent variable and coefficient names.

Input Arguments

libname

Library model name. See List of Library Models for Curve and Surface Fitting.

expr

Custom model expression. The expression can be a string, cell array, or anonymous function.

You can specify any MATLAB command and therefore any .m file.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments, where Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

'coefficients'

The coefficient names. Use a cell array for multiple names. You can use multicharacter symbol names. You cannot use the following names: i, j, pi, inf, nan, eps.

'dependent'

The dependent (response) variable name.

Default: y

'independent'

The independent (predictor) variable name.

Default: x

'options'

The default fit options for the object.

'problem'

The problem-dependent (fixed) parameter names. Use a cell array for multiple names.

Default: None

Output Arguments

ffun

fittype object. You can use a fittype object as an input to the fit function.

Examples

Construct a fittype object for the rat33 library model:

f = fittype('rat33')
f =
   General model Rat33:
   f(p1,p2,p3,p4,q1,q2,q3,x) = 
         (p1*x^3 + p2*x^2 + p3*x + p4)/
                (x^3 + q1*x^2 + q2*x + q3)
 

Construct a fittype object for a custom nonlinear model, designating n as a problem-dependent parameter and u as the independent variable:

g = fittype('a*u+b*exp(n*u)',...
            'problem','n',...
            'independent','u')
g =
     General model:
       g(a,b,n,u) = a*u+b*exp(n*u)
 

Construct a fittype object for a custom linear model, specifying the coefficient names:

h = fittype({'cos(x)','1'},'coefficients',{'a1','a2'})
h =
     Linear model:
       h(a1,a2,x) = a1*cos(x) + a2
 

Fit a curve defined by a file:

  1. Define a function in a MATLAB file:

    function y = piecewiseLine( x, a, b, c, d, k )
    % PIECEWISELINE   A line made of two pieces
    % that is not continuous.
    
    y = zeros( size( x ) );
    
    % This example includes a for-loop and if statement
    % purely for demonstration purposes.
    for i = 1:length( x )
        if x(i) < k,
            y(i) = a + b.* x(i);
        else
            y(i) = c + d.* x(i);
        end
    end
    end
  2. Define some data, create a fittype specifying the function piecewiseLine, create a fit using the fittype, and plot the results:

    x = [0.81;0.91;0.13;0.91;0.63;0.098;0.28;0.55;...
    0.96;0.96;0.16;0.97;0.96];
    y = [0.17;0.12;0.16;0.0035;0.37;0.082;0.34;0.56;...
    0.15;-0.046;0.17;-0.091;-0.071];
    ft = fittype( 'piecewiseLine( x, a, b, c, d, k )' )
    f = fit( x, y, ft, 'StartPoint', [1, 0, 1, 0, 0.5] )
    plot( f, x, y ) 
 

Create a fittype using an anonymous function:

g = fittype( @(a, b, c, x) a*x.^2+b*x+c )
 

Create a fittype for a surface using an anonymous function and specifying independent and dependent parameters:

g = fittype( @(a, b, c, d, x, y) a*x.^2+b*x+c*exp...
    ( -(y-d).^2 ), 'independent', {'x', 'y'},...
     'dependent', 'z' );
 

Create a fittype for a surface using an anonymous function and specifying independent and dependent parameters, and problem parameters that you will specify later when you call fit:

g = fittype( @(a,b,c,d,x,y) a*x.^2+b*x+c*exp( -(y-d).^2 ), ...
        'problem', {'c','d'}, 'independent', {'x', 'y'}, ...
        'dependent', 'z' ); 
 

Use an anonymous function to pass workspace data into the fittype and fit functions.

  1. Create and plot an S-shaped curve. In later steps you will stretch and move this curve to fit to some data.

    % Breakpoints
    xs = (0:0.1:1).';
    % Height of curve at breakpoints
    ys = [0; 0; 0.04; 0.1; 0.2; 0.5; 0.8; 0.9; 0.96; 1; 1];
    % Plot S-shaped curve
    xi = linspace( 0, 1, 241 );
    plot( xi, interp1( xs, ys, xi, 'pchip' ), 'LineWidth', 2 )
    hold on
    plot( xs, ys, 'o', 'MarkerFaceColor', 'r' )
    hold off
    title S-curve
    
  2. Create a fittype using an anonymous function, taking the values from the workspace for the curve breakpoints (xs) and the height of the curve at the breakpoints (ys). Coefficients are b (base) and h (height).

    ft = fittype( @(b, h, x) interp1( xs, b+h*ys, x, 'pchip' ) )
  3. Plot the fittype specifying example coefficients of base b=1.1 and height h=-0.8.

    plot( xi, ft( 1.1, -0.8, xi ), 'LineWidth', 2 )
    title 'Fittype with b=1.1 and h=-0.8'
    
  4. Load and fit some data, using the fittype ft created using workspace values:

    % Load some data
    xdata = [0.012;0.054;0.13;0.16;0.31;0.34;0.47;0.53;0.53;...
       0.57;0.78;0.79;0.93];
    ydata = [0.78;0.87;1;1.1;0.96;0.88;0.56;0.5;0.5;0.5;0.63;...
       0.62;0.39];
    % Fit the curve to the data
    f = fit( xdata, ydata, ft, 'Start', [0, 1] )
    % Plot fit
    plot( f, xdata, ydata )
    title 'Fitted S-curve'

 

The following example demonstrates the differences between using anonymous functions with problem parameters and workspace variable values.

  1. Load data, create a fittype for a curve using an anonymous function with problem parameters, and call fit specifying the problem parameters:

    % Load some random data:
    xdata = [0.098;0.13;0.16;0.28;0.55;0.63;0.81;0.91;0.91;...
        0.96;0.96;0.96;0.97];
    ydata = [0.52;0.53;0.53;0.48;0.33;0.36;0.39;0.28;0.28;...
        0.21;0.21;0.21;0.2];
    
    % Create a fittype that has a problem parameter:
    g = fittype( @(a,b,c,x) a*x.^2+b*x+c, 'problem', 'c' )
    
    % Examine coefficients. Observe c is not a coefficient.
    coeffnames( g )
    
    % Examine arguments. Observe that c is an argument.
    argnames( g )
    
    % Call fit and specify the value of c:
    f1 = fit( xdata, ydata, g, 'problem', 0, 'start', [1, 2] )
    
    % Note: specify start points in the calls to fit to
    % avoid warning messages about random start points
    % and to ensure repeatability of results.
    
    % Call fit again and specify a different value of c,
    % to get a new fit:
    f2 = fit( xdata, ydata, g, 'problem', 1, 'start', [1, 2] )
    
    % Plot results. Note example specified c constants
    % do not make a good fit.
    plot( f1, xdata, ydata )
    hold on
    plot( f2, 'b' )
    hold off
    
  2. Modify the example above to create the same fits using workspace values for variables, instead of using problem parameters. Using the same data, create a fittype for a curve using an anonymous function with a workspace value for variable c:

    % Remove c from the argument list:
    try
        g = fittype( @(a,b,x) a*x.^2+b*x+c )
    catch e
        disp( e.message )
    end
    % Observe error because now c is undefined.
    % Define c and create fittype:
    c = 0;
    g1 = fittype( @(a,b,x) a*x.^2+b*x+c )
    
    % Call fit (now no need to specify problem parameter)
    f1 = fit( xdata, ydata, g1, 'start', [1, 2] )
    % Note that this f1 is the same as the f1 above.
    % To change the value of c, create the fittype again:
    c = 1;
    g2 = fittype( @(a,b,x) a*x.^2+b*x+c ) % uses c = 1
    f2 = fit( xdata, ydata, g2, 'start', [1, 2] )
    % Note that this f2 is the same as the f2 above.
    % Plot results
    plot( f1, xdata, ydata )
    hold on
    plot( f2, 'b' )
    hold off
    
 

Here are other examples using fittype:

g = fittype('a*x^2+b*x+c')
g = fittype('a*x^2+b*x+c','coeff',{'a','b','c'})
g = fittype('a*time^2+b*time+c','indep','time')
g = fittype('a*time^2+b*time+c','indep','time','depen','height')
g = fittype('a*x+n*b','problem','n')
g = fittype({'cos(x)','1'})                            % linear
g = fittype({'cos(x)','1'}, 'coefficients', {'a','b'}) % linear     

See Also

cfit | fit | fitoptions | sfit

How To

  


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