# Table of function derivative evaluation

I want to create a table with numerical values of a function derivative by using the following code:

f[x_, a_] := f[x, a] = Tanh[a (1 - Cos[x])];
g[x_, a_] := g[x, a] = D[f[x, a], x];
pderivative = Table[{x, Evaluate[g[x, 5]]}, {x, - π,  π, 2 π/500.}]


When I look at the result, it's not what I expect. Any trick to fix this?

• g = D[f[x, a], x]; pderivative = Table[{x, g /. a -> 5}, {x, -\[Pi], \[Pi], 2 \[Pi]/500.}] Nov 5, 2018 at 10:48
• 1. As mentioned by Henrik in this comment, memoization for functions with real arguments is usually rather pointless as the chances that exactly the same pops up somewhere else are rather low. 2. Don't abuse SetDelayed(:=). Related: mathematica.stackexchange.com/a/181586/1871 Nov 5, 2018 at 11:20

Method I

Define your g by Derivative instead of D:

g[x_, a_] := g[x, a] = Derivative[1, 0][f][x, a];


Note that Evaluate in pderivative is not a necessity.

Method II

Alternatively it also works in this way:

g[x_, a_] = D[f[x, a], x]


FYI, please check this link concerning the difference between Set (=) and SetDelayed (:=).

• Many thanks! Is this due to two variables? Nov 5, 2018 at 11:28
• @BobLin No, see my update. Nov 5, 2018 at 11:53
• Why you take the derivative with respect to $a$? Nov 5, 2018 at 11:56
• @BobLin Oh, sorry, just a typo; Corrected. Nov 5, 2018 at 11:58