0
$\begingroup$

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$?

$\endgroup$
  • $\begingroup$ You could use Derivative[2][E^#^2 &][0] or more generally Derivative[2][E^#^2 &][x]. $\endgroup$ – b.gates.you.know.what May 25 '17 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$ – Casimir May 25 '17 at 15:29
1
$\begingroup$

Use Rules :

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

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

Out[19]= 2
$\endgroup$
  • 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$ – Shadowray May 25 '17 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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