Re: How to plot derivative directly?
- To: mathgroup at smc.vnet.net
 - Subject: [mg118098] Re: How to plot derivative directly?
 - From: Stefan <wutchamacallit27 at gmail.com>
 - Date: Tue, 12 Apr 2011 05:57:56 -0400 (EDT)
 - References: <inuna0$2au$1@smc.vnet.net>
 
On Apr 11, 7:05 am, =A9er=FDch Jakub <Ser... at panska.cz> wrote:
> Dear mathgroup,
>
> it seems to me, that response to my question shall be very simple,
> but I cannot find it. :-(
>
> I want to plot the derivative of the function. I would like to do it
> directly, something like:
>
> Plot[D[x^3 - 6 (x + 1)^2 + x - 7, x],{x,-3,8}]
>
> It returns: General::ivar: "-2.99978 is not a valid variable."
>
> I can understand that it is because local variable x from Plot command
> interferes with the x variable from the D[].
>
> Yes I can bypass the problem by:
> deriv = D[x^3 - 6 (x + 1)^2 + x - 7, x]
> Plot[deriv, {x, -3, 8}]
>
> which is fully functional, but as far as I know Mathematica, there must
> be some simple solution how to do it directly inside the Plot[].
>
> Thanks in advance for kick-off
>
> Jakub
Jakub, the Plot[] function picks a set of discrete x values (within
the range specified, very closely spaced to create the smooth graph)
and tries to evaluate your function with those numbers in place of the
x's. This creates a problem when it then tries to evaluate
D[-3^3-6(-3+1)^2+(-3)-7, -3], since you cant differentiate with
respect to a number like -3, hence the 'General::ivar: "-2.99978 is
not a valid variable." ' error message. You can get it to evaluate the
derivative before substituting in numbers for x by simply enclosing
your derivative inside an Evaluate[]
Plot[Evaluate[D[x^3 - 6 (x + 1)^2 + x - 7, x]], {x, -3, 8}]
Hope that helps!
-Stefan