有时候我们需要对一些散点进行拟合,但是经常会发现对于未知曲线类型的散点拟合无从下手,常规的方法就是尝试各种拟合曲线,然后选出最优的,但是问题就来了,对这么多的曲线编写程序岂不是累死?
幸好有Matlab!幸好有cftool!
我们可以先把数据导入matlab,如:
x = [8.8,9.4,9.6,7.8,8.5,6.2,11,12.5,12.8,13.4,11,12.1,10.8,10.1,13,12,10.6,10,10.8,12.6,12,13,11.7,17.4,17.2,15.2,14,15.1,14.6,14,17.4,16.2,15.4,14,20.5,19.2,20,18,20,19.5,19.8,19.7,19.7,21.6,24.6,22,22,25,24.8,27,28.5,29.2,27.3,31,31,39]
y = [0.0296,0.035,0.04,0.022,0.036,0.0153,0.0534,0.1064,0.1133,0.122,0.076,0.1082,0.0704,0.0484,0.0926,0.108,0.0458,0.0391,0.0642,0.0981,0.0886,0.077,0.0914,0.1053,0.196,0.1637,0.112,0.1449,0.153,0.1181,0.1913,0.2268,0.1537,0.1024,0.277,0.2974,0.2664,0.1784,0.3014,0.2075,0.2724,0.2899,0.256,0.2493,0.4486,0.3494,0.2536,0.498,0.4839,0.4605,0.7285,0.8039,0.6058,0.8141,0.7751,1.3157]
输入cftool,回车,打开Curve Fitting tool工具箱
在这里面选择数据来源,并选择函数名称,即可进行拟合,matlab会提供函数和误差。接下来选最好的函数就行了!
参考如图:
注:工具箱提供的拟合类型如下,进行简要说明,可以尝试各种不同类型拟合效果
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:只有一种,abx(b-1)*exp(-a*xb)