Re: Plotting a function and its derivative

• To: mathgroup at smc.vnet.net
• Subject: [mg49529] Re: Plotting a function and its derivative
• Date: Thu, 22 Jul 2004 02:47:10 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

>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. Plot::plnr:
>d[x] is not a machine-size real number at x = -4.9999995833333335`
>Plot::plnr: d[x] is not a machine-size real number at x =
>-4.594330084270842` and a few more similar plot errors

The problem you are having is due to the way Plot evaluates its arguements. What is happening is Plot sends your function d a numeric value and d attempts to take the derivative of a numeric value with respect to x. This is clearly not what you want.

There are two ways around this. First, you can define d using Set rather than DelayedSet. That will cause the derviative to be evaluated before Plot tries to substitute a value for x.

The second way would be to use Evaluate to force evaluation of the derivative before Plot substitutes values for x, i.e., with your d defined as is do:

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

Look at the discussion of Plot, Set and DelayedSet in the Mathematica Book for a more complete discussion.
--
To reply via email subtract one hundred and four

• Prev by Date: RE: rounding
• Next by Date: RE: Plotting a contour plot with cylindrical co-ordinates
• Previous by thread: Re: Plotting a function and its derivative
• Next by thread: Re: Re: Plotting a function and its derivative