# [SciPy-user] Fitting Problem with line in 3D-space

Pauli Virtanen pav@iki...
Sat Nov 8 06:59:49 CST 2008

Sat, 08 Nov 2008 03:53:39 +0100, Charles Monty Burns wrote:

> Hello,
>
> I am trying to get an axis of a cylinder in 3D-space using the leastsq
> method.
>
> My modell-equation is:
>
> \vec u \times (\vec r - \vec r_0) = \vec 0
>
> u ... the direction vector
> r ... the independent vector
> r_0 ... one position vector on the line
>
> Can somebody tell me how to construct the functions leastsq is need?
>
> With simple function like y=x*... its very simple ... but not with that
> line

Like this, for example:

-----------------------------------------
from scipy.optimize import leastsq
import numpy as np

points = np.loadtxt('points.dat') # data file with 3 columns

def params(p):
return p[:3]/np.linalg.norm(p[:3]), p[3:]

def model(p):
u, v0 = params(p)
return np.cross(points - v0, u).ravel()

result, ier = leastsq(model, [1, 0, 0, 0, 0, 0])
u, v0 = params(result)

print u
print v0
-----------------------------------------

--
Pauli Virtanen