利用最小二乘法拟合任意次函数曲线(C#)

///<summary>
    ///用最小二乘法拟合二元多次曲线
    ///</summary>
    ///<param name="arrX">已知点的x坐标集合</param>
    ///<param name="arrY">已知点的y坐标集合</param>
    ///<param name="length">已知点的个数</param>
    ///<param name="dimension">方程的最高次数</param>
   
    public static double[] MultiLine(double[] arrX, double[] arrY, int length, int dimension)//二元多次线性方程拟合曲线
    {
        int n = dimension + 1;                  //dimension次方程需要求 dimension+1个 系数
        double[,] Guass=new double[n,n+1];      //高斯矩阵 例如:y=a0+a1*x+a2*x*x
        for(int i=0;i<n;i++)
        {
            int j;
            for(j=0;j<n;j++)
            {
                Guass[i,j] = SumArr(arrX, j + i, length);
            }
            Guass[i,j] = SumArr(arrX,i,arrY,1,length);          
        }

       return ComputGauss(Guass,n);

    }

    public static double SumArr(double[] arr, int n, int length) //求数组的元素的n次方的和
    {
        double s = 0;
        for (int i = 0; i < length; i++)
        {
            if (arr[i] != 0 || n != 0)         
                s = s + Math.Pow(arr[i], n);
            else
                s = s + 1;
  

阅读更多

没有更多推荐了,返回首页