# Simple problem with Manipulate and Plot

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

• 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 Commented May 12, 2013 at 12:37
• 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. Commented May 12, 2013 at 12:48
• @MichaelE2, it looks like Manipulate has special handling of Out via ManipulateDumpresolveOut[] Commented May 12, 2013 at 13:30
• @SimonWoods It clearly does: see ?ManipulateDumpresolveOut Good find! Consider putting it in an answer. Commented May 12, 2013 at 13:44
• probable duplicate with no upvoted answer : mathematica.stackexchange.com/questions/23734/… Commented May 12, 2013 at 20:30

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}] 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: 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}]]

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}]]

Special behaviour with Out

The apparent mystery is why this works:

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

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 ManipulateDumpresolveOut using the Trott-Strzebonski trick.

• Thanks for the responses guys, this is a great insight into the workings of Manipulate. Commented May 13, 2013 at 22:19

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