0

If I want to get the solution for a simple function, e.g. a^x, I can use:

a = 2;
Function[x, a^x][2]

However, if I have the function in a variable, I can't get it to work:

a = 2;
func = a^x
Function[x, func][2]

Similarly, this doesn't work as wished, which is that the last line provides the result if 2 is put into x:

a = 2;
func = a^x;
der = D[func, x]
Function[x, der][2]
Mockup Dungeon
  • 1,854
  • 12
  • 16

1 Answers1

1

Using Evaluate,

a = 2;
func = a^x;
Function[x, func][2]
Function[x, Evaluate@func][2]
(* 2^x *)
(* 4 *)

and

a = 2;
func = a^x;
der = D[func, x];
Function[x, der][2]
Function[x, Evaluate@der][2]
(* 2^x Log[2] *)
(* 4 Log[2] *)
Jason B.
  • 68,381
  • 3
  • 139
  • 286