MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Issuing Function Calls within a Plot command

  • To: mathgroup at
  • Subject: [mg111518] Re: Issuing Function Calls within a Plot command
  • From: "David Park" <djmpark at>
  • Date: Wed, 4 Aug 2010 05:48:03 -0400 (EDT)

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 at  

From: Andrew DeYoung [mailto:adeyoung at] 


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 at

  • Prev by Date: Re: Scoping constructs Block, Module, ModuleBlock
  • Next by Date: Re: Bare Bones Backup Button
  • Previous by thread: Re: Issuing Function Calls within a Plot command
  • Next by thread: Re: Issuing Function Calls within a Plot command