Reputation: 185
I'd like to write a Mathematica function that takes an expression as argument, takes the derivative of that expression, and then does something to the expression. So (as a toy example) I'd like to write
F[f_] = D[f, x] * 2
so that
F[x^2] = 4x
Instead, I get
F[x^2] = 0
Can someone point me to the relevant docs? I spent some time poking around the Mathematica reference, but didn't find anything helpful.
Upvotes: 1
Views: 1693
Reputation: 290
You've used assignment =
when you mean to use delayed assignment :=
. When you evaluate F[f_]=D[f,x]*2
using (non-delayed) assignment, Mathematica looks at D[f,x]
and sees that f
(an unassigned symbol) does not depend on x
; hence, its derivative is 0. Thus, F[f_]=0
for any arguments to F
, which is what it returns later.
If you want F
to be evaluated only after you have specified what f_
should be, you need to use delayed assignment by replacing =
with :=
.
Upvotes: 4