matlab如何做线性拟合
matlab中线性拟合可以使用多项式拟合函数polyfit()实现,其调用格式为
P = polyfit(X,Y,N)
其中,X,Y是需要拟合的数据,N是拟合的多项式的最高次数,P是拟合的多项式的系数。
实例演示如下:
1、示例数据
x=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2]
y=[-8,-236,-415,-562,-701,-860,-961,-1082,-1188,-1304,-1405,-1534]
拟合方程:y=a+bx
2、matlab代码
>> x=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2];
>> y=[-8,-236,-415,-562,-701,-860,-961,-1082,-1188,-1304,-1405,-1534];
>> p = polyfit(x,y,1)
>> plot(x,y,'o',x,polyval(p,x))
3、结果及说明
p =
1.0e+003 *
-1.3253 0.0068
表示拟合的线性公式为 : y = -1325.3x+6.8
把x,y数据输入matlab中,然后输入cftool
1 在界面里点data,输入x和y的值
2 点fitting-Newfit-Polynomial-linearpolynomial,然后直接点apply即可!
Linear model Poly1:
f(x) = p1*x + p2
Coefficients (with 95% confidence bounds):
p1 = -1325 (-1429, -1222)
p2 = 6.788 (-69.34, 82.92)
Goodness of fit:
SSE: 3.082e+004
R-square: 0.9879
Adjusted R-square: 0.9867
RMSE: 55.51
这是运行结果。