matlab polyfit 相关系数

matlab中有没有直接求最小二乘法拟合的相关系数的命令?
我记得好像以前在课本里面看到过
就是给定两组数据 通过polyfit进行一次线性回归后 怎么得到相关系数r
好像有个命令很简单的

第1个回答  推荐于2018-03-11
a=polyfit(x,y,1);
z=polyval(a,x);
R=corrcoef(x,y);
所示为线性拟合
这样得到的R即为相关系数矩阵,其中
R(1,2)=R(2,1)为相关系数,其值在[-1,1]之间,1表示最大的正相关,-1表示绝对值最大的负相关。
最近开始看这方面的,希望能帮到你。本回答被提问者采纳
第2个回答  2010-11-27
用cftool
>>cftool
回车,出现一个对话框。Data输入相应的x,y,然后create data set;fitting,选择提供的各种模型,逐个试试,总能找到残差最小的,相关系数最大的一种模型。

给个例子。
clc;clear
x=-1:0.1:1
for k=1:length(x)
y(k)=x(k)^2+0.1*rand
end
[p,s]=polyfit(x,y,2)
s.R
plot(x,y,'o',x,polyval(p,x))

结果:
p =

1.0072 0.0091 0.0439

s =

R: [3x3 double]
df: 18
normr: 0.1153

ans =

-2.2509 -0.0000 -3.4208
0 2.7749 0.0000
0 0 -3.0492

s是个结构数组。
S contains fields for the triangular factor (R) from a QR decomposition of the Vandermonde matrix of X, the degrees of freedom (df), and the norm of the residuals (normr).
第3个回答  2010-11-26
好复杂啊!我没法帮你本回答被网友采纳