Python Tutorial for Mass-Spring Systems

Open In Colab


This notebook demonstrates the use of the mass_spring Python 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.
# If not already installed, then will install and ask to restart runtime
!pip install matplotlib==3.5.2

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:

  1. Import the damped_harmonic_oscillator() function from the mass_spring module.
    • You will need run import the function only one time in each active session.

  2. Run the default animation with damped_harmonic_oscillator().
  3. Wait a few seconds for the animation to load.
  4. 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 is m=0.2.
  • The friction coefficient is b. The default value is b=0.1.
  • The stiffness coefficient is k. The default value is k=1.
  • The initial position and velocity of the mass is a vector x0. The default value is x0=[-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 is A=0 which means no forcing.
  • The frequency is omega. The default value is omega=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 is tf=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.

damped_harmonic_oscillator(m=0.1,  # mass
                           b=1,  # friction
                           k=1,  # stiffness
                           A=2,  # amplitude of forcing
                           omega=2,  # frequency of forcing
                           x0=[0.5, -1],  # initial pos, initial velocity
                           fps=5,  # frames played per second
                           tf=40)  # total time length

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:

  1. Import the damped_harmonic_oscillator_comp() function from the mass_spring module.
    • You will need run import the function only one time in each active session.

  2. Run the default animation with damped_harmonic_oscillator_comp().
  3. Wait a few seconds for the animation to load.
  4. 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 is m=[0.2, 0.4].
  • The friction coefficients are entered in vector m=[m1, m2]. The default value is m=[0.2, 0.4].
  • The stiffness coefficient are entered in vector m=[m1, m2]. The default value is m=[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 is A=[0, 0] which means no forcing in either system.
  • The frequency is a vector omega=[omega1, omega2]. The default value is omega=[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 is tf=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.

damped_harmonic_oscillator_comp(m=[0.2, 0.3],  # masses
                                b=[0.5, 0.1],  # frictions
                                k=[1, 2],  # stiffnesses  
                                A=[0, 0],  # Amplitudes of forcing
                                omega=[1, 1],  # Frequencies of forcing
                                x0=[[0.5, 1], [-0.5, -1]],  # initial conditions
                                fps=4,  # frames per second
                                tf=40)  # total time

Creative Commons License Information


Creative Commons License
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.