最小二乘法在簡單線性情況下的例子(C#原始碼)

來源:互聯網
上載者:User

假如給定的實驗資料點為(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(200, 200);

            // 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.0f, 10.0f, 10.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, -b / 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):

大家如果有什麼問題的話請給我留言吧。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.