Given 4 lines in 3D (represented as a couple of points), I want to find the point in space which minimizes the sum of distances between this point and every line.
I'm trying to find a way to formulate this as a Least Squares Problem, but I'm not quite sure as to how I should. I'm currently trying to use the definition of distance provided at: http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
Any ideas?
I made a program in Mathematica for calculating the point coordinates.
The result is a large algebraic formula. I uploaded it to ideone
for you.
Here is the program, in case you have Mathematica at hand:
(*Load package*)
Needs["VectorAnalysis`"]
(*Define four lines, by specifying 2 points in each one*)
Table[p[i, j] = {x[i, j], y[i, j], z[i, j]}, {i, 4}, {j, 2}];
(*Define the target point*)
p0 = {x0, y0, z0};
(*Define a Norm function // using Std norm squared here*)
norm[a_] := a[[1]]^2 + a[[2]]^2 + a[[3]]^2
(*Define a function for the distance from line i to point v
used http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html (11) *)
d[i_, v_] := norm[Cross[(v - p[i, 1]), (v - p[i, 2])]]/norm[p[i, 2] - p[i, 1]]
(*Define a function for the sum of distances*)
dt[p_] := Sum[d[i, p], {i, 4}]
(*Now take the gradient, and Solve for Gradient == 0*)
s = Solve[Grad[dt[p0], Cartesian[x0, y0, z0]] == 0, {x0, y0, z0}]
(* Result tooooo long. Here you have it for downloading
http://ideone.com/XwbJu *)
RESULT
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With