Linear Least Squares: Solving 3D Data Points in C++

Click For Summary
SUMMARY

The discussion focuses on fitting a line through a set of 3D data points using linear least squares in C++ with the Eigen 2.0 matrix library. The user outlines the process of setting up matrices A and b to solve the equation Ax = b, transitioning to a square matrix format A^t*Ax = A^t*b, and employing Cholesky decomposition for solution. The challenge lies in arranging the matrices for 3D data points, as opposed to the simpler 2D case. Suggestions for improving numerical stability, such as mean subtraction and using parametric equations, are also discussed.

PREREQUISITES
  • Understanding of linear least squares methodology
  • Familiarity with matrix operations and Cholesky decomposition
  • Proficiency in C++ programming
  • Experience with the Eigen 2.0 matrix library
NEXT STEPS
  • Research how to set up matrices A and b for 3D linear least squares fitting
  • Learn about parametric equations for 3D data representation
  • Explore techniques for enhancing numerical stability in matrix computations
  • Investigate alternative libraries for linear algebra in C++, such as Armadillo or Blaze
USEFUL FOR

Data scientists, C++ developers, and anyone involved in 3D data analysis or linear regression modeling will benefit from this discussion.

Lindley
Messages
6
Reaction score
0
I have a simple problem. I have a set of 3D data points and I want to fit a line through them using linear least squares. I understand the basic approach required: set up two matrices such that Ax = b, then make it a square matrix A^t*Ax = A^t*b, then solve for x using a Cholesky decomposition. (I'm working in C++ using the Eigen 2.0 matrix library.)

The problem is that I'm not sure how to set up A and b to begin with. This would be simple if the data points were in 2D: b would be a column vector of all the ys, and A would be an Nx2 matrix with [x, 1] on each row. Then the unknowns would be x = [m, b] for the normal y = mx+b line.

But with 3D points, it's less clear to me how I have to arrange things.

Also, any suggestions for how to make the operation more numerically stable (such as subtracting off the mean from each data point, I know that one at least) would be welcome.
 
Physics news on Phys.org
It depends on what type of line you have such as can it be curved or is it only straight. If it is straight or you are trying to fit a straight line to the data then the following way could be possible, but only if the data points can be numbered from 1 to n. If you can do this then you can split your data into 3 groups of parametric equations of (t,x), (t,y), (t,z) which are all 2D and you can figure it out from there. After you get all three you would have the parametric function <x(t),y(t),z(t)>.
 
I only need straight lines.

The parametric approach makes sense, thanks, I'll give that a try!
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K