最小二乘直线拟合

#include 
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 {
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< cout<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;

}

你可能感兴趣的:(最小二乘直线拟合)