采用线性回归的方式,Y=bx+a,求出系数
#include
#include
int main()
{
//float x[6]={0.0,1.0,2.0,3.0,4.0,5.0};
//float y[6]={1.1,1.9,3.1,3.9,4.4,4.9};
/*float x[6]={0.0,1.0,2.0,3.0,4.0,5.0};
float y[6]={0.0,1.0,2.0,3.0,4.0,5.0};*/
float x[6]={0.0,1.0,2.0,3.0,4.0,5.0};
float y[6]={1.0,1.0,1.0,1.0,1.0,1.0};
float a,b,mxy,xx,yy,x2,x22;
int i;
a=b=mxy=xx=yy=x2=x22=0.0;
for(i&#61;0;i<6;i&#43;&#43;)
{
mxy&#61;6.0*x[i]*y[i]&#43;mxy;
xx&#61;1.0*x[i]&#43;xx;
yy&#61;1.0*y[i]&#43;yy;
x2&#61;1.0*x[i]*x[i]*6.0&#43;x2;
x22&#61;1.0*x[i]&#43;x22;
}
b&#61;1.0*(mxy-xx*yy)/(x2-x22*x22);
a&#61;1.0*yy/6.0-b*xx/6.0;
printf("Y&#61;%0.2fx&#43;%0.2f\n",b,a);
system("pause");
return 0;
}
Y&#61;0.00x&#43;1.00