Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to plot derivative directly?

4,837 views
Skip to first unread message

Šerých Jakub

unread,
Apr 11, 2011, 7:05:36 AM4/11/11
to

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

Murray Eisenberg

unread,
Apr 12, 2011, 5:53:15 AM4/12/11
to
The trouble, as you probably realize, is that when Plot feeds in a value
of x to its first argument, you are now trying to take the derivative of
a constant with respect to a constant.

Another way to do it is:

f[x_]:= x^3 - 6 (x + 1)^2 + x - 7
Plot[f'[x],{x,-3,8}]

Or, perhaps a bit better f the function is much more complicated:

Plot[Evaluate[f'[x],{x,-3,8}]

--
Murray Eisenberg mur...@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305

David Skulsky

unread,
Apr 12, 2011, 5:53:26 AM4/12/11
to
Plot[Evaluate[D[x^3 - 6 (x + 1)^2 + x - 7, x]], {x, -3, 8}]

David

Peter Pein

unread,
Apr 12, 2011, 5:53:36 AM4/12/11
to

Hi Jakub,

have a look at
http://reference.wolfram.com/mathematica/ref/HoldFirst.html (an
attribute being set for Plot) and use

Plot[D[f[x],x]//Evaluate,{x,x0,x1}]

Peter

Helen Read

unread,
Apr 12, 2011, 5:54:41 AM4/12/11
to
On 4/11/2011 7:05 AM, =8Aer=FDch Jakub wrote:

Well, you can do this:

Plot[Evaluate[D[x^3 - 6 (x + 1) 2 + x - 7, x]], {x, -3, 8}]


But I think it's worth defining your function as a function.

g[x_] := x^3 - 6 (x + 1) 2 + x - 7

Plot[g'[x], {x, -3, 8}]

--
Helen Read
University of Vermont


DrMajorBob

unread,
Apr 12, 2011, 5:55:24 AM4/12/11
to
Plot[D[x^3 - 6 (x + 1)^2 + x - 7, x] // Evaluate, {x, -3, 8}]

or

Clear[f]
f[x_] = x^3 - 6 (x + 1)^2 + x - 7;
Plot[f'[x], {x, -3, 8}]

Bobby

On Mon, 11 Apr 2011 06:05:40 -0500, =A6er=FDch Jakub <Ser...@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
>


--
DrMaj...@yahoo.com

Bill Rowe

unread,
Apr 12, 2011, 5:56:08 AM4/12/11
to
On 4/11/11 at 7:05 AM, Ser...@panska.cz wrote:

>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[].

More specifically, Plot substitutes a numerical value for x
every where it appears in the expression then evaluates the
expression. The way to plot the derivative is to force
evaluation of the derivative before Plot substitutes a numerical
value for x. That is you want to do:

Plot[Evaluate[D[x^3 - 6 (x + 1)^2 + x - 7, x]], {x, -3, 8}]

And even if you could somehow get

Plot[D[x^3 - 6 (x + 1)^2 + x - 7, x],{x,-3,8}]

to work, you really wouldn't want to do that since it would mean
re-evaluating the derivative for each numerical value of x. That
would require a lot of unneeded computation.


Stefan

unread,
Apr 12, 2011, 5:56:19 AM4/12/11
to

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

Anthony Hodsdon

unread,
Apr 12, 2011, 5:56:30 AM4/12/11
to
Plot[Evaluate[D[x^3 - 6 (x + 1)^2 + x - 7, x]], {x, -3, 8}]

--Anthony

Wolfgang Windsteiger

unread,
Apr 12, 2011, 5:57:13 AM4/12/11
to
Dear Jakub,

this is due to

In[3]:= Attributes[Plot]
Out[3]= {HoldAll, Protected}

You must force Mathematica to evaluate the first parameter of Plot
before Plot goes on processing. The easiest way to accomplish this is an
explicit "Evaluate":

Plot[Evaluate[D[x^3 - 6 (x + 1) 2 + x - 7, x]], {x, -3, 8}]

Hope this helps,
ciao,
WW.

On 04/11/2011 01:05 PM, Šerých Jakub wrote:
> Plot[D[x3 - 6 (x + 1)2 + x - 7, x],{x,-3,8}]

Alexei Boulbitch

unread,
Apr 12, 2011, 5:57:23 AM4/12/11
to
Hi, Jakub,

might this:

Plot[D[x^3 - 6 (x + 1)^2 + x - 7, x] // Evaluate, {x, -3, 8}]

be what you are looking for?

Have fun, Alexei


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

--
Alexei Boulbitch, Dr. habil.
Senior Scientist
Material Development

IEE S.A.
ZAE Weiergewan
11, rue Edmond Reuter
L-5326 CONTERN
Luxembourg

Tel: +352 2454 2566
Fax: +352 2454 3566
Mobile: +49 (0) 151 52 40 66 44

e-mail: alexei.b...@iee.lu

www.iee.lu

--

This e-mail may contain trade secrets or privileged, undisclosed or
otherwise confidential information. If you are not the intended
recipient and have received this e-mail in error, you are hereby
notified that any review, copying or distribution of it is strictly
prohibited. Please inform us immediately and destroy the original
transmittal from your system. Thank you for your co-operation.


Bob Hanlon

unread,
Apr 12, 2011, 5:57:45 AM4/12/11
to

f[x_] = x^3 - 6 (x + 1)^2 + x - 7;

Plot[f'[x], {x, -3, 8}]

Attributes[Plot]

{HoldAll, Protected}

Plot[Evaluate[D[f[x], x]], {x, -3, 8}]


Bob Hanlon

---- "=C5 er=C3=BDch Jakub" <Ser...@panska.cz> wrote:

==========================

Peter Breitfeld

unread,
Apr 12, 2011, 5:58:06 AM4/12/11
to
Šerých Jakub wrote:

> Plot[D[x^3 - 6 (x + 1)^2 + x - 7, x],{x,-3,8}]

Use Evaluate:

Plot[D[x^3 - 6 (x + 1)^2 + x - 7, x] // Evaluate, {x, -3, 8}]

--
_________________________________________________________________
Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de

DC

unread,
Apr 12, 2011, 5:58:38 AM4/12/11
to
Plot[D[x^3 - 6 (x + 1)^2 + x - 7, x] /. x->dummy,{dummy,-3,8}]

Richard Hofler

unread,
Apr 12, 2011, 5:52:53 AM4/12/11
to
Hello Jakub,

In[9]:= deriv=D[x^3-6 (x+1)^2+x-7,x];
plot1 = Plot[deriv,{x,-3,8}]

An alternative method with Evaluate inserted:
In[11]:= plot2 = Plot[Evaluate[D[x^3-6 (x+1)^2+x-7,x]],{x,-3,8}]

The two plots are the same.
In[12]:= plot1==plot2
Out[12]= True

Richard Hofler
________________________________________
From: =8Aer=FDch Jakub [Ser...@panska.cz]
Sent: Monday, April 11, 2011 7:05 AM


Subject: How to plot derivative directly?

Dear mathgroup,

Heike Gramberg

unread,
Apr 12, 2011, 6:53:30 AM4/12/11
to
This has to do with the fact that Plot has attribute HoldAll (you can check this
with Attributes[Plot]). This means that the derivative doesn't get evaluated until
after w has been replaced with numerical values. To overcome this, you need to
tell mathematica to evaluate the derivative before plugging in numbers. This can
be done with evaluate:

Plot[Evaluate[D[x^3 - 6 (x + 1)^2 + x - 7, x]], {x, -3, 8}]

Heike

papu...@gmail.com

unread,
Mar 30, 2013, 4:06:56 AM3/30/13
to
Jakub,

Just use a very simple command
Plot[Evaluate[D[x^3-6 (x+1)^2+x-7,x]],{x,-3,8}]

Chair

0 new messages