Linear least squares

  • Thread starter Lindley
  • Start date
  • #1
7
0

Main Question or Discussion Point

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.
 

Answers and Replies

  • #2
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)>.
 
  • #3
7
0
I only need straight lines.

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

Related Threads on Linear least squares

  • Last Post
Replies
6
Views
4K
  • Last Post
Replies
8
Views
2K
  • Last Post
Replies
4
Views
3K
  • Last Post
Replies
2
Views
2K
Replies
2
Views
4K
Replies
1
Views
627
Replies
2
Views
15K
Replies
9
Views
16K
Replies
5
Views
717
Top