How do I evaluate the n'th order derivative of a variable-function

This is what I am trying to do:

tmp[func_, arg_, order_] = Derivative[order][func][arg]

Which evaluates to:

$func^{(order)}[x]$

This seems to be correct, but how can I get the software to evaluate the function at x?

tmp[3x+2, 1, 0]

produces

(2 + 3 x)

and

f[x_] = 3x+2

tmp[f[x], 1, 0] produces the same thing

However, if I write the function explicitly,

f[x_] = 3x+2

tmp[arg_, order_] = Derivative[order][f][arg]

with

tmp[1, 0]
tmp[1, 1]

I get the expected output of 5 and 3.

How do I need to define my tmp function so that tmp[3x+2, 1, 0] evaluates fully?

f[x_] = 2x+1

tmp[func_, arg_, order]

tmp[f, 2, 1]

This seems to work, but I still don't know how to define tmp in such a way as to allow me to pass in the function or write it within the call.

• Your func needs to be a Function object. If you feed your tmp function with just 2 + 3 x, it has no way of knowing what the variable is. Consider: what would happen if you put 2 y + 3 x in it? Do you get 3 (the variable is x)? 2 (the variable is y) ? or 0 (the variable is something else)? Here's the link to the documentation on Function. – JungHwan Min Nov 20 '16 at 23:31
• Also, if you are making a function that changes value according to input, use SetDelayed (:=). i.e. tmp[func_, arg_, order_] := Derivative[order][func][arg] and f[x_] := 2x+1. Just Set (=) will cause problems. – JungHwan Min Nov 20 '16 at 23:35
• To add to the first comment, if x is already defined somewhere else, and it is not Cleared, 2 + 3 x would be evaluated and your derivative would not work. – JungHwan Min Nov 20 '16 at 23:41

Try this:

Clear[tmp];
tmp[func_, arg_, ord_,
argFix_] := (D[func, {arg, ord}] // Activate) /. arg -> argFix

Here argis the argument of the function and argFix is its value that you want it to take after the derivative calculation.

Let us try:

tmp[3 x + 2, x, 1, 1]
tmp[3 x + 2, x, 2, 1]

(*  3

0   *)

As it should be. Have fun!

tmp[func_, arg_, order_] = Derivative[order][func][arg];

The func argument must be a pure function or the Head of a defined function.

Either

tmp[3 # + 2 &, 1, 0]

(*  5  *)

or

tmp[Function[x, 3 x + 2], 1, 0]

(*  5  *)

or

Clear[f]

f = 3 # + 2 &;

tmp[f, 1, 0]

(*  5  *)

or

Clear[f]

f = Function[x, 3 x + 2];

tmp[f, 1, 0]

(*  5  *)

or as you observed.

Clear[f]

f[x_] = 3 x + 2;

tmp[f, 1, 0]

(*  5  *)