Products & Services Solutions Academia Support User Community Company

Learn more about Curve Fitting Toolbox   

fittype - Constructor for fittype object

Syntax

ffun = fittype(libname)
ffun = fittype(expr)
ffun = fittype({expr1,...,exprn})
ffun = fittype(expr,PropName,PropVal,...)
ffun = fittype({expr1,...,exprn},PropName,PropVal,...)

Description

ffun = fittype(libname) constructs the fittype object ffun for the library model specified by libname. You can display library model names with the cflibhelp function.

ffun = fittype(expr) constructs the fittype object ffun for the custom nonlinear model specified by the expression in the string expr. You can use expr to specify any MATLAB command and therefore any M-file. By default, the independent variable is assumed to be x and the dependent variable is assumed to be y. All other variables are assumed to be coefficients. All coefficients must be scalars.

ffun = fittype({expr1,...,exprn}) constructs the fittype object ffun for the custom linear model with terms specified by the expressions in the strings expr1, expr2, ... , exprn. Coefficients are not included in the expressions for the terms. If there is a constant term, use '1' as the corresponding expression in the cell array.

ffun = fittype(expr,PropName,PropVal,...) or ffun = fittype({expr1,...,exprn},PropName,PropVal,...) constructs the fittype object ffun using specified property name/value pairs. Supported property names and values are given in the table below.

PropName

PropVal

'coefficients'

The coefficient names. Use a cell array if there are multiple names. The following names are not allowed: i, j, pi, inf, nan, eps.

'dependent'

The dependent (response) variable name

'independent'

The independent (predictor) variable name

'options'

The default fit options for the object

'problem'

The problem-dependent (fixed) parameter names. Use a cell array if there are multiple names. The default is none.

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

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

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'.

The examples below demonstrate how to specify an independent variable and coefficient names.

Example

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 names of the coefficients:

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

The following example demonstrates how to fit a curve defined by an M-file. First define a function in a M-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

Enter the following commands to define some data, create a fittype specifying the M-file function piecewiseLine, create a fit with 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 ) 

See Also

fit, cfit

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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