Skip to content

Instantly share code, notes, and snippets.

@hazelnusse
Created June 3, 2011 06:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hazelnusse/1005937 to your computer and use it in GitHub Desktop.
Save hazelnusse/1005937 to your computer and use it in GitHub Desktop.
First approach to forming equations of motion for a simple particle using Lagrange's method
# Form Lagrangian only with symbols
from sympy import symbols, diff, S, Eq
t, m, g, x, v = symbols('t m g x v')
# Kinetic and Potential energies
KE = 1/S(2)*m*v**2
PE = m*g*x
# Lagrangian
L = KE - PE
# Unforced equation of motion
eom1 = Eq(diff(L, x) - diff(diff(L, v), t), 0)
# Note that derivative of diff(L, v) w.r.t. t is zero since we didn't write KE
# as:
# KE = 1/S(2)*m*v(t)**2
print eom1
# If we write KE as:
# KE = 1/S(2)*m*v(t)**2
# Then, diff(L, v) == 0, and diff(L, v(t)) doesn't work because diff can only
# differentiate w.r.t. to a symbol
# Alternatively, we can make us of the __call__ method of Symbol to make x a
# function of t
v = diff(x(t), t)
# Kinetic and Potential energies
KE = 1/S(2)*m*v**2
PE = m*g*x(t)
# Lagrangian
L = KE - PE
# But this won't work because diff can't differentiate w.r.t to a Symbol
eom1 = Eq(diff(L, x(t)) - diff(diff(L, v), t), 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment