If I define

f[x_, j_] = D[E^x^2, {x, j}];

and then evaluate

f[0, 2]

I get the error

General::ivar: 0 is not a valid variable.

How can I tell Mathematica to evaluate the derivative first before setting $x = 0$?

  • $\begingroup$ You could use Derivative[2][E^#^2 &][0] or more generally Derivative[2][E^#^2 &][x]. $\endgroup$ Commented May 25, 2017 at 15:00
  • $\begingroup$ @b.gatessucks Just tested this. For some reason this increases evaluation time almost 5x. Any ideas what might be causing this? $\endgroup$
    – Janosh
    Commented May 25, 2017 at 15:29
  • $\begingroup$ Related: mathematica.stackexchange.com/questions/20217/… $\endgroup$
    – Michael E2
    Commented Oct 25, 2019 at 10:47

1 Answer 1


Use Rules :

In[18]:= f[x1_, j_] := D[E^x^2, {x, j}] /. x -> x1

In[19]:= f[0, 2]

Out[19]= 2
  • 3
    $\begingroup$ It is better to localize dummy variable, i.e. f[x_, j_] := Module[{dummy}, D[E^dummy^2, {dummy, j}] /. dummy->x], otherwise one can get an error if x is defined somewhere before. $\endgroup$
    – Ray Shadow
    Commented May 25, 2017 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.