1

As the title says, what's the cleanest way to plot a function's derivative in Mathematica? The two best methods I could find were either to define the function beforehand and then use prime notation, e.g.

F[x_]=x^2;
Plot[{F[x], F'[x]}, {x, -10, 10}]

Or to write the function in terms of a different variable and plot it like so:

Plot[{x^2, D[t^2, t] /. t -> x}, {x, -10, 10}]

Both of these methods appear clunky when used in more complex expressions, so I was wondering if there was a simple way to write it, akin to

Plot[{x^2, IgnoreXFromOutsideScope[D[x^2, x]]}, {x, -10, 10}]
Aggs123
  • 11
  • 2
  • Hi, if you check "plot derivative" in the search box you will see lots of solutions. – userrandrand Nov 03 '22 at 02:25
  • 3
    Plot[{x^2, Evaluate@D[x^2, x]}, {x, -10, 10}] ? – Syed Nov 03 '22 at 02:25
  • what is wrong with the first method you show? Plot[{F[x], F'[x]}, {x, -10, 10}] why do you call it clunky? when used in more complex expressions could you show an example of such more complex expression where the above causes problems? Btw, in Mathematica, there are at least 10 different ways to do the same thing. So pick one way that feels easy for you to understand and use. – Nasser Nov 03 '22 at 02:29
  • 1
    In the comments below this answer for example you will see lots of methods. – userrandrand Nov 03 '22 at 02:31
  • thanks, @Syed, it worked. does the Evaluate function reset the scope? i don't understand the explanation on the docs. – Aggs123 Nov 03 '22 at 02:34
  • @Nasser it's more that i don't necessarily want to define a new function every time I want to plot its derivative. – Aggs123 Nov 03 '22 at 02:36
  • 2
    My favorite from the link above applied to this case would be Plot @@ {{x^2, D[x^2, x]}, {x, -10, 10}} – userrandrand Nov 03 '22 at 02:37
  • thanks, @userrandrand, for the helpful link, and thanks to all for the advice. – Aggs123 Nov 03 '22 at 02:42
  • 1
    that i don't necessarily want to define a new function every time Ok, but it seems to me it is better to have one definition in one place instead of duplicating the code (body of the function) twice and possible making a mistake, also later if you want to change what F is, you have to make the change in one place, not in 2 places. But it is up to you. – Nasser Nov 03 '22 at 02:43
  • in your case you could also use Plot @@ {{({#, D[#, x] } &)[x^2]}, {x, -10, 10}} – userrandrand Nov 03 '22 at 02:43
  • I agree that it is clunky to define a function just for a quick look at a plot you might not need later. If you have in mind to do multiple things later with the function then yes it's better to define a function. – userrandrand Nov 03 '22 at 02:52
  • 2
    Concerning the question you might be wondering about "why can't I just type Plot[D[x^2,x],{x,0,1}] " see this answer essentially it is because Plot does not evaluate D[x^2,x] right away. Instead it will keep the expression held, replace x with a number in the interval you are plotting like 0.3 and then try to evaluate D[0.3^2,0.3]. All solutions given in the above link are ways to circumvent that issue. – userrandrand Nov 03 '22 at 03:08

0 Answers0