C#使用MathNet库来对进行曲线拟合


 下面是用来求取一条直线和一条拟合曲线交点的代码

        /// 
        /// 拟合曲线所筛选的点的个数
        /// 
        const int CurveNums = 10;

        /// 
        /// 拟合曲线的阶数
        /// 
        public int m =2;

        /// 
        /// 取直线上的点的个数
        /// 
        const int XNums = 50;
        /// 
        ///  最小二乘法拟合曲线
        /// 
        /// X轴数组
        /// Y轴数组
        /// 阶数
        /// 返回曲线方程的各阶系数(由高阶到低阶,一般m=2)
        public double[] FittingCurveByLeastSquare(double[] X, double[] Y, int m = 2)
        {
            /// https://blog.csdn.net/qq_23062949/article/details/119700640?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.no_search_link
            double[] res = new double[m + 1];
            if (X.Length > m && Y.Length > m)
            {
                res = Fit.Polynomial(X, Y, m);
            }
            return res;
        }

        /// 
        /// 计算直线方程
        /// 
        /// 直线起点
        /// 直线的角度
        /// 返回k,b的数组
        public double[] CalculateLine(Point StartPoint, double angle)
        {
            double k = Math.Tan(angle / 180 * Math.PI);
            double b = StartPoint.Y - k * StartPoint.X;
            return new double[] { k, b };
        }

        /// 
        /// 计算交点
        /// 
        /// 直线的k,b
        /// 拟合曲线的系数数组
        /// 直线上的点的X坐标
        /// 阶数
        /// 
        public EPoint CalculateInterPoint(double[] LineResult, double[] CurveResult, double[] LineX, int m)
        {
            EPoint InterPoint = new EPoint();
            if (LineResult.Length == 2 && CurveResult.Length == (m + 1))
            {
                double k = LineResult[0];
                double b = LineResult[1];
                for (int i = 0; i < LineX.Count(); i++)
                {
                    double x = LineX[i];
                    double y_Line = k * x + b;
                    double y_Curve = 0;
                    for (int n = 0; n <= m; n++)
                    {
                        y_Curve += CurveResult[n] * Math.Pow(x, n);
                    }
                    bool IsSuccessFind = false;
                    for (int t = 1; t <= 50; t++)
                    {
                        if (Math.Abs(y_Line - y_Curve) < 5)
                        {
                            InterPoint = new EPoint((int)x, (int)y_Line);
                            IsSuccessFind = true;
                            break;//跳出内循环
                        }
                    }
                    if (IsSuccessFind)
                    {
                        break;//跳出外循环
                    }
                }
            }
            return InterPoint;
        }

        /// 
        /// 计算R^2,R^2这个值越接近1,说明拟合出来的曲线跟原曲线就越接近
        /// 
        /// 实际的Y
        /// 代入拟合曲线方程得到的Y
        /// 返回R^2
        public double CalculateRSquared(double[] Y, double[] Ytest)
        {
            double RSquared = GoodnessOfFit.RSquared(Y, Ytest);
            return RSquared;
        }
C