Possible Duplicate:
General::ivar is not a valid variable when plotting - what actually causes this and how to avoid it?
Beginner question:
Why can't I use D[]
like that?
Plot[{Sin[x], D[Sin[x], x]}, {x, -2 Pi, 2 Pi}]
If I assign the result of D[]
to a variable and put that variable in the list it works.
Answer
Plot
has attribute HoldAll
which means in this case that D[Sin[x], x]
isn't evaluated until after x
is replaced with some number, so you end up with something like D[Sin[-6.28], -6.28]
etc. which causes the errors since you can't take a derivative with respect to a number.
One way to get around this is to use Evaluate
to evaluate the derivative before the numbers are plugged in, i.e. to do something like
Plot[Evaluate[{Sin[x], D[Sin[x], x]}], {x, -2 Pi, 2 Pi}]
Comments
Post a Comment