Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/notebooks/pendulum_sympy.ipynb
Views: 531
Modeling and Simulation in Python
Copyright 2017 Allen Downey
This notebook uses SymPy to derive the equations of motion for a springy pendulum and a rigid pendulum.
Here are the symbols we need:
We'll use vectors P, V, and A to represent position, velocity and acceleration. The Vector class in modsim.py doesn't play nicely with SymPy, so I'll use NumPy arrays instead:
The only vector operations we need are mag
and hat
:
For convenience, I'll define intermediate variables like length:
f_spring
is the force on the particle due to the spring
xhat and yhat are unit vectors along the x and y axes:
Now I can write force due to gravity as a Vector
To write , I'll define the left-hand side and right-hand sides of the equations separately.
Now I can make two equations, one for each component of F and A:
Now I want equations that are explicit in ax and ay. In this case I can get them easily by dividing through by m. But for the rigid pendulum we will need to use solve, so I want to demonstrate that here.
Now we can extract the expressions for ax and ay
And we can get SymPy to format the result for LaTeX:
Or generate Python code we can paste into a slope function:
To see these equations run, see pendulum.ipynb
Rigid pendulum
Solving the rigid pendulum is almost the same, except we need a third equation to represent the geometric constraint. The simplest form of the constraint is:
But this equation doesn't involve vx, vy, ax, and ay, so it's not much help. However, if we take the time derivative of both sides, we get
And if we take the time derivative one more time (and divide through by 2), we get
And that's just what we need.
Now we can represent the force due to tension as a vector with unknown magnitude, R, and direction opposite P.
Again, we can write
And make one equation for each dimension:
Now we have three equations in three unknowns:
And we can get explicit expressions for ax and ay
Again, we can get the results in LaTeX or Python.
To see these equations run, see pendulum2.ipynb