I am given a perturbative action $$\frac{S}{\mathcal{T}}=\int dt\sum _{n=0,1} (\dot{c_n}{}^2-c_n^2 \omega _n^2)+7.11 c_0^3+35.3 c_0 c_1^2+4.66 c_0 \dot{c_0}{}^2+1.32 c_0 \dot{c_1}{}^2-7.57 \dot{c_0} c_1 \dot{c_1}$$ ($\omega _0^2=-1.4$ and $\omega _1^2=7.57$).

    \[Omega]sq[0] = -1.4; \[Omega]sq[1] = 7.57;
    lagrangian = 
      Sum[c[n]'[t]^2 - c[n][t]^2 \[Omega]sq[n], {n, {0, 1}}] + 
       7.11 c[0][t]^3 + 35.3 c[0][t] c[1][t]^2 + 
       4.66 c[0][t] c[0]'[t]^2 + 1.32 c[0][t] c[1]'[t]^2 - 
       7.57 c[0]'[t] c[1][t] c[1]'[t];

I tried finding the hamiltonian by hand and found out it to be equal to:

    H[t_] = c[0]'[t]^2 + c[1]'[t]^2 + 4.66 c[0][t] c[0]'[t]^2 + 
       1.32 c[0][t] c[1]'[t]^2 - 7.57 c[0]'[t] c[1][t] c[1]'[t] - 
       1.40 c[0][t]^2 + 7.57 c[1][t]^2 - 7.11 c[0][t]^3 - 
       35.3 c[0][t] c[1][t]^2;

How shall I use Mathematica to verify the above result?

  • 1
    $\begingroup$ (1) Is this a Mathematica question? (2) If so please include code and define all symbols (3) Do you seek to codify an existing algorithm or are you searching for a new algorithm? If so then can this question can be asked more suitably at the Math SE? $\endgroup$
    – Syed
    Commented Apr 26, 2022 at 14:05
  • $\begingroup$ I don't understand what you mean by easily. Isn't the Lagrangian the integrand of the action? Hence, a sum of derivatives w.r.t to the dotted coordinates of the iintegrand times themselves minus the integrand? @Syed the code is there. Is it possible you missed the edit? $\endgroup$
    – bmf
    Commented Apr 26, 2022 at 14:05
  • $\begingroup$ @bmf yes basically I wish to find the hamiltonian from the lagrangian. Since I am working with a complicated problem where the numerical values keep changing, I am trying to find a suitable code to work around the problem, instead of doing it repetitively on a notebook. I have checked out this question: mathematica.stackexchange.com/questions/113017/… but don't understand how to implement it in my problem. $\endgroup$
    – codebpr
    Commented Apr 26, 2022 at 14:15

1 Answer 1


The procedure is basically what you would do by hand, $ \mathcal{H} = \pi \dot{\phi} - \mathcal{L} $ So just define the momentum $\pi$ as usual

momentum[n_] := D[lagrangian, c[n]'[t]]

and then

hamiltonian = Sum[momentum[n] c[n]'[t], {n, {0, 1}}] - lagrangian

gives the result you found by hand (if you Expand). If you need a symbol with a passable t, you can add

h[t_] = hamiltonian
  • $\begingroup$ Thank you for the explanation! The results match perfectly :) $\endgroup$
    – codebpr
    Commented Apr 26, 2022 at 15:03

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.