4
$\begingroup$

I am new to Mathematica and still struggling to learn basic things. In this spirit, I am trying to compute the following expression:

$$C_n = \left[ \frac{\partial^n W(J)}{\partial J^n}\right]_{J=0},$$ where $W(J) = \ln Z(J) = \ln \left[\sum_{n=0}^{\infty}\frac{1}{n!} J^n G_n\right]$, and $G_n$s are different non-zero constants.

How can I write a function for computing $C_n$ where I would input the value of $n$ and get the corresponding expressions? For instance, $C_1 = G_1$, $C_2 = G_2-G_1^2$, etc. We assume that $G_0=1$.

My current approach is very elementary. It is given below.

Z[J_] = g0 + g1 J + g2/2 J^2 + g3/3! J^3 + g4/4! J^4 + g5/5! J^5 + g6/6! J^6
g0 = 1
W[J_] = Log[Z[J]]
C1[J_] = D[W[J], {J, 1}]
C1[0]

Could you please help me to improve it? A sample code would help me a lot.

$\endgroup$
4
$\begingroup$
Clear["Global`*"]

Z[J_] = g0 + g1 J + g2/2 J^2 + g3/3! J^3 + g4/4! J^4 + g5/5! J^5 + g6/6! J^6;

W[J_] = Log[Z[J]];

The nth derivative of W evaluated at zero is just

c[n_Integer?NonNegative] := c[n] = Derivative[n][W][0]

The first several are

seq1 = c /@ Range[0, 5] // Simplify

(* {Log[g0], g1/g0, (-g1^2 + g0 g2)/g0^2, (
 2 g1^3 - 3 g0 g1 g2 + g0^2 g3)/g0^3, (-6 g1^4 + 12 g0 g1^2 g2 - 
  4 g0^2 g1 g3 + 
  g0^2 (-3 g2^2 + g0 g4))/g0^4, (1/(g0^5))(24 g1^5 - 60 g0 g1^3 g2 + 
   20 g0^2 g1^2 g3 - 5 g0^2 g1 (-6 g2^2 + g0 g4) + g0^3 (-10 g2 g3 + g0 g5))} *)

Alteratively,

c2[n_Integer?NonNegative] := c2[n] = n!*SeriesCoefficient[W[J], {J, 0, n}]

seq2 = c2 /@ Range[0, 5] // Simplify

(* {Log[g0], g1/g0, (-g1^2 + g0 g2)/g0^2, (
 2 g1^3 - 3 g0 g1 g2 + g0^2 g3)/g0^3, (-6 g1^4 + 12 g0 g1^2 g2 - 
  4 g0^2 g1 g3 + 
  g0^2 (-3 g2^2 + g0 g4))/g0^4, (1/(g0^5))(24 g1^5 - 60 g0 g1^3 g2 + 
   20 g0^2 g1^2 g3 - 5 g0^2 g1 (-6 g2^2 + g0 g4) + g0^3 (-10 g2 g3 + g0 g5))} *)

seq1 === seq2

(* True *)
$\endgroup$
3
  • $\begingroup$ What does this part c2[n_Integer?NonNegative] mean? $\endgroup$ – rainman Aug 30 '20 at 5:49
  • $\begingroup$ It restricts the function to only evaluating for arguments that are nonnegative integers. _Integer requires the head of the argument to be Integer (see documentation for Blank) and ?Nonnegative is a pattern test (see documentation for PatternTest). $\endgroup$ – Bob Hanlon Aug 30 '20 at 5:56
  • $\begingroup$ Thanks, Bob! I appreciate your help. $\endgroup$ – rainman Aug 30 '20 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.