6
$\begingroup$

Please have a look at the code below. The second Manipulate uses the name rl of the replacement rule given in the first line, but unlike the first Manipulte, it does not plot anything, at least not on my computer. What goes wrong and how should I correct it? And why is % immune to the problem?

rl = {ρ -> 1 / 5 Sqrt[25 - 25 z^2 + 10 Sin[5 ϕ] + Sin[5 ϕ]^2]};
Manipulate[PolarPlot[ρ /. % // Evaluate, {ϕ, 0, 2 Pi}], {z, -1, 1}]
Manipulate[PolarPlot[ρ /. rl // Evaluate, {ϕ, 0, 2 Pi}], {z, -1, 1}]
$\endgroup$
  • 1
    $\begingroup$ With[{rl = rl // Evaluate}, Manipulate[PolarPlot[\[Rho] /. rl, {\[Phi], 0, 2 Pi}], {z, -1, 1}]] - read the documentation, understand the scoping behavior of Manipulate... $\endgroup$ – ciao Apr 30 '16 at 9:43
  • 1
    $\begingroup$ @ciao that doesn't explain why % works. $\endgroup$ – QuantumDot Apr 30 '16 at 9:47
  • 1
    $\begingroup$ Manipulate is a scoping construct so the second example which is not working is behaving correctly :-) z defined in rl is different than z from Manipulate's spec part, or will be as soon as Manipulate is evaluated. So at the end you should use With as ciao showed or define a function and pass z and ϕ. This of course doesn't explain why % works. Maybe there are some special rules about how FrontEnd reads % and Out[_], would be good to know. $\endgroup$ – Kuba Apr 30 '16 at 10:06
  • $\begingroup$ @QuantumDot, huh? Of course it does. That causes the result of the prior line to replace the % before anything else, exactly as if it had been there directly. $\endgroup$ – ciao Apr 30 '16 at 10:08
  • 2
    $\begingroup$ @ciao but why % is expected to be replaced asap? $\endgroup$ – Kuba Apr 30 '16 at 10:11
6
$\begingroup$

This version works:

rl = {ρ[z_, ϕ_] :> 1/5 Sqrt[25 - 25 z^2 + 10 Sin[5 ϕ] + Sin[5 ϕ]^2]};
Manipulate[PolarPlot[Evaluate[ReplaceAll[ρ[z, ϕ], %]], {ϕ,0, 2 Pi}], {z, -1, 1}]
Manipulate[PolarPlot[Evaluate[ReplaceAll[ρ[z, ϕ], rl]], {ϕ,0, 2 Pi}], {z, -1, 1}]

The main change is in the way the rule is defined as a $RuleDelayed$ instead of $Rule$.

$\endgroup$
  • $\begingroup$ Thank you! I did try RuleDelayed but my attempt was in vain. Your code tells me why my code didn't work: I did not write rho as a function. $\endgroup$ – User18 Apr 30 '16 at 13:56
  • $\begingroup$ It's not a function per se, but a pattern. A function is similar to this but applies the rule whenever the pattern is encoutered. Happy to help $\endgroup$ – Lior Blech Apr 30 '16 at 13:59
  • $\begingroup$ oh, I see. Thanks! $\endgroup$ – User18 Apr 30 '16 at 14:03
4
$\begingroup$

Thanks to ciao's and Kuba's explanation, I have thought of some little code to exemplify the scoping behaviour of Manipulate; I hope it will be helpful to people who are still not very familiar with the concept:

a),

Manipulate[Hold@x, {x,0,1}]

b),

p=x;
Manipulate[2 p-x,{x,0,1}]

c),

rpl=q->x;
Manipulate[(2 q/.rpl)-x,{x,0,1}]
$\endgroup$
4
$\begingroup$

Others have explained why the version with rl does not work. But this does not explain the very weird phenomenon that the version with % does work. Why is % (which is just a notation for Out) special?

It seems that Manipulate singles out Out (i.e. %) for special treatment. Observe:

In[1]:= x
Out[1]= x

In[2]:= Manipulate[Hold[%1], {x, 0, 1}]
Out[2]= Manipulate[Hold[x], {x, 0, 1}]

Manipulate has taken the Out[1] (same as %1), and evaluated it, even though it was inside of Hold. This is not normal evaluation. It is Manipulate looking specifically for % and replacing it with its value.


This is no doubt done to make Mathematica less confusing and more accessible to beginners. But personally I don't like this special behaviour at all. For people who already understand how Mathematica works this (undocumented!) behaviour is very surprising and completely unexpected.

$\endgroup$
  • 3
    $\begingroup$ Yes I find this very confusing. I think I fall somewhere between "beginner" and "experienced", and I find weird nuances in Mathematica like these only serve to confuse people like me. I really wish they implemented a global switch that turned off all "noob" features to make Mathematica behave more regularly. $\endgroup$ – QuantumDot May 1 '16 at 10:04
  • $\begingroup$ Also, I think that Wolfram Research ought to clearly document the evaluation sequences inside Manipulate, Animate and other functions that are as complex and as, so to speak, mysterious. $\endgroup$ – User18 May 2 '16 at 5:52

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.