最小二乘法的控制台实现,给出一系列的点坐标,能够通过这段代码拟合成一段直线,算出斜率和截距,给出方程式
static bool fnCalculateLineKB(ref List mapFoldList, ref double fk, ref double fb)
{
double fmX = 0.0; double fmY = 0.0; double fmXX = 0.0; double fmXY = 0.0; double fn = 0.0;
if (0 == mapFoldList.Count)
{
return false;
}
long lCount = mapFoldList.Count;
if (lCount < 2)
{
return false;
}
fn = lCount;
foreach (sAD_PWR iter in mapFoldList)
{
fmX += iter.fAD;
fmY += iter.fPWR;
fmXX += iter.fAD * iter.fAD;
fmXY += iter.fAD * iter.fPWR;
}
if (Math.Abs(fmX * fmX - fmXX * fn) < 0.000001)
{
return false;
}
fk = (fmY * fmX - fmXY * fn) / (fmX * fmX - fmXX * fn);
fb = (fmY - fmX * fk) / fn;
return true;
}
2022-03-12 16:35:27
899B
最小二乘法
1