I was just evaluating a couple of expressions and started to get errors like this:
General::ivar: -1.49994 is not a valid variable. >>
General::ivar: -1.43871 is not a valid variable. >>
General::ivar: -1.37749 is not a valid variable. >>
General::stop: Further output of General::ivar will be
suppressed during this calculation. >>
I'm doing nothing complicated - currently, I simply did this:
f[x_]:=x^2 + 2x + 1
Plot[f[x], {x, -4, 4}]
Solve[f[x] == 4]
g[x_]:=D[f[x], x]
Plot[g[x], {x, -2, 2}]
// ^ errors caused by this
Actually, this isn't the exact quadratic I am investigating, but it is a quadratic and I expected this to work. I googled, as you'd expect, and found this Stack Overflow question which suggested:
Plot[Evaluate[g[x]], {x, -2, 2}]
As a workaround.
It works - my question is, why doesn't that set of instructions generate that error (I can see it is something to do with replacing, but why is one plot different from the other?) and how can I avoid it? Is there something I should specifically have done in forming g
?
g[x_] = D[f[x], x]
(note that I changedSetDelayed
toSet
) before plotting... $\endgroup$