# If not already installed, then will install and ask to restart runtime
!pip install matplotlib==3.5.2
Python Tutorial for Mass-Spring Systems
This notebook demonstrates the use of the mass_spring
Python module.
- The actual Python code for each function can be found in the file named mass_spring.py located in the directory ../utils.
- Good news! The functions defined in mass_spring.py are coded and ready for use with no mofications needed to the source file!
- You do not even have to view the source file to understand how to use and adjust the functions to fit your needs.
- See the documentation below for a “Quick Reference Guide” to working with functions in the
mass_spring
module.
Section 1: Required Install for Interactive Plots
Each time you want to run one of the mass-spring system animations, you will need to install a newer version of matplotlib
. Each time you open/create a new Jupyter notebook where you want to run these animations you will need to:
- Run the command
!pip install matplotlib==3.5.2
in a code cell. - You only need to do this one time in an active session.
- If you terminate your session or close the file, you will need to rerun the install command each time you establish a new session.
Section 2: Loading mass_spring
from GitHub
- Run the code cell below to load the most up to date modules stored in GitHub.
- You will only need to run this code cell one time during an active session.
!pip install git+https://github.com/CU-Denver-MathStats-OER/ODEs
from IPython.display import clear_output
clear_output()
Section 3: Running a Mass Spring Experiment with damped_harmonic_oscillator
A function named damped_harmonic_oscillator()
runs an animation of a damped, driven harmonic oscillator:
- Damped means friction is present (though you can set it to 0).
- Driven means there is an external forcing function acting on the mass spring system.
Running the Animation with Default Settings
We can run a mass-spring animation with the default settings as follows:
- Import the
damped_harmonic_oscillator()
function from themass_spring
module.- You will need run import the function only one time in each active session.
- You will need run import the function only one time in each active session.
- Run the default animation with
damped_harmonic_oscillator()
. - Wait a few seconds for the animation to load.
- Press the play button to play the animation.
# Step 1 - Only need to do this once per active session
from utils.mass_spring import damped_harmonic_oscillator
damped_harmonic_oscillator()
Customizing the Default Settings
Customizing Mass-Spring Parameters
The variables in damped_harmonic_oscillator()
that determine the mass-spring set up are:
- The mass coefficient is
m
. The default value ism=0.2
. - The friction coefficient is
b
. The default value isb=0.1
. - The stiffness coefficient is
k
. The default value isk=1
. - The initial position and velocity of the mass is a vector
x0
. The default value isx0=[-2, 0]
.- The mass is initially displaced two units the the left of equilibrium.
- The mass is let go with no initial velocity.
Customizing the Driving Function
The variables in damped_harmonic_oscillator()
that determine the driving (or forcing) function are:
- The amplitude is
A
. The default value isA=0
which means no forcing. - The frequency is
omega
. The default value isomega=1
.
Since the default setting is A=0
, the default animation is undriven by default, meaning there is no external forcing function acting on the system.
Customizing the Animation Settings
The variables in damped_harmonic_oscillator()
that determine animation length and speed:
- The option
fps
is how many frames are played per second.- The bigger the value, the faster the animation will go.
- The default is
fps=3
.
- The option
tf
gives the final time when the animation stops. The default istf=30
.
Each time you want to run a new animation
Run the command:
damped_harmonic_oscillator(m=new.m, b=new.b, k=new.k, A=new.A, omega=new.w, x0=[new.s0, new.v0], fps=new.fps, tf=new.tf)
Below we run a customized mass-spring system.
=0.1, # mass
damped_harmonic_oscillator(m=1, # friction
b=1, # stiffness
k=2, # amplitude of forcing
A=2, # frequency of forcing
omega=[0.5, -1], # initial pos, initial velocity
x0=5, # frames played per second
fps=40) # total time length tf
Section 4: Comparing two Different Mass-Spring Systems with damped_harmonic_oscillator_comp
If we want to compare two mass-spring systems running under different conditions, we can use teh damped harmonic oscillator comparison funciton which is named damped_harmonic_oscillator_comp()
.
Running the Comparison Animation with Default Settings
We can run a simultaneous comparison of two mass-spring animations with the default settings as follows:
- Import the
damped_harmonic_oscillator_comp()
function from themass_spring
module.- You will need run import the function only one time in each active session.
- You will need run import the function only one time in each active session.
- Run the default animation with
damped_harmonic_oscillator_comp()
. - Wait a few seconds for the animation to load.
- Press the play button to play the animation.
# Step 1 - Only need to do this once per active session
from utils.mass_spring import damped_harmonic_oscillator_comp
damped_harmonic_oscillator_comp()
Customizing the Default Settings
Customizing Mass-Spring Parameters
The variables in damped_harmonic_oscillator()
that determine the mass-spring set up are:
- The mass coefficients are entered in vector
m=[m1, m2]
. The default value ism=[0.2, 0.4]
. - The friction coefficients are entered in vector
m=[m1, m2]
. The default value ism=[0.2, 0.4]
. - The stiffness coefficient are entered in vector
m=[m1, m2]
. The default value ism=[0.2, 0.4]
. - The initial positions and velocities of both masses are entered in matrix
x0=[[pos.1, vel.1], [pos.2, vel.2]]
.- The first row of the matrix gives the initial position and velocity of the first mass-spring system.
- The second row of the matrix gives the initial position and velocity of the second mass-spring system.
- The default values are
x0=[[-2, 0],[-2,0]]
.
Customizing the Driving Functions
The variables in damped_harmonic_oscillator_comp()
that determine the driving (or forcing) functions for each system are:
- The amplitude is a vector
A=[amp1, amp2]
. The default value isA=[0, 0]
which means no forcing in either system. - The frequency is a vector
omega=[omega1, omega2]
. The default value isomega=[1,1]
.
Since the default setting is A=[0,0]
, the default animations are undriven by default, meaning there is no external forcing function acting on either system.
Customizing the Animation Settings
The variables in damped_harmonic_oscillator()
that determine animation length and speed:
- The option
fps
is how many frames are played per second.- The bigger the value, the faster the animation will go.
- The default is
fps=3
.
- The option
tf
gives the final time when the animation stops. The default istf=30
.
Each time you want to run a new animation
Run the command:
damped_harmonic_oscillator_comp(m=[m1, m2], b=[b1, b2], k=[k1, k2], A=[A1, A2], omega=[w1, w2], x0=[[pos1, vel1], [pos2, vel2]], fps=new.fps, tf=new.tf)
Below we run a customized mass-spring system.
=[0.2, 0.3], # masses
damped_harmonic_oscillator_comp(m=[0.5, 0.1], # frictions
b=[1, 2], # stiffnesses
k=[0, 0], # Amplitudes of forcing
A=[1, 1], # Frequencies of forcing
omega=[[0.5, 1], [-0.5, -1]], # initial conditions
x0=4, # frames per second
fps=40) # total time tf
Creative Commons License Information
Exploring Differential Equations by Adam Spiegler is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Python scripts created by Jonathon Hirschi, Troy Butler, and Adam Spiegler.