Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimising distance: distance formula

I am writing a program in C. I want to find a solution by minimizing expression

D1+D2+......+Dn

where Di's are distances calculated by distance formula between 2 points. The above expression is in x & y variables

Now I will differentiate this expression and find the solution. My doubt is:

since in the above expression, all Di's will occur as square roots which will be difficult to solve. So instead we can solve for this expression:

D1^2 + D2^2 + ......+ Dn^2

Will the answer produced by the above expression will be same as that would have been produced by solving the original one?

I have checked for simple test cases such as n=2. It produces the correct answer. Is it true in general?

If not, how this problem can be solved?

like image 599
nowonder Avatar asked Dec 07 '09 12:12

nowonder


People also ask

What is the 3d distance formula?

The distance formula states that the distance between two points in xyz-space is the square root of the sum of the squares of the differences between corresponding coordinates. That is, given P1 = (x1,y1,z1) and P2 = (x2,y2,z2), the distance between P1 and P2 is given by d(P1,P2) = (x2 x1)2 + (y2 y1)2 + (z2 z1)2.


3 Answers

Even for 2d distances, it is not in general true that the minimum of a^2 + b^2 is at the same location as the minimum of a + b. It may be true for some very specific limited set of problems, of course. If you're trying to find a counterexample, note that the squares overpenalize long distances; if you construct an example with a minimum containing at least one long distance, you've a good chance that the sum of squares has a different minimum.

What is the problem you are trying to solve? It's quite possible that for your problem the distinction doesn't matter, of course; or that the minimum of the sum of squares is a cheaper problem and an easier first approximation to a final solution.

It may be obvious, but if the various distances are unrelated, then for each individual distance the square is minimal when the distance is and thus the sum of unrelated distances is minimal where the sum of the squares is.

Edit Post update: You're trying to find a centroid with the limitation that it lies on a particular line. In general outlines then: you've only one degree of freedom, and you can do plain differentiation. However, the result will have a sum of fractions with sqrt in the denominator; solving that algebraically in the general case isn't possible (AFAIK). I'm not 100% positive, but I think you're in luck in that your distance-sum has no local minimum except the global one; in that case newton's method will converge reliably and fast.

So, if you can verify the assumption that there is only one local minimum, you're home free, and even if you can, you can achieve a pretty good result fairly reliably and detect when it goes wrong simply by comparing your newton-method computed minimum with a few reality check points (say, the orthogonal projection of each point onto the line).

like image 142
Eamon Nerbonne Avatar answered Oct 20 '22 01:10

Eamon Nerbonne


You have not tested enough. Minimizing D1 + D2 is not the same as minimizing D1^2 + D2^2 in general, although it may be for some particular D1 and D2.

EDIT after you reminded me that you are only interested in distances in the plane:

In the case where D1 and D2 are distances in the geometric plane, the point in the plane that minimizes D1^2 + D2^2 also minimizes D1 + D2, but it breaks down with three points.

Try it with the three points (0,0), (1,0) and (10, 0): Minimizing |x|+|x-1|+|x-10| is not the same as minimizing x^2+(x-1)^2+(x-10)^2

like image 37
Pascal Cuoq Avatar answered Oct 20 '22 01:10

Pascal Cuoq


You are a bit unclear about the origin of your D1, D2, .. Dn but I assume you have a set of points P1, P2, ..., Pn in the x-y plane, and you want to find the point p0=(x0,y0) that minimises the sum of the distances between each point P1.. Pn and p0.

So your D1.. Dn are actually:

D1 = sqrt((x0-x1)^2 + (y0-y1)^2)
D2 = sqrt((x0-x2)^2 + (y0-y2)^2)
..
Dn = sqrt((x0-xn)^2 + (y0-yn)^2)

where x1 .. xn and y1 ... yn are known and x0, y0 are unknown. And you want to minimise D0:

D0 = D1+D2+......+Dn

If this is correct then you want to find the Geometric median. The wikipedia article should help you produce a solution.

Update: you state in a comment that point P0 should lie on a given line (please add this to your original problem statement). This means you can rewrite y0 as a function of x0:

y0 = a*x0 + b

where a and b are given. This reduces the complexity of your distance functions and makes derivation a serious possibility.

D1 = sqrt((x0-x1)^2 + (ax0+b-y1)^2)
D2 = sqrt((x0-x2)^2 + (ax0+b-y2)^2)
..
Dn = sqrt((x0-xn)^2 + (ax0+b-yn)^2)

But if the amount of points n is not too large I would just do a brute-force search* in the area of the line where x is close to the mean of x1 .. xn to find the point x-, y0 that minimises D0.

like image 25
jilles de wit Avatar answered Oct 19 '22 23:10

jilles de wit