I'm trying to make a Manipulate command to plot the contour lines of a function of two variables $f(x,y)$. I think I should use the "InputField" option somehow, and I thought of something like this:

              ContourPlot[f[x, y] == 0, {x, -5, 5}, {y, -5, 5}], 
              {f, #1-#2 &, InputField[_]}]

However, this gives me a slider for $f$, which I don't want, and also changing the function doesn't seem to work.

My question is, how do you work with the Manipulate command when one of the arguments is a function, and more specifically, how do you fix my line of code.



I also don't like feeding the function with the # notation, I'd prefer to feed in an expression involving x and y.


1 Answer 1


Both styles are possible

  Manipulate[ ContourPlot[ a[x, y] == 0, {x, -5, 5}, {y, -5, 5}], {{a, #1 - #2 &}}],
  Manipulate[ ContourPlot[       a == 0, {x, -5, 5}, {y, -5, 5}], {{a, x - y}}]}

Mathematica graphics


Answering your comment below, you may use the function in many ways. Here I numerically solve a differential equation involving it:

 nd = NDSolve[{(a /. y -> y[x]) == y'[x], y[0] == 1}, y, {x, -5, 5}];
              Plot[y[x] /. nd, {x, -5, 5}],
              ContourPlot[a == 0, {x, -5, 5}, {y, -5, 5}]}],
 {{a, -Cos[y] + Sin[x]^2}}]

Mathematica graphics

  • $\begingroup$ Thanks for your answer. Can this be modified so that the input is still in the elegant terms of x and y, but I can still use it as a function? (i.e. use something like a[x,y]==0). $\endgroup$
    – user1337
    Jul 2, 2015 at 14:10
  • 1
    $\begingroup$ @user1337 try Manipulate[ ContourPlot[Evaluate@a, {x, -5, 5}, {y, -5, 5}], {{a, x - y == 0}}] $\endgroup$ Jul 2, 2015 at 14:14
  • $\begingroup$ Sorry, but to be more specific, what if I want to feed in the function a in terms of x and y, but use it both to plot the contour a[x,y]==0, and to solve the ODE y'[x]==a[x,y[x]]. I mean, working with both a[x,y] and a[x,y[x]] in the same Manipulate, while the UI still in nice symbolic form. $\endgroup$
    – user1337
    Jul 2, 2015 at 14:17
  • $\begingroup$ Actually, the follow up question is far too big to be discussed in the comments. I'll post it separately. Thanks again. $\endgroup$
    – user1337
    Jul 2, 2015 at 14:26
  • $\begingroup$ @user1337 Yep. I think so too. Thanks for the accept $\endgroup$ Jul 2, 2015 at 14:36

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.