For the Lagrangian:

T = 1/2 Subscript[J, 1] Derivative[1][Subscript[\[Phi], 1]][t]^2 + 
  1/2 Subscript[J, 2] Derivative[1][Subscript[\[Phi], 2]][t]^2

P = 1/2 Subscript[c, 
  12] (Subscript[\[Phi], 1][t] - Subscript[\[Phi], 2][t])^2

L = T - P

The equations of motion will look like:

eqns = {D[D[L, Derivative[1][Subscript[\[Phi], 1]][t]], t] - D[L, Subscript[\[Phi], 1][t]], D[D[L, Derivative[1][Subscript[\[Phi], 2]][t]], t] - D[L, Subscript[\[Phi], 2][t]]}

Now let's try to restore the Lagrangian using the inverse formula:

$L = \int \int \frac{d}{dt}\frac{\partial L}{\partial \dot{q}} \ dt \ d \dot{q} + \int \frac{\partial L}{\partial q} \ dq$

restoredL = Integrate[
  Integrate[D[D[L, Derivative[1][Subscript[\[Phi], 1]][t]], t], t], 
  Derivative[1][Subscript[\[Phi], 1]][t]] + 
 Integrate[-D[L, Subscript[\[Phi], 1][t]], Subscript[\[Phi], 1][t]] + 
  Integrate[D[D[L, Derivative[1][Subscript[\[Phi], 2]][t]], t], t], 
  Derivative[1][Subscript[\[Phi], 2]][t]] + 
 Integrate[-D[L, Subscript[\[Phi], 2][t]], Subscript[\[Phi], 2][t]]

And here is the result. This is the original Lagrangian:

enter image description here

And restored:

enter image description here

This is the first and most obvious approach. Yes, the results are similar, but still they differ (because of the work with the integration operation and possibly not quite the right combination of the integration results). Are there methods or commands in Mathematica for restoring the Lagrangian from the ODE or ODE's system.


Clear["Derivative"]; ClearAll["Global`*"]

L = -(1/2) (x - y)^2 + 1/2 Derivative[1][x][t]^2 + 
    1/2  Derivative[1][y][t]^2 // Expand;

D[D[L, Derivative[1][x][t]], t] - D[L, x] // Simplify;

D[D[L, Derivative[1][y][t]], t] - D[L, y] // Simplify;

DSolve[{D[Lag[x, y], x] == (x - y) + x^\[Prime]\[Prime], 
   D[Lag[x, y], y] == -(x - y) + y^\[Prime]\[Prime]}, {Lag[x, y]}, {x,

enter image description here

  • 1
    $\begingroup$ could you provide a link/reference to where you obtained the inverse formula? There might be different interpretations. btw, I think using subscripted variables makes your code not readable when it is outside the notebook. That is the problem with 2D math in general. $\endgroup$
    – Nasser
    Nov 15, 2022 at 15:59
  • $\begingroup$ @Nasser The inverse formula is a "naive" (oh, that's a word...) replacement of differentiation operations by integration in the structure of the Lagrange-Euler equations. $\endgroup$
    – dtn
    Nov 15, 2022 at 16:09
  • 1
    $\begingroup$ I think I found where you went wrong with your naive method. Will try to write something. hard to explain in comments might need to write some latex. $\endgroup$
    – Nasser
    Nov 15, 2022 at 17:19

1 Answer 1


Are there methods or commands in Mathematica for restoring the Lagrangian from the ODE or ODE's system.

The bottom line is that the Lagrangian is not unique. So the answer is no. This is like asking given the determinant, can one restore the matrix that generated this (even if the size is known)?

But lets look at what you did. The main thing to note in all of the following is that $$ \int\frac{\partial f\left( x,y\right) }{\partial x}dx\neq f\left( x,y\right) +c $$ Where $c$ is constant of integration. This is because the integrand here is a partial derivative and not normal derivative. The above should be $$ \int\frac{\partial f\left( x,y\right) }{\partial x}dx=f\left( x,y\right) +g\left( y\right) $$ Where $g$ an arbitrary function (not arbitrary constant) of the other independent variable.

This is very important. Because Mathematica will simply give $f\left( x,y\right) $ in both cases which is what you did and why you obtained the result you did.

Mathematica graphics

This is because CAS software does not generate "constant" of integration by default.

Now lets look at what you did from the start \begin{align} T & =\frac{1}{2}J_{1}\left( \phi_{1}^{\prime}\right) ^{2}+\frac{1}{2} J_{2}\left( \phi_{2}^{\prime}\right) ^{2}\nonumber\\ P & =\frac{1}{2}c\left( \phi_{1}-\phi_{2}\right) ^{2}\nonumber\\ L & =T-P\nonumber\\ & =\frac{1}{2}J_{1}\left( \phi_{1}^{\prime}\right) ^{2}+\frac{1}{2} J_{2}\left( \phi_{2}^{\prime}\right) ^{2}-\frac{1}{2}c\left( \phi_{1} -\phi_{2}\right) ^{2}\tag{1} \end{align} Hence $$ L\equiv L\left( \phi_{1},\phi_{2},\phi_{1}^{\prime},\phi_{2}^{\prime },t\right) $$ Where the time $t$ dependency is implicit. The above does not include $J_{1},J_{2}$ as these are assumed constant. (masses). The equations of motion \begin{align} \frac{d}{dt}\left( \frac{\partial L}{\partial\phi_{1}^{\prime}}\right) -\frac{\partial L}{\partial\phi_{1}} & =0\tag{2}\\ \frac{d}{dt}\left( \frac{\partial L}{\partial\phi_{2}^{\prime}}\right) -\frac{\partial L}{\partial\phi_{2}} & =0\nonumber \end{align} These give \begin{align*} \frac{d}{dt}\left( \frac{\partial}{\partial\phi_{1}^{\prime}}\left( \frac {1}{2}J_{1}\left( \phi_{1}^{\prime}\right) ^{2}+\frac{1}{2}J_{2}\left( \phi_{2}^{\prime}\right) ^{2}-\frac{1}{2}c\left( \phi_{1}-\phi_{2}\right) ^{2}\right) \right) -\frac{\partial}{\partial\phi_{1}}\left( \left( \frac{1}{2}J_{1}\left( \phi_{1}^{\prime}\right) ^{2}+\frac{1}{2}J_{2}\left( \phi_{2}^{\prime}\right) ^{2}-\frac{1}{2}c\left( \phi_{1}-\phi_{2}\right) ^{2}\right) \right) & =0\\ \frac{d}{dt}\left( \frac{\partial}{\partial\phi_{2}^{\prime}}\left( \frac {1}{2}J_{1}\left( \phi_{1}^{\prime}\right) ^{2}+\frac{1}{2}J_{2}\left( \phi_{2}^{\prime}\right) ^{2}-\frac{1}{2}c\left( \phi_{1}-\phi_{2}\right) ^{2}\right) \right) -\frac{\partial}{\partial\phi_{2}}\left( \left( \frac{1}{2}J_{1}\left( \phi_{1}^{\prime}\right) ^{2}+\frac{1}{2}J_{2}\left( \phi_{2}^{\prime}\right) ^{2}-\frac{1}{2}c\left( \phi_{1}-\phi_{2}\right) ^{2}\right) \right) & =0 \end{align*} Or \begin{align*} \frac{d}{dt}\left( J_{1}\phi_{1}^{\prime}\right) -\left( -c\left( \phi _{1}-\phi_{2}\right) \right) & =0\\ \frac{d}{dt}\left( J_{2}\phi_{2}^{\prime}\right) -\left( c\left( \phi _{1}-\phi_{2}\right) \right) & =0 \end{align*} Or \begin{align} J_{1}\phi_{1}^{\prime\prime}+c\left( \phi_{1}-\phi_{2}\right) & =0\tag{3}\\ J_{2}\phi_{2}^{\prime\prime}-c\left( \phi_{1}-\phi_{2}\right) & =0\nonumber \end{align} So far so good. Lets now try to obtain $L$ from (2) and see if we get (1). We can use either equation of motion. Using the first in (2) gives $$ \frac{d}{dt}\left( \frac{\partial L}{\partial\phi_{1}^{\prime}}\right) =\frac{\partial L}{\partial\phi_{1}}% $$ Integrating both sides gives \begin{align} \int\frac{d}{dt}\left( \frac{\partial L}{\partial\phi_{1}^{\prime}}\right) dt & =\int\frac{\partial L}{\partial\phi_{1}}d\phi_{1}\nonumber\\ \int d\left( \frac{\partial L}{\partial\phi_{1}^{\prime}}\right) & =\int\frac{\partial L}{\partial\phi_{1}}d\phi_{1}\nonumber\\ \frac{\partial L}{\partial\phi_{1}^{\prime}} & =\int\frac{\partial L}{\partial\phi_{1}}d\phi_{1}\tag{4}% \end{align} But $$ \int\frac{\partial L}{\partial\phi_{1}}d\phi_{1}=L+f\left( \phi_{2},\phi _{2}^{\prime},\phi_{1}^{\prime},t\right) $$ Where $f\left( \phi_{2},\phi_{2}^{\prime},\phi_{1}^{\prime},t\right) $ is arbitrary constant of all the other independent variables that $L$ depends on. Hence (4) now becomes $$ \frac{\partial L}{\partial\phi_{1}^{\prime}}=L+f\left( \phi_{2},\phi _{2}^{\prime},\phi_{1}^{\prime},t\right) $$ This is first order pde in $L$. Since it contains an arbitrary function, there is no unique solution to $L$. Integrating again will generate new arbitrary function. Any combination of these arbitrary function that satisfy the ode give a valid Lagrangian that can be used.

Here is a nice youtube video on this also.


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.