• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

最小二乘法在简单线性情况下的例子(C#源代码)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请


假如给定的实验数据点为(Xi,Yi),其中i=0,1,...n,那么 直线与数据点的偏差平方和为

                                          


要使得

取到极小值,则要求:


,    

这两个式子是取得极小值的必要条件,具体运算的过程如下:


对该式求解得到:


以下就是我用C#做的源代码:
public class LeastSquare
    {
        
/// <summary>
        
/// To Draw Linear Least Square Fitting Curve 
        
/// </summary>
        
/// <param name="g">The device on which to draw the curve</param>
        
/// <param name="lp">A list of point used to do the approximation</param>
        public static void LeastSquare2(Graphics g, List<PointF> lp)
        {
            
// Clear the client window with the white color
            g.Clear(Color.White);

            
// Move the drawing origin to the point(200,200)
            g.TranslateTransform(200200);

            
// Use FillEllipse method to draw each point as an ellipse
            foreach (PointF p in lp)
            {
                g.FillEllipse(Brushes.Red, 
new RectangleF(p.X - 5.0f, p.Y - 5.0f10.0f10.0f));
            }


            
int i;
            
float a, b, sumX, sumY2, sumY, sumXY;
            sumX 
= 0.0f;
            sumY2 
= 0.0f;
            sumY 
= 0.0f;
            sumXY 
= 0.0f;

            
// To calculate as per the description of the Mathematical Formular
            for (i = 0; i < lp.Count; i++)
            {
                sumY 
+= lp[i].Y;
                sumY2 
+= lp[i].Y * lp[i].Y;
                sumX 
+= lp[i].X;
                sumXY 
+= lp[i].X * lp[i].Y;
            }

            
// Deduct the coefficients required to do the approximation using the mathematical formular
            a = (lp.Count * sumXY - sumX * sumY) / (lp.Count * sumY2 - sumY * sumY);
            b 
= (sumY2 * sumX - sumY * sumXY) / (lp.Count * sumY2 - sumY * sumY);

            Pen newPen 
= new Pen(Color.Blue, 3.0f);
            g.DrawLine(newPen, 
new PointF(0-/ a), new PointF(360, (360 - b) / a));
        }
    }

下面则是调用上述代码的程序:
private void linearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
// Declare a list of points
            List<PointF> lp = new List<PointF>();

            
// PointF array
            PointF[] pf = new PointF[]{
                
new PointF(0.0f,68.0f),
                
new PointF(10.0f,73.1f),new PointF(20.0f,66.4f),
                
new PointF(30.0f,70.6f),new PointF(40.0f,64.6f),
                
new PointF(50.0f,68.8f),new PointF(60.0f,61.0f),
                
new PointF(70.0f,65.8f),new PointF(80.0f,60.4f),
                
new PointF(90.0f,61.0f)
            };

            
// Using AddRange method of the list to add the pointf array to the end of the list
            lp.AddRange(pf);

            
// Call the static metod LeastSquare2 of LeastSquare Class to proceed
            LeastSquare.LeastSquare2(this.CreateGraphics(), lp);
        }
下面是本程序运行结果的屏幕截图(Screen Shot):

大家如果有什么问题的话请给我留言吧。

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap