以下是最小二乘法类的实现:
public class MatrixEquation
{
private double[,] gaussMatrix;
private int coe;
public MatrixEquation()
{
}
public MatrixEquation(double[] arrX, double[] arrY, int n)
{
coe = n;
gaussMatrix= GetGauss(GetXPowSum(arrX, n), GetXPowYSum(arrX, arrY, n), n);
}
public double[,] GetGaussMatrix()
{
return gaussMatrix;
}
public double[] GetResult()
{
return ComputeGauss(gaussMatrix, coe);
}
///
///
计算获取
x
散点的幂次和数组
///
/// x
散点序列
///
函数拟合次数
///
protected double[] GetXPowSum(double[] arrX, int n)
{
int m = arrX.Length;//X
散点的个数
double[] xPow = new double[2 * n + 1]; //
存储
X
散点的幂次值
for (int i = 0; i
{
if (i == 0)
{
xPow[i] = m;
}
else