Matlab通过自定义方程实现数据拟合

文章目录(展开/隐藏→)

展开
4.3
(3)

本文,我们继续使用Matlab内置Census数据,通过自定义方程,实现数据的拟合。使用的Matlab函数为fittype。这里,自定义拟合的方程有二次和三次多项式。函数形式为:y = a(x-b)n。通过数据拟合,我们可以大致判断出未来数据的变化情况,可以预测数据走向等。

Matlab通过自定义方程实现数据拟合

fittype帮助文件

首先,给出fittype函数的帮助文件如下:

>> help fittype
 fittype   Fittype for curve and surface fitting
 
    A fittype encapsulates information describing a model. To create a
    fit, you need data, a fittype, and (optionally) fit options and an
    exclusion rule.
 
 
    LIBRARY MODELS
 
    fittype(LIBNAME) constructs a fittype for the library model
    specified by LIBNAME.
 
    Choices for LIBNAME 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 "List of Library Models
    for Curve and Surface Fitting" in the documentation.
 
 
    CUSTOM MODELS
 
    fittype(EXPR) constructs a fittype from 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 (see SYMVAR). In this case, the fittype
    assumes 'x' is the independent variable, 'y' is the dependent
    variable, and all other variables are coefficients of the model. If
    no variable exists, 'x' is used.
 
    All coefficients must be scalars. You cannot use the following
    coefficient names in the expression string EXPR: i, j, pi, inf,
    nan, eps.
 
    If EXPR is a string or anonymous function, then the toolbox uses a
    nonlinear fitting algorithm to fit the model to data. To use a
    linear fitting algorithm, use a cell array of terms.
 
 
    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>, <problem parameters>, <x>, <y>) expression
 
    There must be 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 you can specify different names by using the
    'independent' property 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 variables xs and ys from the workspace:
 
        % Variables in workspace
        xs = (0:0.1:1).';
        ys = [0; 0; 0.04; 0.1; 0.2; 0.5; 0.8; 0.9; 0.96; 1; 1];
        % Create fittype
        ft = fittype( @(b, h, x) interp1( xs, b+h*ys, x, 'pchip' ) )
        % 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] )
 
 
    LINEAR MODELS
 
    To use a linear fitting algorithm specify EXPR as a cell array of
    terms. That is, 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' (since c=c*1) and so EXPR is
 
        EXPR = {'x','sin(x)','1'}
 
 
    ADDITIONAL PROPERTIES
 
    fittype(EXPR,PROP1,VALUE1,PROP2,VALUE2,....) uses the property
    name/value pairs PROP1-VALUE1, PROP2-VALUE2 to specify property
    values other than the default values.
 
    PROPERTY         DESCRIPTION
    'independent'    Specifies the independent variable name
    'dependent'      Specifies the dependent variable name
    'coefficients'   Specifies the coefficient names (in a cell array
                     if there are two or more). Note excluded names
                     above.
    'problem'        Specifies the problem-dependent (constant) names
                     (in a cell array if there are two or more)
    'options'        Specifies the default 'FITOPTIONS' for this
                     equation
 
    Defaults: The independent variable is x.
              The dependent variable is y.
              There are no problem dependent variables.
              Everything else is a coefficient name.
 
    Multi-character symbol names may be used.
 
 
    EXAMPLES
 
       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
       g = fittype( @(a,b,c,x) a*x.^2+b*x+c )
       g = fittype( @(a,b,c,d,x,y) a*x.^2+b*x+c*exp(-(y-d).^2), ...
            'independent', {'x', 'y'}, ...
            'dependent', 'z' ); % for fitting surfaces

数据写入

本示例显示如何将自定义方程拟合到人口普查数据,指定边界、系数和与问题相关的参数。

加载并绘制census.mat中的数据:

load census
plot(cdate,pop,'o')
hold on
Matlab通过自定义方程实现数据拟合

数据拟合

为自定义非线性模型y=a(x-b)n创建拟合选项结构和拟合类型对象,其中ab是系数,nproblem相关参数。有关problem相关参数的更多详细信息,请参阅fittype函数页面,以上帮助文档也有提到。

s = fitoptions('Method','NonlinearLeastSquares',...
               'Lower',[0,0],...
               'Upper',[Inf,max(cdate)],...
               'Startpoint',[1 1]);
f = fittype('a*(x-b)^n','problem','n','options',s);

使用拟合选项和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

使用拟合选项和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-05  (1.245e-05, 1.474e-05)
       b =        1725  (1718, 1731)
     Problem parameters:
       n =           3

gof3 = 

           sse: 232.0058
       rsquare: 0.9981
           dfe: 19
    adjrsquare: 0.9980
          rmse: 3.4944

绘制拟合结果和数据:

plot(c2,'m')
plot(c3,'c')
legend('data','fit with n=2','fit with n=3')
Matlab通过自定义方程实现数据拟合

共计3人评分,平均4.3

到目前为止还没有投票~

很抱歉,这篇文章对您没有用!

让我们改善这篇文章!

告诉我们我们如何改善这篇文章?

原创文章,作者:古哥,转载需经过作者授权同意,并附上原文链接:https://iymark.com/program/matlab-fittype-fit.html

(1)
上一篇 2022年09月01日 20:57
下一篇 2022年09月03日 15:13

相关推荐

发表回复

登录后才能评论
本站APP