0
$\begingroup$

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?

$\endgroup$
0

1 Answer 1

4
$\begingroup$

Try Derivative

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

or Evaluate

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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