Re: Substitution ignored?
- To: mathgroup at smc.vnet.net
- Subject: [mg48746] Re: [mg48738] Substitution ignored?
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Sat, 12 Jun 2004 23:33:36 -0400 (EDT)
- References: <200406120359.XAA05134@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 12 Jun 2004, at 12:59, Jos R Bergervoet wrote:
> 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
>
Actually both of these are examples show that you have not bothered to
read the documentation for Plot (or the archives of this list since
this is one of the most commonly asked questions). The correct code is
Plot[Evaluate[{f, f /. x -> x - 1}], {x, 0, 2}]
(and in fact Plot[Evaluate[{f, g}], {x,0,2}] is also better than what
you have).
Now the explanation. If you do not Evaluate the first argument in Plot,
the graph is made by substituting individual numerical values for x,
and then evaluating the function at this point. So your second
function, f/.x->x-1 is evaluated as follows (with x = 1.5 chosen to
illustrate what is going on):
(1.5)^2/. 1.5->(1.5-1)
which is just
2.25/. 1.5->0.5
which is of course just 2.25, in other words your substitution rule has
no effect at all..
In the other case of course this does not happen since the second
function is already (x-1)^2 before any values are substituted.
Andrzej Kozlowski
Chiba, Japan
http://www.mimuw.edu.pl/~akoz/
- References:
- Substitution ignored?
- From: Jos R Bergervoet <Jos.Lastname@Companyname.com>
- Substitution ignored?