设直线为y=kx+b,已知的三个点为(xi,yi),i=1,2,3
F(k,b)=(kx1+b-y1)^2+(kx2+b-y2)^2+(kx3+b-y3)^2需取最小值,求导得:
F'k=2x1(kx1+b-y1)+2x2(kx2+b-y2)+2x3(kx3+b-y3)=0-->
k(x1^2+x2^2+x3^2)+b(x1+x2+x3)=x1y1+x2y2+x3y3
F'b=2(kx1+b-y1)+2(kx2+b-y2)+2(kx3+b-y3)=0--->
k(x1+x2+x3)+3b=y1+y2+y3
记x'=(x1+x2+x3)/3,y'=(y1+y2+y3)/3为平均数
解得:
k=∑(xi-x')(yi-y')/∑(xi-x')^2
b=y'-kx