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?
Even for 2d distances, it is not in general true that the minimum of
a^2 + b^2is at the same location as the minimum ofa + 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).