最小二乘法拟合直线,对于大多数人都不是很陌生,直线方程 y=kx+b,令sum((y-kx-b)^2),分别对k和b求导便可求出最佳的参数,但是如果遇到诸如“X=5”这样的直线方程,又该如何呢?这时候就不再适用了,参考如下描述http://mathforum.org/library/drmath/view/51809.html,这样拟合出来的才是直线的一般方程。
Let the line sought be A*x + B*y + C = 0, with no restriction on A, B, and C, except not both of A and B are zero. Then the distance from a point (x[n],y[n]) to this line is given by d[n] = |A*x[n]+B*y[n]+C|/sqrt(A^2+B^2). Then the sum of the squares of the distances of the r points from the line is F(A,B,C) = SUM d[n]^2 = SUM (A*x[n]+B*y[n]+C)^2/(A^2+B^2). Here and henceforth, the sums run over the range 1 <= n <= r. Now take the partial derivatives of F with respect to A, B, and C, and set them equal to zero. This will give you a set of three simultaneous nonlinear equations in A, B, and C, and you need to solve them. They will be polynomial, of degree 3. When I did this, the equations turned out to be SUM (A*x[n]+B*y[n]+C)*(A*C-B^2*x[n]+A*B*y[n]) = 0, SUM (A*x[n]+B*y[n]+C)*(B*C+A*B*x[n]-A^2*y[n]) = 0, SUM A*x[n]+B*y[n]+C = 0. These can be rewritten as polynomials in A, B, and C with coefficients involving r = SUM 1, s = SUM x[n], t = SUM y[n], u = SUM x[n]^2, v = SUM x[n]*y[n], w = SUM y[n]^2, which are quantities you can compute from your data points. -v*A^2*B-s*A^2*C+(u-w)*A*B^2-2*t*A*B*C-r*A*C^2+v*B^3+s*B^2*C = 0, v*A^3+(w-u)*A^2*B+t*A^2*C-v*A*B^2-2*s*A*B*C-t*B^2*C-r*B*C^2 = 0, s*A+t*B+r*C = 0. C can be eliminated using the last equation, which is the same as C = -(s*A+t*B)/r. That will leave you with two cubic equations in two unknowns A and B. They turn out to be equivalent to the single quadratic equation (r*v-s*t)*A^2 + (s^2-t^2-r*u+r*w)*A*B - (r*v-s*t)*B^2 = 0. A^2 + [(s^2-t^2-r*u+r*w)/(r*v-s*t)]*A*B - B^2 = 0. Now unless r*v - s*t = 0, you can solve this equation for -A/B, the slope of the line. Then you can find C/B, too, and thus the equation of the line. You will get two distinct solutions. One is the minimum you seek, and the other is a saddle point of F(A,B,C). You can tell which is the minimum by evaluating F(A,B,C) at each one and choosing the solution that gives you the smaller value. If r*v = s*t, but the coefficient s^2-t^2-r*u+r*w is nonzero, then the minimum will be achieved for either A = 0 or B = 0. Figure out C for both of these possibilities, and substitute them into F(A,B,C). The one that gives you the lesser value is your solution. If r*v = s*t and s^2-t^2-r*u+r*w = 0, then any line through the center of mass (s/r,t/r) will give the same value of F as any other, and all are tied for being the best.