#include <iostream>
using namespace std;
int nihe(double *x,double *y,int N)
{
double a0=0;
double a1=0;
double sum_x_square=0;
double sum_x=0;
double sum_y=0;
double sum_x_y=0;
double x_sum_square=0;
for (int i=0;i<N;i++)
{
sum_x_square+=x[i]*x[i];
sum_y+=y[i];
sum_x+=x[i];
sum_x_y+=x[i]*y[i];
}
x_sum_square=sum_x*sum_x;
a0=((sum_x_square*sum_y)-(sum_x*sum_x_y))/(N*sum_x_square-x_sum_square);
a1=((N*sum_x_y)-(sum_x*sum_y))/(N*sum_x_square-x_sum_square);
cout<<a0<<endl;
cout<<a1<<endl;
return 0;
}
int main()
{
double x[]={1,2,3,4,5};
double y[]={1,3,3,5,5};
int s;
s=nihe(x,y,5);
system("pause");
return 0;
}
最小二乘直线拟合
最新推荐文章于 2024-01-20 15:51:54 发布