MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Substitution ignored?


Let's look at a simpler case.

f = x^2;
Plot[f /. x -> x - 1, {x, 0, 2}];

The arguments of Plot are held unevaluated until all the substitutions are
made. For each value of x, f and x are substituted into the held expression
and then the expression is evaluated.

The following illustrates the process for a particular value of x. (Copy it
and evaluate in a notebook.)

Print["The first argument held"]
Hold[f /. x -> x - 1]
Print["For each plotting value the routine substitutes for f"]
%% /. HoldPattern[f] -> x^2
Print["And also substitutes the current choice for x, say 0.01"]
%% /. x -> 0.01
Print["Then it releases the Hold to obtain the value"]
%% // ReleaseHold

The rule does not match because 0.01^2  is evaluated first to 0.0001.

If you evaluate the first argument, then it will work because the rule is
applied before any numerical values are substituted.

f = x^2
Plot[{  f,  f /. x -> x - 1  } // Evaluate, {x, 0, 2}];

David Park
djmp at

From: Jos R Bergervoet [mailto:Jos.Lastname at]
To: mathgroup at

Dear MathGroup,

Why is the following going wrong?

f = x^2
Plot[{  f,  f /. x->x-1  }, {x,0,2}]   (* Identical functions plotted !!! *)

g = f /. x->x-1
Plot[{f, g}, {x,0,2}]                  (* OK, different functions plotted *)

-- Jos

  • Prev by Date: RE: (**) comments highlighted - newbie
  • Next by Date: Re: Substitution ignored?
  • Previous by thread: Re: Substitution ignored?
  • Next by thread: Integral of a bivariate function