6
$\begingroup$

It's my first time here but I didn't find answer to my problem and I can't menage that by myself,Mathematica documentation or even by previous stackexchange problems.

In Manipulate I have two variables and I want to control them separately with Locator: x coordinate of the Locator should be my, lets say, a variable, and y coordinate - b variable.

To be more precise: I want to control the position of a red point on a surface plot with the position of the Locator (probably with LocatorPane) on the contour plot. Is it possible?

potentialsurface = 1/r^12-2/r^6+1/d^12-2/d^6-(2 Exp[-(r-d)]+2 Exp[-(d-r)]-2 Exp[-(2 (r-d)-2 (d-r))*Cos[d]])    
Row[{ContourPlot[potentialsurface, {r, .8, 1.6}, {d, .8, 1.6}], 
  Manipulate[
   Show[Graphics3D[{PointSize[0.04], Red, 
      Point[{a, b, 
        1/a^12 - 2/a^6 + 1/b^12 - 
         2/b^6 - (2 Exp[-(a - b)] + 2 Exp[-(b - a)] - 
           2 Exp[-(2 (a - b) - 2 (b - a))*Cos[b]])}]}, 
     BoxRatios -> {1, 1, 1}, Boxed -> True, Axes -> All, 
     ViewPoint -> {1.5, 2.4, 3}],
    Plot3D[potentialsurface, {r, 0.8, 1.6}, {d, 0.8, 1.6}]], 
  {a,0.86, 1.6, 0.01}, {b, 0.86, 1.6, 0.01}]}]

PES

It would be great to remove sliders and replace them with the Locator on the contour plot.

$\endgroup$

1 Answer 1

9
$\begingroup$

is this what you mean?

enter image description here

ClearAll[r, a, b, d];
potentialsurface = 
  1/r^12 - 2/r^6 + 1/d^12 - 
   2/d^6 - (2 Exp[-(r - d)] + 2 Exp[-(d - r)] - 
     2 Exp[-(2 (r - d) - 2 (d - r))*Cos[d]]);
Manipulate[
 Module[{z},
  z = N[1/a^12 - 2/a^6 + 1/b^12 - 
     2/b^6 - (2 Exp[-(a - b)] + 2 Exp[-(b - a)] - 
       2 Exp[-(2 (a - b) - 2 (b - a))*Cos[b]])];
  Grid[{{Row[{"(", a, ",", b, ",", z, ")"}]},
    {Row[{ContourPlot[potentialsurface, {r, .8, 1.6}, {d, .8, 1.6}, 
        ImageSize -> 400],
       Show[
        Graphics3D[
         {PointSize[0.04], Red,
          Point[{a, b, z}]
          },
         BoxRatios -> {1, 1, 1},
         Boxed -> True,
         Axes -> All,
         ViewPoint -> {1.5, 2.4, 3}
         ],
        Plot3D[potentialsurface, {r, 0.8, 1.6}, {d, 0.8, 1.6}, 
         PerformanceGoal -> "Quality"],
        ImageSize -> 400
        ]
       }
      ]
     }}]
  ],
 { {a, 1.17}, Locator},
 {{b, 1.16}, Locator},
 TrackedSymbols :> {a, b}
 ]

Update:

To answer comment.

I'm unable to rotate the potentialsurface plot because of that

This is known issue. Locator is known to prevent rotation of graphics.

If you put a LocatorPane around the ContourPlot only, then this keeps the locator active only on the ContourPlot and will not get in the way of other graphics. Now you are able to rotate the 3D graphics next to it.

But to move the locator around, need to Click the mouse on the new location, and not slide the mouse like before.

So to move the locator, simply point the mouse to the new location you want to move to instead of sliding it. Here is demo

enter image description here

Here is the updated code

Manipulate[
 Module[{z, r, d, potentialsurface, a, b},
  a = pt[[1]];
  b = pt[[2]];
  potentialsurface = 
   1/r^12 - 2/r^6 + 1/d^12 - 
    2/d^6 - (2 Exp[-(r - d)] + 2 Exp[-(d - r)] - 
      2 Exp[-(2 (r - d) - 2 (d - r))*Cos[d]]);

  z = N[1/a^12 - 2/a^6 + 1/b^12 - 
     2/b^6 - (2 Exp[-(a - b)] + 2 Exp[-(b - a)] - 
       2 Exp[-(2 (a - b) - 2 (b - a))*Cos[b]])];

  Grid[{{Row[{"(", a, ",", b, ",", z, ")"}]},
    {Row[{
       LocatorPane[Dynamic[pt],
        Graphics[
         ContourPlot[potentialsurface, {r, .8, 1.6}, {d, .8, 1.6}, 
          ImageSize -> 400]]
        ]
       ,
       Show[
        Graphics3D[{PointSize[0.04], Red, 
          Point[{pt[[1]], pt[[2]], z}]},
         BoxRatios -> {1, 1, 1},
         Boxed -> True, Axes -> All, ViewPoint -> {1.5, 2.4, 3}

         ],
        Plot3D[potentialsurface, {r, 0.8, 1.6}, {d, 0.8, 1.6}, 
         PerformanceGoal -> "Quality"
         ],
        ImageSize -> 400
        ]
       }
      ]
     }}
   ]
  ],
 {{pt, {1.17, 1.2}}, None},
 TrackedSymbols :> {pt}
 ]
$\endgroup$
3
  • $\begingroup$ Yes! That's exactly what's needed. Thank You very much for Your help. Now I'm go to understang what's going on in Your code :) Thanks once again! $\endgroup$
    – Lechuu
    Commented May 8, 2020 at 9:00
  • $\begingroup$ Oops, there is one problem: Locator works everywhere on the image. How to make him works only on a contourplot? I'm unable to rotate the potentialsurface plot because of that. Could You help me? $\endgroup$
    – Lechuu
    Commented May 8, 2020 at 9:46
  • 1
    $\begingroup$ @LeszekNowakowski I've updated it $\endgroup$
    – Nasser
    Commented May 8, 2020 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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