您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 商业计划书 > 最小二乘法C语言编程
最小二乘法C语言编程#includestdio.h#includestdlib.h#includemath.hintmain(){inti,j,n;floatt[100],R[100];floatt_mean=0.0,R_mean=0.0,t_square=0.0,R_t=0.0;floata,b;printf(最小二乘法确定关系式:R=a+bt\n);printf(输入您测得的数据组数n=);scanf(%d,&n);printf(输入您测得的n组数据t:);for(i=0;in;i++){scanf(%f,&t[i]);/*记录t值*/}printf(输入您测得的n组数据R:);for(i=0;in;i++){scanf(%f,&R[i]);}/*计算t的平均值*/for(i=0;in;i++){t_mean=t_mean+t[i];}t_mean=t_mean/n;/*计算R的平均值*/for(i=0;in;i++){R_mean=R_mean+R[i];}R_mean=R_mean/n;/*计算t的平方*/for(i=0;in;i++){t_square=t_square+pow(t[i],2);}t_square=t_square/n;/*计算R*t的值*/for(i=0;in;i++){R_t=R_t+t[i]*R[i];}R_t=R_t/n;/*计算a的最佳值*/a=(t_mean*R_t-R_mean*t_square)/(pow(t_mean,2)-t_square);/*计算b的最佳值*/b=(t_mean*R_mean-R_t)/(pow(t_mean,2)-t_square);printf(====================================\n);printf(计算结果:\n);printf(a=%.4f\nb=%.4f\n,a,b);printf(关系式:);printf(R=%.4f+%.4ft\n,a,b);printf(=====================================\n);return0;}
本文标题:最小二乘法C语言编程
链接地址:https://www.777doc.com/doc-5160014 .html