16
$\begingroup$

Would anyone have an idea why the following doesn't work:

rule = {z -> x^2 + 2 x + y};
Manipulate[
 Plot[z /. rule, {x, 0, 10}],
 {{y, 2, "y"}, 1, 5}
 ]

But the following do:

rule = {z -> x^2 + 2 x + y};
z /. rule;
Manipulate[
 Plot[%, {x, 0, 10}],
 {{y, 2, "y"}, 1, 5}
 ]

and:

Manipulate[
 Plot[z /. z -> x^2 + 2 x + y, {x, 0, 10}],
 {{y, 2, "y"}, 1, 5}
 ]

I was looking to Plot and Manipulate the results from a Solve of multiple equations, but it seems like I haven't understood the substitution rules correctly.

Thanks,

Jaleno

$\endgroup$
5
  • 2
    $\begingroup$ This is explained in the Possible Issues of the document of Manipulate: Manipulate only "notices" explicit visible parameters. You can check the document for more information: reference.wolfram.com/mathematica/ref/Manipulate.en.html $\endgroup$
    – xzczd
    May 12, 2013 at 12:37
  • 4
    $\begingroup$ The new thing for me in this question is why % is evaluated. E.g., this works with % in place of rule: rule = {z -> x^2 + 2 x + y}; Manipulate[Plot[z /. %, {x, 0, 10}], {{y, 2, "y"}, 1, 5}]. I thought Out[1] would be treated like the global variable but it's not. $\endgroup$
    – Michael E2
    May 12, 2013 at 12:48
  • 5
    $\begingroup$ @MichaelE2, it looks like Manipulate has special handling of Out via Manipulate`Dump`resolveOut[] $\endgroup$ May 12, 2013 at 13:30
  • $\begingroup$ @SimonWoods It clearly does: see ?Manipulate`Dump`resolveOut Good find! Consider putting it in an answer. $\endgroup$
    – Michael E2
    May 12, 2013 at 13:44
  • $\begingroup$ probable duplicate with no upvoted answer : mathematica.stackexchange.com/questions/23734/… $\endgroup$
    – andre314
    May 12, 2013 at 20:30

2 Answers 2

21
$\begingroup$

The general issue, as mentioned by xzczd, is that Manipulate only "notices" explicit visible parameters. This is because when you evaluate something like Manipulate[x, {x, 0, 1}] and start waggling the slider, you are not changing the value of the global symbol x, but instead a temporary symbol called something like x$$15. You can see this like so:

Manipulate[{SymbolName @ Unevaluated @ x, x}, {x, 0, 1}]

enter image description here

So in order to make the magic happen, Manipulate has to find occurrences of x in the manipulated expression and replace them with the temporary symbol. It does this before evaluating the expression.

So if you try this...

a = 10 + x;
Manipulate[a, {x, 0, 1}]

...Manipulate looks at the unevaluated expression a and finds no occurrence of x. So you get this output:

enter image description here

Here a is evaluating to 10 + x (that's the global symbol x) but the slider is controlling some temporary symbol like x$$15.

To get the desired behaviour you can use With to replace the a inside the Manipulate with its evaluated form 10 + x. That way Manipulate will "notice" the x and replace it with the temporary symbol tied to the slider:

With[{a = a}, Manipulate[a, {x, 0, 1}]]

enter image description here

So for the problem in the question, the plot can be obtained like this:

rule = {z -> x^2 + 2 x + y};
With[{rule = rule},
 Manipulate[Plot[z /. rule, {x, 0, 10}], {{y, 2, "y"}, 1, 5}]]

enter image description here

Special behaviour with Out

The apparent mystery is why this works:

a = 10 + x;
Manipulate[%, {x, 0, 1}]

enter image description here

The answer is simply that Manipulate has special handling for Out. Any occurrence of Out in the manipulated expression is evaluated before Manipulate does its localization, similarly to what we did above with With. So Manipulate "notices" the x and works as desired.

For those who like to know these things: the handling of Out is implemented by Manipulate`Dump`resolveOut using the Trott-Strzebonski trick.

$\endgroup$
1
  • $\begingroup$ Thanks for the responses guys, this is a great insight into the workings of Manipulate. $\endgroup$
    – Jaleno
    May 13, 2013 at 22:19
2
$\begingroup$

Here's another quick way to make your initial example work:

rule = {z -> x^2 + 2 x + y};
Manipulate[Plot[#, {x, 0, 10}], {{y, 2, "y"}, 1, 5}] &@z /. rule
$\endgroup$

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.