       RE: Plotting a function and its derivative

• To: mathgroup at smc.vnet.net
• Subject: [mg49504] RE: [mg49478] Plotting a function and its derivative
• From: "David Park" <djmp at earthlink.net>
• Date: Thu, 22 Jul 2004 02:45:30 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Adrian,

I don't know what you were doing with your d definition. In any case, you
can write the derivative of a function f[x] simply as f'[x]. Or you could
use the longer form
D[f[x],x].

Then, as an example, you could plot a function and its derivative as...

Needs["Graphics`Colors`"]

Plot[{f[x], f'[x]} // Evaluate, {x, 0, 2}, PlotStyle -> {Black, Red},
ImageSize -> 400];

If you wanted to be a little more fancy you could use...

Plot[{f[x], f'[x]} // Evaluate, {x, 0, 2},
PlotStyle -> {Black, Red},
Frame -> True,
FrameLabel -> {x, None},
PlotLabel ->
SequenceForm[HoldForm[f[x]] == f[x], ", ",
StyleForm[HoldForm[f'[x]] == f'[x], FontColor -> Red]],
Background -> Linen,
ImageSize -> 400];

If you want to plot a function and the tangent line at a point you could
first define a general tangent line as...

tangentline[f_, x0_][x_] := f[x0] + (x - x0)f'[x0]

We can then plot the curve and the tangent line at a specific value of x0. I
put the Plot statement inside a With statement that defines x0. You can
easily change x0 and reevaluate the plot.

With[
{x0 = 1.5},
Plot[{f[x], tangentline[f, x0][x]} // Evaluate, {x, 0, 2},
PlotStyle -> {Black, Red},
Epilog -> {AbsolutePointSize, Point[{x0, f[x0]}]}]];

Or better yet, you can easily turn this into an animation.

Needs["Graphics`Animation`"]

frame[x0_] :=
Plot[{f[x], tangentline[f, x0][x]} // Evaluate, {x, 0, 2},
PlotStyle -> {Black, Red},
Epilog -> {AbsolutePointSize, Point[{x0, f[x0]}]},
PlotRange -> {-2, 20}]

The PlotRange option was necessary to insure that all frames of the
animation have the same range, otherwise you will get jumping of images when
Mathematica picks different ranges as it tries to show what is "most
interesting" in each frame plot. You can then display the animation with...

Animate[frame[x0], {x0, 0, 2, 0.1}]
SelectionMove[EvaluationNotebook[], All, GeneratedCell]
FrontEndTokenExecute["OpenCloseGroup"]; Pause[0.5];
FrontEndExecute[{FrontEnd`SelectionAnimate[200, AnimationDisplayTime -> 0.1,
AnimationDirection -> ForwardBackward]}]

The last three statements select all the graphic cells, close them up, and
starts the animation with the given characteristics. You don't really need
them.

David Park

To: mathgroup at smc.vnet.net

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

anyways, to simplify my question, how to i plot a function (like x^2)
and its derivative (like 2x) and apply its derivative to any part of
the function (like x=3) so as to show the tangent line which should
touch the function? i thought my way should have worked, but it seems
the d(x) is the troublesome function, since without it, i get a graph
of the function (albeit without the derivative).....

many thanks for looking this question over!

For a man to attain to an eminent degree in learning costs him time,
watching, hunger, nakedness, dizziness in the head, weakness in the
stomach, and other inconveniences. - Miguel De Cervantes

```

• Prev by Date: Re: rounding
• Next by Date: Re: rounding
• Previous by thread: Re: Plotting a function and its derivative
• Next by thread: Re: Plotting a function and its derivative