dynamics and IPython notebook

164 views
Skip to first unread message

Rathmann

unread,
Aug 14, 2014, 1:13:21 AM8/14/14
to sy...@googlegroups.com
Hello,

I have been watching the lectures of Susskind's "Theoretical Minimum" course, and using Sympy with IPython notebook to take notes, and work through some of the examples.

Sympy is serious overkill for this purpose, but overall it has been working well.

A couple of questions:
  • What is the best way to deal with dynamics variables and the dot convention for printing? (In physics, the first time derivative of x is often written as \dot{x} instead of dx/dt.)  Is there an easy way to get IPython notebook to print dynamics variables using the dot convention, and still give the nice LaTeX-rendered equations?  If I use vprint (from physics.vector), I get the variables  with primes, but just a text rendering of the equations.
  • I notice sympy.physics.mechanics.LagrangesMethod and sympy.calculus.euler.euler_equations both implement Lagrangian mechanics. Is one of these more "official"  than the other?  Both seem to work for the very simple examples I have tried.
Thanks

Jason Moore

unread,
Aug 14, 2014, 1:23:38 AM8/14/14
to sy...@googlegroups.com
You can subclass a printer and have it do what you want. You can see here:

https://github.com/sympy/sympy/blob/master/sympy/physics/vector/printing.py#L145

where we subclass the latex printer and get the \dot{} notation for derivatives, for example. There is also an example here:

http://docs.sympy.org/dev/modules/printing.html

of subclassing to do custom derivative printing. Maybe exactly what you want.

The LagrangesMethod in sympy.physics.mechanics works with the classes available in that package (RigidBody, ReferemceFrame, etc). The other one is more basic math. So if you want to write all the math yourself then maybe the later is preferable, but if you want to use the objects in sympy.physics.mechanics to build up a rigid body system and find the equations of motion, the use the former.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/fe1737e3-3b19-40e5-8983-5d64bfad8e2f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jason Moore

unread,
Aug 14, 2014, 1:24:47 AM8/14/14
to sy...@googlegroups.com
Oh, you should use

sympy.physics.vector.init_printing()

If you want the dot notation in latex in your notebooks.

Tim Lahey

unread,
Aug 14, 2014, 2:01:55 AM8/14/14
to sy...@googlegroups.com
Hi,

Since Euler-Lagrange equations are useful outside of physics, that's why there's a method in the calculus module. But, like Jason said, you'll probably want to use the one in the physics module to take advantage of the other features.

Cheers,

Tim.

Ondřej Čertík

unread,
Aug 14, 2014, 12:47:02 PM8/14/14
to sympy
Awesome. Share it with us if you want, I would be interested to look
at your notes.

Ondrej

Rathmann

unread,
Aug 14, 2014, 2:09:08 PM8/14/14
to sy...@googlegroups.com
Thanks a lot!  It turned out that init_vprinting() gives the behavior I wanted.

In case others want to go down this path, I am appending a minimal notebook entry which shows the syntax.  I am really impressed that diff(L, xdot) just works, and that I can use solve to get algebraic expressions for velocities and accelerations.

One additional question - is there a better way to get the time variable than  dynamicsymbols._t ?  I look at that leading underscore as an indication that I shouldn't be touching it from user code.

------------------

In [1]:
from sympy.physics.vector import dynamicsymbols
from sympy import diff, symbols, S
from sympy.physics.vector import init_vprinting
 
init_vprinting
()
 
# Simplest test case .. particle of mass m in uniform gravitational field g
x
= dynamicsymbols('x')
t
= dynamicsymbols._t
xdot
= diff(x,t)
m
, g = symbols('m g')
 
T
= (m * xdot**2)/S(2) #kinetic energy
V
= m*g*x # potential energy is just mgh
L
= T - V
 
#Euler-Lagrange equation
diff
(L, xdot,t) - diff(L,x)

Out[1]:
gm+mx¨

Rathmann

unread,
Aug 14, 2014, 2:12:16 PM8/14/14
to sy...@googlegroups.com
Oops - except the m \ddot{x} formats properly in the notebook.

Jason Moore

unread,
Aug 14, 2014, 2:15:23 PM8/14/14
to sy...@googlegroups.com
The dynamicsymbols._t is just a symbols('t') variable declaration. So

dynamicsymbols._t is symbols('t') gives True

dynamicsymbols is just a light wrapper to Symbol Function that gives functions of the ._t variable.

Rathmann

unread,
Aug 26, 2014, 2:24:04 PM8/26/14
to sy...@googlegroups.com
Thanks to Jason, Tim, and Ondřej for their helpful replies.   In case anyone finds it interesting or useful, I am including an example I have worked on.   

It includes the notebook, an svg file for one diagram, and as well as a pdf version for anyone who just wants to look at the output.

The notebook itself is more engaging, since you can play with it.  

I would appreciate any comments/corrections. 
wedge_lagrange.svg
Lag3.pdf
Lag3.ipynb
Reply all
Reply to author
Forward
0 new messages