質問

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.

役に立ちましたか?

解決

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 :=.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top