1.polyfit函数(只限多项式拟合)
语法:
a = polyfit ( x, y, n)
参数n为x的最高阶,返回值a是n阶函数的系数,a是一个长度为n+1的行向量。
2.lsqcurvefit函数
应用场合:
可以自编公式进行拟合,比polyfit函数灵活,可以得出拟合优度。
语法:
[s,resnorm]=lsqcurvefit(f,a,x,y)
f:符号函数句柄。
a:系数预估的值(预拟合的未知参数的估计值)。
x:我们已经获知的x的值。
y:我们已经获知的x对应的y的值。
s:求解的系数a的值
resnorm:拟合优度
举例
function[s,resnorm]=formular1(x,y)
a=[0 0 0 0]
f=@(a,x)a(1)+a(2)*(x).^0.5+a(3)*x+a(4)*(x).^2
[s,resnorm]=lsqcurvefit(f,a,x,y)
3.curving fitting tool(cftool)
通过下拉菜单“Type of fit”选择拟合曲线的类型,工具箱提供的拟合类型有:
Custom Equations:用户自定义的函数类型
Exponential:指数逼近,有 2 种类型, aexp(bx) 、 aexp(bx) + cexp(dx)
Fourier:傅立叶逼近,有 7 种类型,基础型是 a0 + a1cos(xw) + b1sin(xw)
Gaussian:高斯逼近,有 8 种类型,基础型是 a1exp(-((x-b1)/c1)^2)
Interpolant:插值逼近,有 4 种类型,linear、nearest neighbor、cubic spline、shape-preserving
Polynomial:多项式i逼近,有 9 种类型,linear polynomial、quadratic polynomial、cubic polynomial 以及 4-9th degree polynomial
Power:幂逼近,有 2 种类型。
Rational:有理数逼近,分子(Numerator)、分母(Denominator)共有的类型是 linear rational、quadratic rational、cubic rational、4-5th degree rational;此外,分子(Numerator)还包括 constant 型
Smoothing Spline:平滑样条逼近
Sum of Sin Functions:正弦曲线逼近,有 8 种类型,基础型是a1sin(b1x + c1)
- 注:center and scale--------将xdata原始数据进行中心化与比例化处理,即将x变量替换为(x-mean(x))/std(x),同样替换y变量。类似于线性插值技术。这是在计算任何多项式项之前完成的。这种变换在理论上不会改变拟合,但在有限精度的计算机上可以使结果更好。matlab在曲线拟合工具箱中加入这个选项是为了当xdata与ydata拟合不好时,选择这个选项有利于得到更好的拟合效果。
- SSE(和方差、误差平方和):The sum of squares due to error
接近0,表示与数据拟合的好,但是要小心过拟合 - MSE(均方差、方差):Mean squared error
- RMSE(均方根、标准差):Root mean squared error
- R-square(确定系数):Coefficient of determination
测数据与推理数据之间的相关系数平方值,趋近于1较好 - Adjusted R-square:Degree-of-freedom adjusted coefficient of determination