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.ipynb
Views: 531
Modeling and Simulation in Python
Chapter 10 Example: Springy Pendulum
Copyright 2017 Allen Downey
Pendulum
This notebook solves the Spider-Man problem from spiderman.ipynb, demonstrating a different development process for physical simulations.
In pendulum_sympy, we derive the equations of motion for a springy pendulum without drag, yielding:
We'll use the same conditions we saw in spiderman.ipynb
Now here's a version of make_system
that takes a Condition
object as a parameter.
make_system
uses the given value of v_term
to compute the drag coefficient C_d
.
Let's make a System
To write the slope function, we can get the expressions for ax
and ay
directly from SymPy and plug them in.
As always, let's test the slope function with the initial conditions.
And then run the simulation.
Visualizing the results
We can extract the x and y components as Series
objects.
The simplest way to visualize the results is to plot x and y as functions of time.
We can plot the velocities the same way.
Another way to visualize the results is to plot y versus x. The result is the trajectory through the plane of motion.
We can also animate the trajectory. If there's an error in the simulation, we can sometimes spot it by looking at animations.
Here's a function that encapsulates that code and runs the animation in (approximately) real time.