6

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?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
user61865
  • 61
  • 2

1 Answers1

4

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}]
Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
Rolf Mertig
  • 17,172
  • 1
  • 45
  • 76
  • 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 01:14
  • Also, why % behaves in that "special" way, i.e. see C versus D – user61865 Feb 12 '13 at 01:15
  • @user61865. % is a shortcut for a function call. Evaluate FullForm@Hold@%; you will get Hold[Out[]]. – m_goldberg Feb 12 '13 at 02: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 02:05
  • I see, indeed. And Evaluate works as well. What if I want to store the symbolic results of a function call and then use them to feed into another expression? I've had these problems with D[] for example. Do I have always to define a function, capturing all the variables, i.e. test[x_,k_]=Simplify[x*(x+k)], or can I define a symbol which when evaluated will simply "paste" its expression, without stopping the evaluation right after? – user61865 Feb 12 '13 at 02:12
  • 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
  • @m_goldberg Well, it is documented at M.SE, by maestro belisarius, here – Rolf Mertig Feb 12 '13 at 15:41