Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

In the Wolfram Demonstration Fermat Point for Many Points, it appears that the geometric median is being calculated for an arbitrary set of five manipulable points. How might one extend this demonstration to computing the median of an arbitrary list of points?

share|improve this question
    
The linked demonstration covers the cases of 3 to 10 points, so it must give the general algorithm. Did you download the Mathematica notebook giving the code from the demonstration site? –  m_goldberg Jun 25 '13 at 6:05
add comment

1 Answer 1

up vote 1 down vote accepted

This is modified code of the Demonstration. In this version you add/remove locators by clicking mouse while holding CTRL (command on mac) key. It wroks due to LocatorAutoCreate -> True option. You should check that it makes sense, I am not sure why author have chosen a different implementation.

Manipulate[
 Module[{s, d, simp, x, y},
  s = Nest[(simp = #; d = Map[Norm[# - simp] &, p];
      Total[Map[First[#]/Last[#] &, Thread[List[p, d]]]]/
       Total[1/d]) &, Total[p]/Length[p], 10];
  Show[{DensityPlot[
     Total[Norm[{x, y} - s] & /@ p], {x, -3, 3}, {y, -3, 3}, 
     ColorFunction -> "Aquamarine", Frame -> False, ImageSize -> 500],
     Graphics[{Gray, Line[{s, #}] & /@ p, Black, Disk[#, .1] & /@ p, 
      Red, Disk[s, .05], Orange, Disk[b = Plus @@ p/Length[p], .05], 
      Style[{
        Text["sums of distances", {0, -2.2}, {0, 0}],
        Text[
         "from Fermat point (red): " <> 
          ToString[Total[Norm[# - s] & /@ p]], {0, -2.8}, {0, 0}], 
        Text["from centroid (orange): " <> 
          ToString[Total[Norm[# - b] & /@ p]], {0, -2.5}, {0, 0}]}, 
       20, Black]}, PlotRange -> 3, ImageSize -> 500]}]],
 {{p, Table[1. {Cos[k 2 \[Pi]/11], Sin[k 2 \[Pi]/11]}, {k, 5}]}, 
  Locator, LocatorAutoCreate -> True}]

enter image description here

share|improve this answer
    
Very nice, thank you! Is there some way I can directly feed a list of points to the function and calculate the coordinates for the red point? –  Terry Jun 25 '13 at 2:30
    
How is the calculation of the geometric median actually being performed here? –  Terry Jun 25 '13 at 2:33
    
@Terry yes, - replace Table[...] with your list of points. –  Vitaliy Kaurov Jun 25 '13 at 2:33
    
@Terry: I think it's Weiszfeld's algorithm run for 10 iterations. –  Rahul Narain Jun 25 '13 at 2:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.