怎样用matlab做出一元线性回归图形

2024-12-02 13:13:32
推荐回答(2个)
回答(1):

>> clear
>> x=[1656,2122,2864,4033,6099];
>> y=[2112,2170,2291,2456,2759];
>> [P,S]=polyfit(x,y,1)
%P为拟合回归系数即y=P(1)*x+p(2)
P =
1.0e+003 *
0.0001 1.8666
S =
R: [2x2 double]
df: 3
normr: 9.4223

>> [Y,delta]=polyconf(P,x,S)
%给出回归Y的95%的置信区间为[Y-delta,Y+delta]
Y =
1.0e+003 *

2.1090 2.1772 2.2858 2.4569 2.7592
delta =

20.6907 19.8923 19.1147 19.2502 23.2005
>> x1=1600:100:6100;
>> f=polyval(P,x1);
>> plot(x,y,'ro',x1,f,'-')%绘图查看拟合效果
>> hold on
>> plot(x,Y+delta,'*g')
>> plot(x,Y-delta,'*g')%给出拟合的置信区间
希望我的回答能帮助你!

回答(2):

x=[1656,2122,2864,4033,6099];
y=[2112,2170,2291,2456,2759];
p=polyfit(x,y,1);%一次拟合;
yfit=polyval(p,x);%求拟合后的y值;
plot(x,y,'r*',x,yfit,'b-');%画图;
axis([1500 6500 2100 2800]);%坐标轴范围;