1
$\begingroup$

I am attempting to create a tool to visualize gradient, eigenvector, and eigenvalue information for a 2D surface. I want to show a contour plot of an inputted function and overlay the gradient and eigenvectors based at a point selected by a Locator. The problem is when I move the Locator point the contour plot redraws -- which is slow. So I'd like for the gradient and eigenvector information to change but the contour plot to stay (unless the function is changed).

Manipulate[
  Module[{},
    p1 = ContourPlot[expr /. {x -> x, y -> y}, {x, -5, 5}, {y, -5, 5}, Contours -> 20];

    grad = D[expr, {{x, y}}];
    ngrad = grad /. {x -> Dynamic[pt[[1]]], y -> Dynamic[pt[[2]]]} ;
    p2 = Graphics[{Red, Arrow[{pt, pt + ngrad}]}];

    Show[p1, p2]
  ],
  {{pt, {1, 1}}, {-5, -5}, {5, 5}, Locator},
  {{expr, 2*x*Cos[y] - x*y + 10, "f(x,y)"}, InputField},
  {locx, pt[[1]], InputField},
  {locy, pt[[2]], InputField},
  {locval, expr /. {x -> pt[[1]], y -> pt[[2]]}, InputField}]

I expect this can be done using Dynamic, but everything I've tried so far has not worked. Any help would be greatly appreciated.

$\endgroup$
1
  • $\begingroup$ Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. $\endgroup$
    – bbgodfrey
    Commented Jan 24, 2015 at 21:41

1 Answer 1

2
$\begingroup$

Everywhere wrapping pt in Dynamic allows it to be updated without triggering changes in the CountourPlot, which is independent of pt:

Manipulate[Module[{},
  p1 = ContourPlot[expr /. {x -> x, y -> y}, {x, -5, 5}, {y, -5, 5}, 
  Contours -> 20]; grad = D[expr, {{x, y}}]; 
  ngrad = grad /. {x -> Dynamic[pt[[1]]], y -> Dynamic[pt[[2]]]};
  p2 = Graphics[{Red, Arrow[{Dynamic[pt], Dynamic[pt + ngrad]}]}]; 
  Show[p1, p2]],
  {{pt, {1, 1}}, {-5, -5}, {5, 5}, Locator}, {{expr, 2*x*Cos[y] - x*y + 10, "f(x,y)"}, 
  InputField}, {locx, pt[[1]], InputField}, {locy, pt[[2]], 
  InputField}, {locval, expr /. {x -> pt[[1]], y -> pt[[2]]}, InputField}]

See "Using Dynamic Inside Manipulate", the second section of Advanced Manipulate Functionality for details.

$\endgroup$
2
  • $\begingroup$ This works. Thank you very much for the help! I'll spend more time with the documentation. $\endgroup$
    – chris
    Commented Jan 25, 2015 at 22:39
  • $\begingroup$ Glad this is what you needed. I do recommend you take the 10 minutes needed to read the introductory Tour, under Help. $\endgroup$
    – bbgodfrey
    Commented Jan 26, 2015 at 2:38

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.