3
$\begingroup$

Let's assume I have a symbolic expression, and inside a Manipulate I define the actual values:

myf = x + y;
Manipulate[myf, {x, 0, 1}, {y, 0, 1}]

doing this I don't obtain the sum of the numbers, so I did

Manipulate[Evaluate@myf, {x, 0, 1}, {y, 0, 1}]

and is fine, actual numbers are added up. If however I have more expressions:

Manipulate[{{Evaluate@myf}, {1 + Evaluate@myf}}, {x, 0, 1}, {y, 0, 1}]

Evaluate does not substitute the x and y with the actual values.

How can I solve this? More specifically: how to replace symbolic values with actual ones inside a Manipulate in the most general case? Hope to have been clear ...

$\endgroup$
  • $\begingroup$ myf[x_, y_] := x + y; Manipulate[myf[x, y], {x, 0, 1}, {y, 0, 1}]? $\endgroup$ – Karsten 7. Oct 26 '15 at 20:46
  • $\begingroup$ What is the actual problem you want to solve? Your example problem is easily solved by using a function myf[x_, y_] := x + y $\endgroup$ – paw Oct 26 '15 at 20:46
  • 1
    $\begingroup$ I have a pretty complicated Manipulate, with the plot of a symbolic expression function of values set by the manipulate. I knew that the function is the best option but for some reason I can't manage to define the function (gives me an error), so I was trying with the symbolic expression ... $\endgroup$ – dario Oct 26 '15 at 20:51
  • $\begingroup$ Could you present the actual code you are working on? Perhaps this would be a good start to get everyone on board. $\endgroup$ – e.doroskevic Oct 26 '15 at 21:51
3
$\begingroup$

Another way, to show how to use Evaluate, is to apply it to the whole first argument:

Manipulate[Evaluate@{{myf}, {1 + myf}}, {x, 0, 1}, {y, 0, 1}]

With functions like Manipulate that hold their arguments, one applies Evaluate to the whole argument to override the argument being held. When it is buried inside another function, even one like List, the Evaluate has no effect.

$\endgroup$
4
$\begingroup$

Best to define a function:

myf[x_, y_] := x + y;
Manipulate[myf[x, y], {x, 0, 1}, {y, 0, 1}]
$\endgroup$
3
$\begingroup$

Two different ways of injection:

Manipulate[{{#}, {1 + #}}, {x, 0, 1}, {y, 0, 1}] & @ myf

or

With[{myf = myf},
 Manipulate[{{myf}, {1 + myf}}, {x, 0, 1}, {y, 0, 1}]
 ]
$\endgroup$
  • $\begingroup$ works perfectly fine, thank you, but I accepted the answer from Michael since it came first $\endgroup$ – dario Oct 27 '15 at 11:42
  • $\begingroup$ @dario The answer by Michael came last, but I don't mind. $\endgroup$ – Karsten 7. Oct 27 '15 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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