Example of least square method in simple linearity (C # source code)

Source: Internet
Author: User

If the given experimental data point is (XI, Yi), where I =,... n, then the sum of squared deviation between a straight line and a data point is

To make

To obtain the minimum value, the following requirements are required:

,

These two formulas are necessary to obtain the minimum value. The specific operation process is as follows:

The formula is obtained as follows:


Here is what I do with C #Source code:

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 ));
}
}

The following is the call of the above Code Of Program : 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 , 68366f ),
New Pointf ( 10.0f , 73.1f ), New Pointf ( 20366f , 66.4f ),
New Pointf ( 30366f , 70.6f ), New Pointf ( 40f , 64.6f ),
New Pointf ( 50366f , 68.8f ), New Pointf ( 60366f , 61.0f ),
New Pointf ( 70.0f , 65.8f ), New Pointf ( 80366f , 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 );
}

The following is the screen shot of the program running result ):

If you have any questions, please leave a message.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.