2
$\begingroup$

The code is suppose to show the movement of optimization points from the first until the minimum point is found on the Contour Plot. However, the plot that I have is not showing the correct movement as expected in the example.

The code as per example from this youtube video: https://www.youtube.com/watch?v=vXzfnExR_t4

f = Reap[FindMinimum[{(-10 x)/((x - 1)^2 + (y - 1)^2 + 1) - 
   2000/((x + 1)^2 + (y + 1)^2 + 1), 
  x^2 + y^2 > 2}, {{x, 1.5}, {y, 0}},
 StepMonitor -> (Sow[{x, y}])];];
ContourPlot[-(100/((x - 1)^2 + (y - 1)^2 + 1)) - 
200/((x + 1)^2 + (y + 2)^2 + 1), {x, -3, 3}, {y, -4, 2},
RegionFunction -> (#1^2 + #2^2 > 2 &), Contours -> 10,
Epilog -> {Red, Text["Constrained Area", {0, 0}], Line[f[[2]]], 
Yellow, Map[Point, f[[2]]], Blue, PointSize[0.02], 
Point[f[[2, 1, 1]]]}, ContourLabels -> True]

However I can't reproduce the lines and points showing the movement of optimization process with an error message saying:

Coordinate {$CellContext`x, $CellContext`y} should be a pair of numbers, or a Scaled or Offset form.

What I got: enter image description here

What I should get: enter image description here

Help is much appreciated.

Edit2:

I have corrected again my code as below ad produced the following chart. The error message is gone:

f = Reap[FindMinimum[{-((10 x)/((x - 1)^2 + (y - 1)^2 + 1)) - 
   2000/((x + 1)^2 + (y + 2)^2 + 1), 
  x^2 + y^2 > 2}, {{x, 1.5}, {y, 0}},
 StepMonitor :> (Sow[{x, y}])];];
ContourPlot[-(100/((x - 1)^2 + (y - 1)^2 + 1)) - 
200/((x + 1)^2 + (y + 2)^2 + 1), {x, -3, 3}, {y, -4, 2},
RegionFunction -> (#1^2 + #2^2 > 2 &), Contours -> 10,
Epilog -> ({Red, Text["Constrained Area", {0, 0}], Line[f[[2]]], 
Yellow, Map[Point, f[[2]]], Blue, PointSize[0.02], 
Point[f[[2, 1, 1]]]}), ContourLabels -> True]

enter image description here

$\endgroup$

closed as off-topic by Yves Klett, RunnyKine, user9660, István Zachar, m_goldberg Mar 26 '16 at 8:59

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Yves Klett, RunnyKine, Community, István Zachar, m_goldberg
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    $\begingroup$ Extremely important: Change StepMonitor -> (Sow[{x, y}]) to StepMonitor :> (Sow[{x, y}]). Then it will work (although it doesn't seem to be finding the minimum). $\endgroup$ – march Mar 26 '16 at 4:51
  • 2
    $\begingroup$ Oh: the reason why it's not finding the right minimum is because the function you are finding the mimimum of is not the one that you're plotting. Make sure they're the same and things will work better. $\endgroup$ – march Mar 26 '16 at 4:57
  • $\begingroup$ That's weird, I copied exactly the same code (accept for the StepMonitor mistake as you mentioned). But somehow obtained different result $\endgroup$ – iFikr Mar 26 '16 at 5:02
  • $\begingroup$ I have reworded the question to be more useful and hopefully beneficial for future visitors $\endgroup$ – iFikr Mar 26 '16 at 11:31
  • $\begingroup$ @iFikr Actually I looked at the youtube video you linked: the author of that video also made the same mistake of not plotting the function he had minimized, probably due to typos in copying the numerical factors. You might want to change that in your edit. $\endgroup$ – MarcoB Mar 26 '16 at 13:03