Re: Plotting a function and its derivative

• To: mathgroup at smc.vnet.net
• Subject: [mg49557] Re: Plotting a function and its derivative
• Date: Fri, 23 Jul 2004 05:59:58 -0400 (EDT)
• Organization: EM Computer Facility, The University of Vermont
• References: <cdljc0\$85i\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```adrian sky wrote:
> first of all, thanks for reading this message, i would imagine this is
> a beginner level question, and sorry if this was answered previously,
> im still getting the hang of proper searches in a mathematical forum.
>
> anyways, what i'm trying to do is illustrate the simplest of
> derivatives in the form of a graph but i am getting errors.
>
> the function i'm trying to derive is x^2
> its derivative i'm trying to illustrate is 2x
>
> i would like to set up a graph which allows me to determine the
> argument (be it x^2 or x^4+1), calculate its derivative, and graph them
> so as to illustrate their relationship.
>
> i know that the derivative can be graphed at any x value, and as far as
> this is concerned, the value can be anything, probably a small value to
> keep the graph manageable.
>
> anyways, i'm inputting this:
> f[x_] := x^2; d[x_] := ¶_x f[x]
> which supposedly should assign the f(x) a value of ^2 and d(x) a value
> of the derivative of f(x)
>
> i can even type f(x) and get back the value x^2 and d(x) gives me 2x,
> so it seems the system understands and assigns the requested values
>
> but when I try to plot this with the command:
> Plot[{f[x], d[x]}, {x, -5, 5}]
> i get numerous errors and only get the graph of x^2
> the errors are:
> General::ivar: -5. is not a valid variable.

The problem is the delayed evaluation on d[x]. Try this:

Plot[{f[x], Evaluate[d[x]]}, {x, -5, 5}]

A simpler way to do it is the following.

f[x_]=x^2

Plot[{f[x],f'[x]},{x,-5,5}]

You might want to make them different colors while you are at it.

Plot[{f[x],f'[x]},{x,-5,5},PlotStyle->{RGBColor[1,0,0],RGBColor[0,0,1]}]

--