MATLAB|曲线拟合基本介绍

曲线拟合工具箱cftool基本介绍

Tips

mathworks官网的和help文件
https://cn.mathworks.com/help/curvefit/fit-comparison-in-curve-fitting-app.html
在mathworks官网找到相应的help文件,点击右上角的Translate This Page,可以查看中文翻译,虽然是自动翻译的,很不准确,但偶尔可以看看。

《MATLAB|曲线拟合基本介绍》

☆曲线拟合工具箱界面

《MATLAB|曲线拟合基本介绍》

《MATLAB|曲线拟合基本介绍》

☆☆操作方法

  • 在命令行输入数据:
    x
    y

  • 启动曲线拟合工具箱
    命令:cftool

  • 进入曲线拟合工具箱界面“Curve Fitting tool”
    1)利用X data和Y data的下拉菜单读入数据x,y
    2)选择曲线拟合类型
    3)自动拟合 即可在结果窗口和曲线窗口显示出拟合结果

  • 工具箱提供的拟合类型如下,进行简要说明,可以尝试各种不同类型拟合效果

    • Custom Equations:用户自定义的函数类型
    • Exponential:指数逼近,有2种类型, aexp(bx) 、 aexp(bx) + cexp(dx)
    • Fourier:傅立叶逼近,有7种类型,基础型是 a0 + a1cos(xw) + b1sin(xw)
    • Gaussian:高斯逼近,有8种类型,基础型是 a1*exp(-((x-b1)/c1)^2)
    • Interpolant:插值逼近,有4种类型,linear、nearest neighbor、cubic spline、shape-preserving
    • Polynomial:多形式逼近,有9种类型,linear ~、quadratic ~、cubic ~、4-9th degree ~
    • Power:幂逼近,有2种类型,ax^b 、ax^b + c
    • Rational:有理数逼近,分子、分母共有的类型是linear ~、quadratic ~、cubic ~、4-5th degree ~;此外,分子还包括constant型
    • Smoothing Spline:平滑逼近(翻译的不大恰当,不好意思)
    • Sum of Sin Functions:正弦曲线逼近,有8种类型,基础型是 a1sin(b1x + c1)
    • Weibull:只有一种,a*b*x^(b-1)*exp(-a*x^b)
  • 选择View > Residuals Plot,可以查看残差图。

  • 在Table of Fits中右击选择Duplicate “XX” (或者在Fit主菜单里面复制) 复制某个拟合,重新生成别的拟合,进行对比。

  • 观察Results以及residuals plot 残差图。
    The residuals from a good fit should look random with no apparent pattern. A pattern, such as a tendency for consecutive residuals to have the same sign, can be an indication that a better model exists.

☆☆☆如何选取最好的拟合结果

To determine the best fit, you should examine both the graphical and numerical fit results.

第一步:查看图的拟合结果【Examine the Graphical Fit Results】
  • 残差图:越小,拟合越好。
  • 选择Tools > Axes Limits,或者直接点解Adjust Axes Limits图标,调整加大x轴坐标,显示曲线的变化趋势,若拟合曲线的趋势与预期趋势一致(上升、下降),则表明拟合效果较好。

    《MATLAB|曲线拟合基本介绍》

第二步:检查数值结果【Evaluate the Numerical Fit Results】
  • 拟合结果指标【Goodness of fit】(参见help: Evaluating Goodness of Fit)

    《MATLAB|曲线拟合基本介绍》

  • 在Table of Fits中双击某个指标可以排序。

SSE【the sum of squares due to error】:误差平方和
R-square: 复相关系数或复测定系数
Adjusted R-square:调整自由度的复相关系数
RMSE【Root mean squared error】: 均方根误差

The adjusted R-square statistic is generally the best indicator of the fit quality when you add additional coefficients to your model.

当SSE和RMSE越小,R越接近于1时标明拟合的越好。

☆☆☆☆其他操作

  • 选择Window > Left/Right Tile or Top/Bottom Tile(窗口>左/右平铺或顶部/底部平铺图),可以对比拟合结果。
  • 选择View > Fit Settings or Table of Fits可以关闭Fit Settings、Table of Fits,只显示图形,进行对比。

MATLAB help文件里有有个Note:
The fitted coefficients associated with the constant, linear, and quadratic terms are nearly identical for each normalized polynomial equation. However, as the polynomial degree increases, the coefficient bounds associated with the higher degree terms cross zero, which suggests overfitting.
对于多项式拟合,多项式阶数越高,如果拟合系数的置信区间接近0,说明可能过度拟合了。。。

《MATLAB|曲线拟合基本介绍》

MATLAB中的fit函数

  • fit函数fit()
    • fitobject = fit(x,y,fitType,fitOptions)
  • 拟合类型:拟合表达式expression
    • aFittype = fittype(expression,Name,Value)
  • fitoptions = fitoptions(libraryModelName,Name,Value)
options =
 
        Normalize: 'off'
          Exclude: []
          Weights: []
           Method: 'NonlinearLeastSquares'
           Robust: 'Off'
       StartPoint: [1x0 double]
            Lower: [0 -Inf 0 0 -Inf 0]
            Upper: [1x0 double]
        Algorithm: 'Trust-Region'
    DiffMinChange: 1.0000e-08
    DiffMaxChange: 0.1000
          Display: 'Notify'
      MaxFunEvals: 600
          MaxIter: 400
           TolFun: 1.0000e-06
             TolX: 1.0000e-06
  • fitoption得出的各参数的意义:

Normalize: 对数据归一化处理
Exclude: 排除数据
Weights: 加权
Method: 拟合方法, 非线性最小二乘法
Robust: 稳健方式(通过加权方式排除异常值影响)
StartPoint: 拟合开始点
Lower: 拟合参数下界
Upper: 拟合参数上界
Algorithm: 算法 ‘置信区间’
DiffMinChange: 差分时参数最小变化值
DiffMaxChange: 差分时参数最大变化值
Display: 显示通知
MaxFunEvals: 最大函数计算次数
MaxIter: 最大迭代次数
TolFun: 函数精度
TolX: 参数精度

《MATLAB|曲线拟合基本介绍》

MATLAB线性拟合函数polyfit、polyval、polyconf

  • 参见:
  1. 多项式拟合与线性回归分析对比案例,polyfit、polyval、polyconf
  2. MATLAB 线性拟合小结 —— polyfit
  • MATLAB简单应用实例
% 原始数据
data1; %x数据
data2; %y数据
% cftool查看
cftool(data1,data2);
% 多项式拟合
[pp1,ss1]=polyfit(data1,data2,1),%%一次多项式
nh_line=polyval(pp1,data1);%拟合曲线,相当于:nh_line=pp1(1,1).*data1+pp1(1,2);
% 得到具有75%保证率的直线
[yhat,delta]=polyconf(pp1,data1,ss1,0.25); %% 0.25表示1-0.25保证率
pp75=polyfit(data1,yhat+delta,1); %%公式
% 绘图
figure;
plot(data1,data2,'ko');hold on; %原始数据
plot(data1,nh_line,'b-','LineWidth',2.5);% 拟合的一次曲线
plot(data1,yhat+delta,'r:','LineWidth',2.5);% 具有75%保证率的直线--上限
plot(data1,yhat-delta,'r:','LineWidth',2.5);% 具有75%保证率的直线--下限

《MATLAB|曲线拟合基本介绍》

推荐阅读:

    原文作者:云飘飘同学
    原文地址: https://www.jianshu.com/p/26e1bb418aa4
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞