When trying to plot a derivative of a function, (I take Sin as a simple example), the naive approach

Plot[D[Sin[x], x], {x, 0, 2 π}]

fails. One can work around this by a replacement rule

Plot[D[Sin[y], y] /. y -> x, {x, 0, 2 π}]

which then indeed plots the derivative, i.e. the `Cos.

This is an example of the general syntax "problem", that if I have a function f, say of one variable, and I want to obtain f' as well as a function, then I have to perform this cumbersome definition of f' with that replacement rule:

fprim[x_]:=D[f[y], y]/.y → x

I know that this works, and in this sense that is not an actual problem. However I always thought this a bit cumbersome that one has to introduce an additional dummy variable, and from a mathematical perspective I would rather have the derivative to be a function by default, to which arguments can be applied.

So the question is: Is there a better syntax to obtain f' as a function than the one I used in the examples above?


Try Derivative

Plot[ Derivative[1][Sin ][x], {x, 0, 2 \[Pi]}]

or Evaluate

Plot[Evaluate[D[Sin[x], x]], {x, 0, 2 \[Pi]}]
| improve this answer | |
  • $\begingroup$ I like this much better, thank you so much! $\endgroup$ – Britzel Jul 28 at 10:31
  • $\begingroup$ You're welcome! $\endgroup$ – Ulrich Neumann Jul 28 at 12:22
  • $\begingroup$ Or Plot[Sin'[x], {x, 0, 2 Pi}] $\endgroup$ – Bob Hanlon Jul 29 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.