// testliner.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
/**
* 计算方程系数 y=ax+b 中的系数
*/
void validateCoefficients(double x[],double y[],int pn,double &a,double &b)
{
/* 待估参数 */
//double a;
//double b;
/* 数据点 */
//int pn; //数据点个数pointNum
//double x[];
// double y[];
/* 最小二乘法估计参数 */
double sumX = 0;
double sumY = 0;
double sumXX = 0;
double sumXY = 0;
double sumYY = 0;
for(int i = 0; i < pn; i++) {
sumX += x[i];
sumY += y[i];
sumXX += x[i]*x[i];
sumXY += x[i]*y[i];
sumYY += y[i]*y[i];
}
if (pn >= 2)
{
double xBar = sumX / pn;
double yBar = sumY / pn;
a = (pn * sumXY - sumX * sumY) / (pn * sumXX - sumX * sumX);
b = yBar - a * xBar;
}
else
{
a = b = 0;//Float.NaN;
}
}
int main(int argc, char* argv[])
{
//double x[]={1,5,7};
//double y[]={1,5,7};
//double y[]={4,8,10};
double x[]={100,200};
double y[]={114.05357 ,114.05353666666667};
double a=0;
double b=0;
int pn=2;
// (100,100)(114.05357 ,22.664986666666667 )
//(200,400)(114.05353666666667 ,22.665073333333332 )
validateCoefficients(x,y,pn,a,b) ;
//double x2=0; //150;
//double x1=1000; //150;
//double y1=a*x2+b;
double x1=0; //150;
double y1=a*x1+b;
double x2=1000; //150;
double y2=a*x2+b;
printf("a:%12.10lf,b:%12.10lf\n",a,b);
printf("x1:%12.10lf,y1:%12.10lf\n",x1,y1);
printf("x2:%12.10lf,y2:%12.10lf\n",x2,y2);
printf("Hello World!\n");
return 0;
}