Fit a 3D line to 3D point data in Java?

China☆狼群 提交于 2019-12-01 03:49:49

This is easy enough to do, but to write it yourself you will need an eigenvalue solver or a singular value decomposition. Create the nx3 matrix A, of your (x-xbar, y-ybar, z-zbar) data as columns. Save those column means for later, I'll call it V0 = [xbar,ybar,zbar].

Now, compute the eigenvalues and eigenvectors of A'*A, i.e., the 3x3 matrix formed from A transpose multiplied by A.

If this data lies on a line in R^3, then one of those eigenvalues will be significantly larger than the other two eigenvalues. If this is not true, then the orthogonal regression line will not be well estimated.

Take the eigenvector that is associated with the largest eigenvalue of A'*A. Then if V is the corresponding eigenvector, the orthogonal regression line is defined as

V(t) = V0 + t*V

Any point on that line can be given by some value of the parameter t.

Alternatively, compute the singular value decomposition of A, and take the right singular vector which corresponds to the largest singular value of A.

In either event, if you wish to compute the errors for the data points, this would be defined as simply the orthogonal distance to the line in question.

Google for "java linear least squares regression library" and you should find a few options. One is Drej. I have not used this myself, though.

EDIT - I'm not confident this answers the question - I don't know whether 3D data is supported.

It's easy enough to do it if you know the trick: http://www.scribd.com/doc/21983425/Least-Squares-Fit

More dimensions means more coefficients, but they're easy enough to add in. The ideas are all the same.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!