From: Andrew DeYoung on
Hi,

It seems like Mathematica has difficulty plotting calls to other
functions. Is this true? For example, can you help me understand why
the following does not plot?

If I write

myFunction[x_] := x^2;
Plot[D[myFunction[x], x], {x, 0, 5}]

nothing plots.

If I write

myFunction[x_] := x^2;
Plot[D[myFunction[x][x], x], {x, 0, 5}]

again, nothing plots.

However, if I compute the derivative of the function outside of the
Plot command,

myFunction[x_] := x^2;
myDerivative = D[myFunction[x], x]
Plot[myDerivative, {x, 0, 5}]

the derivative of x^2 (i.e., 2x) plots correctly.

Can anyone please help me understand why my first two tries do not
work, but my third try does?

Many thanks in advance.

Andrew DeYoung
Carnegie Mellon University
adeyoung(a)andrew.cmu.edu

From: David Park on
The reason you obtain nothing is that Plot has the Attribute HoldFirst,
which means that the plot expression is not immediately evaluated. Instead,
Mathematica substitutes x values into it and only then evaluates. So say
that Mathematica evaluates at x = 0.1. Then the plot expression becomes
D[myFunction[0.1],0.1], which makes no sense and will not return a value.

The solution is to tell Mathematica to evaluate the derivative first.

myFunction[x_] := x^2;

Plot[D[myFunction[x], x] // Evaluate, {x, 0, 5}]

In this case, there is an even simpler construction.

Plot[myFunction'[x], {x, 0, 5}]

If you had a function with parameters, defined as f[a_,b_][x_]:= expression
in a, b and x, then you could use f[2,3]`[x] say as a plot function of the
derivative with specific parameters.

If you have a plot function that involves complicated processing, such as
Integral, then it is worthwhile to define the plot function outside of any
Plot statement and make certain you know what you are dealing with - by
looking at the resulting expression, or evaluating at some points, or
looking at it with Table. This untangles the plot function definition from
the plotting algorithm and simplifies any debugging.


David Park
djmpark(a)comcast.net
http://home.comcast.net/~djmpark/



From: Andrew DeYoung [mailto:adeyoung(a)andrew.cmu.edu]

Hi,

It seems like Mathematica has difficulty plotting calls to other
functions. Is this true? For example, can you help me understand why
the following does not plot?

If I write

myFunction[x_] := x^2;
Plot[D[myFunction[x], x], {x, 0, 5}]

nothing plots.

If I write

myFunction[x_] := x^2;
Plot[D[myFunction[x][x], x], {x, 0, 5}]

again, nothing plots.

However, if I compute the derivative of the function outside of the
Plot command,

myFunction[x_] := x^2;
myDerivative = D[myFunction[x], x]
Plot[myDerivative, {x, 0, 5}]

the derivative of x^2 (i.e., 2x) plots correctly.

Can anyone please help me understand why my first two tries do not
work, but my third try does?

Many thanks in advance.

Andrew DeYoung
Carnegie Mellon University
adeyoung(a)andrew.cmu.edu



From: Curtis Osterhoudt on
myFunction[x_] := x^2;
Plot[Evaluate[ D[myFunction[x], x] ], {x, 0, 5}]



The function "Plot" has the attribute "HoldAll", which means that it holds the argument unevaluated for as long as possible. I think it does this to keep things in an analytic state, and allows things such as symbolic manipulation (as your differentiation operator, "D", uses). In this case (as in many plotting cases), the use of Evaluation makes sure that things resolve to numbers when they should.

At least in earlier versions of Mathematica, it was emphasized that one should Evaluate arguments to Plot whenever possible; in the latest documentation, I don't see that emphasis.




On Tuesday, August 03, 2010 04:34:19 Andrew DeYoung wrote:
> Hi,
>
> It seems like Mathematica has difficulty plotting calls to other
> functions. Is this true? For example, can you help me understand why
> the following does not plot?
>
> If I write
>
> myFunction[x_] := x^2;
> Plot[D[myFunction[x], x], {x, 0, 5}]
>
> nothing plots.
>
> If I write
>
> myFunction[x_] := x^2;
> Plot[D[myFunction[x][x], x], {x, 0, 5}]
>
> again, nothing plots.
>
> However, if I compute the derivative of the function outside of the
> Plot command,
>
> myFunction[x_] := x^2;
> myDerivative = D[myFunction[x], x]
> Plot[myDerivative, {x, 0, 5}]
>
> the derivative of x^2 (i.e., 2x) plots correctly.
>
> Can anyone please help me understand why my first two tries do not
> work, but my third try does?
>
> Many thanks in advance.
>
> Andrew DeYoung
> Carnegie Mellon University
> adeyoung(a)andrew.cmu.edu
>
>


--
==================================
Curtis Osterhoudt
cfo(a)remove_this.lanl.and_this.gov
==================================

From: David Reiss on

Because you are differenciating with respect to a number.

The value of x in the argument of the plot function is substituted
first and then the argument is evaluated, effectively resulting in
attempting to differentiate your function evaluated at numerical
values with respect to those numerical values.

Try the alternative


Plot[D[myFunction[z], z] /. z -> x, {x, 0, 5}]

or

Plot[Evaluate[D[myFunction[x], x] ], {x, 0, 5}]

to force the differentiation to take place first (since Plot has the
HoldAll attribute).


which will work. However you may want to think through why you are
including the unevaluated differentiation within the Plot command
anyway: it will take place at each plot point and thus be quite
inefficient...

Hope this helps,

David

On Aug 3, 6:34 am, Andrew DeYoung <adeyo...(a)andrew.cmu.edu> wrote:
> Hi,
>
> It seems like Mathematica has difficulty plotting calls to other
> functions. Is this true? For example, can you help me understand why
> the following does not plot?
>
> If I write
>
> myFunction[x_] := x^2;
> Plot[D[myFunction[x], x], {x, 0, 5}]
>
> nothing plots.
>
> If I write
>
> myFunction[x_] := x^2;
> Plot[D[myFunction[x][x], x], {x, 0, 5}]
>
> again, nothing plots.
>
> However, if I compute the derivative of the function outside of the
> Plot command,
>
> myFunction[x_] := x^2;
> myDerivative = D[myFunction[x], x]
> Plot[myDerivative, {x, 0, 5}]
>
> the derivative of x^2 (i.e., 2x) plots correctly.
>
> Can anyone please help me understand why my first two tries do not
> work, but my third try does?
>
> Many thanks in advance.
>
> Andrew DeYoung
> Carnegie Mellon University
> adeyo...(a)andrew.cmu.edu


From: Themis Matsoukas on
The error messages give a hint of what is going on:

myFunction[x_] := x^2;
Plot[D[myFunction[x], x], {x, 0, 5}]

General::ivar: 0.00010214285714285715` is not a valid variable. >>

Plot works by evaluating myFunction at fixed x but then D[] fails b/c it expects x to be a variable, not a number. You will get the same error is you try to execute the following:

N[D[myFunction[x], x = 0.000102]]

You can avoid this conflict by using separate variables in D[] and in Plot[]:

myFunction[x_] := x^2;
Plot[D[myFunction[y], y] /. y -> x, {x, 0, 5}]

Themis

 |  Next  |  Last
Pages: 1 2
Prev: Exporting into PowerPoint
Next: StringForm v.s Row