Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
2  
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 –  xzczd May 12 '13 at 12:37
3  
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. –  Michael E2 May 12 '13 at 12:48
4  
@MichaelE2, it looks like Manipulate has special handling of Out via Manipulate`Dump`resolveOut[] –  Simon Woods May 12 '13 at 13:30
    
@SimonWoods It clearly does: see ?Manipulate`Dump`resolveOut Good find! Consider putting it in an answer. –  Michael E2 May 12 '13 at 13:44
    
probable duplicate with no upvoted answer : mathematica.stackexchange.com/questions/23734/… –  andre May 12 '13 at 20:30
add comment

2 Answers

up vote 9 down vote accepted

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.

share|improve this answer
    
Thanks for the responses guys, this is a great insight into the workings of Manipulate. –  Jaleno May 13 '13 at 22:19
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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