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.

I'm confused with Mathematica's way of parsing expressions. I've been struggling with this for a while and never found an exhaustive answer, sometimes things don't parse the way I think they would and I don't really understand why.

As an example, with Mathematica 8:

(* A works *)
Manipulate[
  Plot[(y/a) /. {y -> (x - b)}, {x, 0, 2}, PlotRange -> {0, 1}],
  {a, 1, 2},
  {b, 0, 1}]

(* B doesn't work *)
Manipulate[
  Plot[(x/a) /. {x -> (x - b)}, {x, 0, 2}, PlotRange -> {0, 1}],
  {a, 1, 2},
  {b, 0, 1}]

(* C works *)
(x/a) /. {x -> (x - b)}
Manipulate[
  Plot[%, {x, 0, 2}, PlotRange -> {0, 1}],
  {a, 1, 2},
  {b, 0, 1}]

(* D doesn't work *)
test = (x/a) /. {x -> (x - b)}
Manipulate[
 Plot[test, {x, 0, 2}, PlotRange -> {0, 1}]
, {a, 1, 2}, {b, 0, 1}]

(* E works *)
test2[x_, a_, b_] = (x/a) /. {x -> (x - b)}
Manipulate[
  Plot[test2[x, a, b], {x, 0, 2}, PlotRange -> {0, 1}],
  {a, 1, 2},
  {b, 0, 1}]

Case A works, so the substitution is performed fine inside Plot and Manipulate.

But B doesn't, which I could understand as an issue trying to substitute a variable with an expression containing itself, but then, if you evaluate it beforehand, as in C, everything works again, so it has to be a problem with x being part of Plot, I guess.

Then if you assign the result of the substitution to a variable, you can't directly plot it, so it seems that variables are not evaluated if they are not functions (as in E, with pattern matching) inside Plot. But % is, so % is "special" as it gets evaluated inside plot while a standard symbol assigned to a value does not.

Can someone explain me all this? I guess it's related to the Hold attributes a function can have?

share|improve this question
    
You might also want to read this: mathematica.stackexchange.com/q/1731/5 –  rm -rf Feb 12 '13 at 2:09
add comment

1 Answer

Plot has Attributes HoldAll, so one possibility to get what you expect is to do just

SetOptions[Plot, Evaluated -> True];

at the beginning.

Another possibility (better documented) would be to use Evaluate inside Plot:

Plot[Evaluate[...], {x, 0, 2}]
share|improve this answer
    
I see, but why A works? I guess it's because when Plot comes to Evaluate the expression it first replaces x with a numeric value, and that replacement goes "inside" my /.{x->x-b} which then starts making very little sense... Right? –  user61865 Feb 12 '13 at 1:14
    
Also, why % behaves in that "special" way, i.e. see C versus D –  user61865 Feb 12 '13 at 1:15
    
@user61865. % is a shortcut for a function call. Evaluate FullForm@Hold@%; you will get Hold[Out[]]. –  m_goldberg Feb 12 '13 at 2:00
    
"Evaluate inside Plot" better documented?! Now that's an understatement if I ever read one. The use of Evaluated ->True is essentially undocumented. –  m_goldberg Feb 12 '13 at 2:05
1  
@user61865. Your last comment is really another question. Don't ask a new question in a comment. Post it in a new question. –  m_goldberg Feb 12 '13 at 15:29
show 2 more comments

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.