欢迎来到天天文库
浏览记录
ID:59339913
大小:16.50 KB
页数:2页
时间:2020-09-04
《线性拟合C语言算法.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、最小二乘法拟合一条直线(C语言代码) #include#defineN10 //N为要拟合的数据的个数 floatX[10]={1.9,0.8,1.1,0.1,-0.1,4.4,4.6,1.6,5.5,3.4};floatY[10]={0.7,-1.0,-0.2,-1.2,-0.1,3.4,0.0,0.8,3.7,2.0};floatK=0; //拟合直线的斜率floatR=0; //拟合直线的截距floatx_sum_average=0; //数组X[N]个元素
2、求和并求平均值floaty_sum_average=0; //数组Y[N]个元素求和并求平均值floatx_square_sum=0; //数组X[N]个个元素的平均值floatx_multiply_y=0; //数组X[N]和Y[N]对应元素的乘机floatSqure_sum(floatc[N]);floatSum_Average(floatd[N]);floatX_Y_By(floatm[N],floatn[N]);floatSqure_sum(floatc[N]);voidLine_Fit(void);voidLine_Fit(void){
3、 x_sum_average=Sum_Average(X); y_sum_average=Sum_Average(Y); x_square_sum=Squre_sum(X); x_multiply_y=X_Y_By(X,Y); K=(x_multiply_y-N*x_sum_average*y_sum_average)/(x_square_sum-N*x_sum_average*x_sum_average); R=y_sum_average-K*x_sum_average; printf("K=%f",K); printf("R=%f",R)
4、;}floatSum_Average(floatd[N]){unsignedinti=0;floatz=0;for(i=0;i5、z;}voidmain(void){ Line_Fit();}
5、z;}voidmain(void){ Line_Fit();}
此文档下载收益归作者所有