利用C#实现最小二乘法

最小二乘法可根据坐标点的信息拟合出一条最优直线,y=bx+a,保证每个坐标点到直线的距离之和达到最小值。

前提条件是先筛选出有效的坐标点,再进行算法处理。

代码如下:(实测可用)

/****************声明定义*************************/

            float[] distance = new float[271];  

            float length;

            float[] ArrPoint_X = new float[270];
            float[] ArrPoint_Y = new float[270];

            ArrayList pointX = new ArrayList();
            ArrayList pointY = new ArrayList();
            float lengthX;
            float lengthY;
            float theta;
            float dis_length;
            float dis_theta;

/****************函数实现*************************//最小二乘法,拟合直线
            int ArrPoint_Count = 0;
            for (int i = (135 - 23 * 3); i <= (135 + 23 * 3); i++)   //边缘检测
            {
                length = distance[i];
                if ((length > 100) && (length < User_length))
                {
                    theta = (float)((i / 3) + 45);          //极坐标系往直角坐标系转换
                    lengthX = length * (float)(Math.Cos(Math.PI * theta / 180));
                    lengthY = length * (float)(Math.Sin(Math.PI * theta / 180));
                    pointX.Add(lengthX);
                    pointY.Add(lengthY);
                    ArrPoint_X[ArrPoint_Count] = lengthX;
                    ArrPoint_Y[ArrPoint_Count] = lengthY;
                    ArrPoint_Count++;
                }
            }

            float averagex = 0, averagey = 0;
            foreach (float i in pointX)
            {
                averagex += i;
            }
            foreach (float j in pointY)
            {
                averagey += j;
            }
            averagex /= pointX.Count;        //  取X坐标的平均数
            averagey /= pointY.Count;        //  取Y坐标的平均数

            //经验回归系数的分子与分母
            float numerator = 0;
            float denominator = 0;
            for (int i = 0; i < pointX.Count; i++)
            {
                numerator += (ArrPoint_X[i] - averagex) * (ArrPoint_Y[i] - averagey);
                denominator += (ArrPoint_X[i] - averagex) * (ArrPoint_X[i] - averagex);
            }
            //回归系数b(Regression Coefficient)   y = bx + a ;
            float RCB = numerator / denominator;
            //回归系数a
            float RCA = averagey - RCB * averagex;

 

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,可以使用最小二乘法进行函数拟合。最小二乘法是一种常用的数学统计方法,用于通过拟合函数来找到最佳的曲线拟合数据点。通过最小二乘法,可以得到拟合函数的参数,从而实现对数据的拟合。 在C#中,可以使用MathNet.Numerics库来实现最小二乘法拟合。该库提供了一些用于数值计算和科学计算的功能,包括最小二乘法拟合。 首先,需要在项目中引用MathNet.Numerics库。然后,可以使用库中的Fit.Polynomial方法进行多项式拟合。该方法接受两个参数,分别是待拟合的数据点和拟合的阶数。 以下是一个使用最小二乘法进行多项式拟合的示例代码: ```csharp using MathNet.Numerics; using MathNet.Numerics.LinearRegression; // 定义待拟合的数据点 double[] x = { 1, 2, 3, 4, 5 }; double[] y = { 2, 4, 6, 8, 10 }; // 定义拟合的阶数 int degree = 2; // 进行多项式拟合 double[] parameters = Fit.Polynomial(x, y, degree); // 打印拟合的结果 for (int i = 0; i < parameters.Length; i++) { Console.WriteLine($"参数{parameters.Length - i - 1}: {parameters[i]}"); } ``` 在上述示例代码中,我们定义了待拟合的数据点x和y,以及拟合的阶数degree。然后,使用Fit.Polynomial方法进行多项式拟合,并将拟合结果存储在参数数组parameters中。最后,我们通过遍历参数数组,打印出拟合的结果。 注意:以上示例代码仅仅是一个示例,实际使用时需要根据具体需求进行相应的修改和调整。另外,MathNet.Numerics库还提供了其他拟合方法,如指数拟合、幂指数拟合等,你可以根据需要选择适合的方法进行拟合。<span class="em">1</span> #### 引用[.reference_title] - *1* [C#最小二乘法函数拟合.rar](https://download.csdn.net/download/qq_31110355/15908024)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值