# Why does the replace all (/.) evaluate differently in plot

I'm plotting this function:

Plot[(2 (l/2 π)^2 ((2 π f)/c)^2 /. {f -> (f 10^9),
l -> .01, c -> 3 10^8}), {f, 17.5, 26.5}]


but it does not plot correctly unless I do this:

Plot[Evaluate[(2 (l/2 π)^2 ((2 π f)/c)^2 /. {f -> (f 10^9),
l -> .01, c -> 3 10^8})], {f, 17.5, 26.5}]


Why is the replace all command being evaluated differently in the two cases?

This is strongly related to Plot draws list of curves in same color when not using Evaluate but the specific issue there has to do with styling of lines rather than evaluation itself.

In this case the question is why the curve scale ends up different. For that you need to understand how Plot works. It is akin to Block over the plot variables. Now consider:

Block[{f = 3},
(2 (l/2 π)^2 ((2 π f)/c)^2 /. {f -> (f 10^9), l -> .01, c -> 3 10^8})
]

Block[{f = 3},
Evaluate[(2 (l/2 π)^2 ((2 π f)/c)^2 /. {f -> (f 10^9), l -> .01, c -> 3 10^8})]
]

1.94818*10^-18

1.94818


Clearly the replacement rule is not having the same effect in both cases. Critically ReplaceAll does not hold its arguments therefore standard evaluation order is followed which means that we get something like this:

Block[{f = 3},
foo[2 (l/2 π)^2 ((2 π f)/c)^2, {f -> (f 10^9), l -> .01, c -> 3 10^8}]
]

foo[(18 l^2 π^4)/c^2, {3 -> 3000000000, l -> 0.01, c -> 300000000}]


(foo stands in as an arbitrary head with standard evaluation.) Note that the replacement rule is now 3 -> 3000000000 and that 3 does not appear in the left-hand side. We would need ReplaceAll to also hold its arguments to prevent this; it can be simulated using Unevaluated:

Block[{f = 3},
Unevaluated[2 (l/2 π)^2 ((2 π f)/c)^2] /.
Unevaluated[{f -> (f 10^9), l -> .01, c -> 3 10^8}]
]

1.94818


Evaluate works in your example because it causes a symbolic evaluation outside the scope of Plot. However for the same reason if f has a global value it will again fail. It is more robust to use Unevaluated as shown above but this is rather inconvenient. There is another robust though undocumented method: the Plot option Evaluated -> True. This causes an evaluation of the plot function before a value is substituted but after the variable is scoped by Plot:

f = 1.23;

Plot[(2 (l/2 π)^2 ((2 π f)/c)^2 /. {f -> (f 10^9), l -> .01, c -> 3 10^8}),
{f, 17.5, 26.5}, Evaluated -> True]


This option won't scope the other two replacement symbols l and c of course but it is still my preferred approach for such problems. Also look at HoldPattern which can be of some utility.