I am trying to use SymPy to apply the Euler-Lagrange formalism to the following Lagrangian function:
$$\mathcal{L}(x, \dot{x}) = \mathcal{K}(x, \dot{x}) – \mathcal{P}(x) = \dfrac{1}{2} \mathfrak{m} \dot{x} – \mathfrak{m} g x.$$
The result should be
$$
f = \frac{d}{dt} \frac{\partial\mathcal{L}}{\partial\dot{x}}
– \frac{\partial\mathcal{L}}{\partial x}
= \mathfrak{m} \ddot{x} + \mathfrak{m} g.$$
However, I get $f = \mathfrak{m} g$ with SymPy…
Below is the source of my jupyter notebook.
In [1]: from sympy import *
from sympy.physics.mechanics import *
In [2]: m,g = symbols('m g', real=True)
x = dynamicsymbols('x')
xd = dynamicsymbols('x', 1)
xdd = dynamicsymbols('x', 2)
In [3]: kin_energy = 1/2 * m * xd ** 2
pot_energy = m * g * x
In [4]: L = kin_energy - pot_energy
f = diff(L, xdd) - diff(L, x)
In [5]: pprint(f)
Out[5]: g⋅m
Best Answer
There are a couple of errors in your code
Depending on the version of python you're using, this could be interpreted as either '0' or '1/2'. So To avoid problems just use
Note that
$$ \frac{{\rm d}}{{\rm d}t}\frac{\partial \mathcal{L}}{\partial \dot{x}} \not= \frac{\partial \mathcal{L}}{\partial \ddot{x}} $$
If you implement these two things, you should get