Let the points be given as (xi,yi) for i=1,…,n, where n≥2
is the number of points.
We will furthermore require that the xi are not all equal.
Again, we will look for a least squares definition of best.
We seek a line y=ax+b that minimizes the following error function:
J=i=1∑n(axi+b−yi)2.
Note this very important observation: Translating the points and the line by the same
amount does not change the value of the error function.
This means that we can translate the points so their center of mass is at the origin,
compute the best fitting line for the translated points,
and then translate the line and points back to the original position.
So let us set
x~i=xi−xˉandy~i=yi−yˉ
where xˉ=n1sx and yˉ=n1sy
with sx=∑i=1nxi and sy=∑i=1nyi.
We now have ∑i=1nx~i=∑i=1ny~i=0,
so we can apply the results from the
previous posts
to find the best fitting line y~=a~x~ for the
translated points and we get
a~=∑i=1nx~i2∑i=1nx~iy~i.
Rewriting the equation for the line,
y~=a~x~⇔y−yˉ=a~(x−xˉ)⇔y=a~x+yˉ−a~xˉ,
we see that the line we seek is given by y=ax+b with