2
$\begingroup$

Consider a function

derivative[x_]:=D[Sin[x],x]

When calling it with some argument, like derivative[2], instead of $\cos(x)$ it gives me $\partial_{2}\sin(2)$. Is it possible to define a function in one string that first takes the derivative and then returns it at the given point? And to generalize this to an arbitrary function f, i.e.

derivative[f_,x_]:=D[f,x]
$\endgroup$
2
  • 1
    $\begingroup$ Derivative[1][f][x], here f is a pure function. I think you want a function such as derivative[f_,x_,x0_] ? $\endgroup$
    – cvgmt
    Commented Oct 28, 2020 at 23:24
  • $\begingroup$ @cvgmt : yes, you are correct. $\endgroup$ Commented Oct 29, 2020 at 7:55

2 Answers 2

4
$\begingroup$

I think simplest and cleanest way would be to use

derivative[f_] := f'

E.g. if you evaluate

derivative[Sin]

you get the pure function

Cos[#1] &

and therefore

derivative[Sin][x0]

returns

Cos[x0]
$\endgroup$
2
  • $\begingroup$ Oh this is much more elegant than my solution! Does this generalise to arbitrary derivatives, e.g. derivative[f_] := f^(1,0) for f[x_,y_] = Cos[x]Sin[y] for example? $\endgroup$ Commented Nov 1, 2020 at 12:42
  • $\begingroup$ For that case you might want to take a look at Derivative. You could say derivative[f_] := Derivative[1,0][ f ]; Beware that in that case you need too invoke derivative[ Cos[#1] Sin[#2]& ], (or g[x_,y_] := Cos[x]Sin[y]; derivative[g] ) because your function takes 2 arguments and mathematica needs to know which one is the first argument and which one the second. $\endgroup$
    – Gert
    Commented Nov 1, 2020 at 21:23
4
$\begingroup$

I think the issue here is one of the order of operations. Consider instead:

derivative[x_] := ( D[Sin[y],y] /. y -> x )

And, more generally,

derivative[f_, x_] := ( D[f[y], y] /. y -> x)
$\endgroup$

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.