MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: best line through a set of 3D points

  • To: mathgroup at
  • Subject: [mg18457] Re: [mg18386] best line through a set of 3D points
  • From: "Richard Finley" <rfinley at>
  • Date: Wed, 7 Jul 1999 00:11:38 -0400
  • Sender: owner-wri-mathgroup at

Look at the add-on package Statistics`LinearRegression` and you can fit 
the linear equation to your data with:
In[1]:= Regress[DATA,{1,x1,x2},{x1,x2}]
assuming that your data is such that the xi and the yi are the independent 
variables and zi is the dependent variable. 

This is just a least squares approach and you should have gotten the same 
result the long way around by assuming the equation:
z = a x + b y + c
and minimizing the equation
F = Sum[ (a xi + b yi + c) - zi )^2, {i,1,n}]
with respect to a, b, and c.  That is D[F,a]==0 etc....
So, for example, after differentiating wrt a you get
Sum[ (a xi + b yi + c) - zi) xi, {i,1,n}] == 0
and so on for b and c.
You can immediately simplify these by using definitions for the mean, 
variance, and cross-correlations  of the xi, yi and zi to get simple 
solutions for a, b, and c.
After you solve for a, b, and c you will see they are the same as in 
Regress above...not sure why it didn't work for you when you tried it??

Hope that helps...RF

>>> <Maarten.vanderBurgt at> 06/30/99 12:13PM >>>
Dear all,

This is not strictly a mathematica question but someone might have a
solution in the form of a mathematica function or so.

I have a set of 3d points, DATA = {{x1,y1,z1},{x2,y2,z3},...}, roughly
occupying a sigar shaped volume in the 3D space.
I want to find the line that best fits these points.

I tried a least squares approach: I assumed the line was going through the
average off all the points in DATA and then I tried to find a vector in 
direction of the line by minimizing the sum of the squares of the 
from the points to the line.
For some reason I end up with a set of 3 equations which only solution is
(0,0,0).  There is probably some sensible reason for this but I did not
manage to figure out why. Maybe someone else knows?

Another approach I tried is averaging the vectors connecting each point in
DATA with the average point of DATA. This average vector could be a good
direction for the best line. But comparing this method in two dimensions
with a least squares fit, I noticed the agreement is not always good. So I
am not sure whether this is the best method.

Does anyone have a solution to this problem or can anyone point me to some
resources (book(s), web page(s)) where I could find a solution.

thanks a lot



Maarten van der Burgt

ICOS Vision Systems
Esperantolaan 9
B-3001 Leuven, Belgium
tel. + 32 16 398220; direct + 32 16 398316; fax. + 32 16 400067
e-mail: maarten.vanderburgt at

  • Prev by Date: Q: ReplaceAll with pattern in pattern
  • Next by Date: Re: Problem with Mathematica 4. Someone can help me ?
  • Previous by thread: Re: Q: ReplaceAll with pattern in pattern
  • Next by thread: Re: best line through a set of 3D points