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

久未见 提交于 2020-01-21 06:53:54

问题


I've spent a decent amount of time trying to hunt down a simple way of doing this - ideally, a magical library exists out there somewhere that will take my set of 3D data points and return 2 points on the best fit line using either orthogonal regression or least squares and also return the error of the fitted line. Does such a thing exist, and if so, where?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/2352256/fit-a-3d-line-to-3d-point-data-in-java

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