3
$\begingroup$

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:

   Manipulate[
              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.

Thanks!

P.S.

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

$\endgroup$
0

1 Answer 1

2
$\begingroup$

Both styles are possible

Row[{
  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


Edit

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

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

Mathematica graphics

$\endgroup$
8
  • $\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 that you have read and understand our privacy policy and code of conduct.

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