Home      Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science

6 December 2009
Author: Giorgos Lazaridis
The Least Squares Fitting

What is this Least Square Fitting?

This method is a mathematical procedure for finding the best-fitting line (for linear systems) or curve (for non-linear), for a given set of points.

Using this method, the sum of squares of the offsets is used, instead of the offset absolute value. This means that to find a point on X axis, the Y axis is vertically increased or decreased. This causes sometimes a disproportionate effect on the fit which is not always desirable, but that depends on the problem itself.

To understand this difference look at the following two images. The left one shows the way that the least squares fitting is working. The Y axis is vertically increasing or decreasing until the X value is returned. This is most of the time what the experiments request. The right image shows the perpendicular offsets.

 Vertical offsets Perpendicular offsets

The vertical offsets fitting is more simple and more often used method.

Equations

To calculate the vertical offsets fitting line, you need to make a series of calculations. Suppose that we have taken n pairs of X and Y values:

First you calculate the Sum of X values:

SumX = X1 + X2 + X3 + ...+ Xn

Then you calculate the Sum of X2 values:

SumX2 = X12 + X22 + X32 + ...+ Xn2

Next calculation is the Sum of Y values:

SumY = Y1 + Y2 + Y3 + ...+ Yn

And then, the Sum of X*Y pairs:

SumXY = X1 * Y1 + X2 * Y2 + X3 * Y3 + ...+ Xn * Yn

Using the calculations above, you can directly find the slope and offset of the best fitting line:

 a = n * SumXY - SumX * SumY n * SumX2 - (SumX)2

And:

 b = SumY * SumX2 - SumX * SumXY n * SumX2 - (SumX)2

To find two sets of X-Y points and draw a line, you use the line equation:

Y = a * X + b

You do this for two X-values and you get two Y-values.

Example
For this example, i use the Dr. Calculus Least Squares calculator to do the calculations

Suppose that after an experiment, we acquired the following X-Y pairs of values:

 X Y 6 4 10 9 18 13 25 14 30 17 40 21 45 23 50 27

Now i will calculate the required sums:

SumX = 224

SumX2 = 8110

SumY = 128

SumXY = 4433

And now, i will use the above numbers to find the slope and offset of the line:

a = 0.46

b = 3.07

Using the line equation, if will calculate the Y for two values of X. The first value will be zero (0) and the other 60:

Y0 = a * 0 + b => Y0 = 3.07

Y60 = a * 60 + b => Y0 = 30.67

In the following diagram, the red points indicate the X-Y pairs taken from the experiment, and the orange points indicate the two X-Y pairs calculated using the least square method. The line that connects these two points is the best-fitting line:

Relative pages
• Dr.Calculus: Least Squares calculator
• Dr.Calculus: A calculator for the line equation
• International unit converter
• Dr.Calculus: Least Squares calculator

 Name  Email (shall not be published)  Website Notify me of new posts via emailWrite your comments below:BEFORE you post a comment:You are welcome to comment for corrections and suggestions on this page. But if you have questions please use the forum instead to post it. Thank you.

 At 27 February 2012, 10:13:12 user Giorgos Lazaridis wrote:   [reply @ Giorgos Lazaridis]@Sam it seems that it is not linear. Better use a linear sensor, or else you have to fin the equation that describes this sensor, which is not ax bAt 26 February 2012, 17:51:38 user Sam wrote:   [reply @ Sam]Hi Zach, can You help me ? how can i calculating the temperature with my sensor (i use 10bit adc,voltage divider 5,6k ( 5V--[div]--NTC-GND) ? i measuring some resistance value, what here is:100°/200R; 60°/690R; 36°/1,7k; 20°/3,37k ; 8°/6,1k; -10°/15k; -15°/22k . You see, this is far non-linear device. ? regards:SamAt 4 July 2011, 12:47:19 user prasad wrote:   [reply @ prasad]It is very useful those who are beginners.We can easily understand & remembering .Thanking you for these type of support for beginersAt 28 December 2010, 4:05:11 user Zach wrote:   [reply @ Zach]This version only works for linear lines. The matrix version of least squared fit (A^-1*A*x=A^-1*b) can work for any function that can be converted into a polynomial form (not just the linear y=mx+b).

 HOT in heaven!

 NEW in heaven! New Theory: AC electric motor working principle