[Link]
blog
Mass-Spring-Damper
System with Python
Hans-Petter Halvorsen
Free Textbook with lots of Practical Examples
[Link]
Additional Python Resources
[Link]
Contents
• Mass-Spring-Damper System
• Simulations:
–SciPy ODE Solvers
–State-space Model
–Discrete System
Mass-Spring-Damper System
The ”Mass-Spring-Damper“ System is
Spring typical system used to demonstrate and
𝑘 illustrate Modelling and Simulation
𝐹(𝑡) Applications
Mass
𝑚
𝑥(𝑡)
𝑐 Damper
Mass-Spring-Damper System
Given a so-called "Mass-Spring-Damper" system Newtons [Link]: ∑ 𝐹 = 𝑚𝑎
The system can be described by the following equation:
𝐹 𝑡 − 𝑐 𝑥̇ 𝑡 − 𝑘𝑥 𝑡 = 𝑚𝑥(𝑡)
̈
Where 𝑡 is the time, 𝐹(𝑡) is an external force applied to the system, 𝑐 is the
damping constant, 𝑘 is the stiffness of the spring, 𝑚 is a mass.
𝑥(𝑡) is the position of the object (𝑚)
𝑥̇ 𝑡 is the first derivative of the position, which equals the velocity/speed
of the object (𝑚)
𝑥(𝑡)
̈ is the second derivative of the position, which equals the acceleration
of the object (𝑚)
Mass-Spring-Damper System
𝐹 𝑡 − 𝑐𝑥̇ 𝑡 − 𝑘𝑥 𝑡 = 𝑚𝑥(𝑡)
̈
Higher order differential equations can typically
𝑚𝑥̈ = 𝐹 − 𝑐𝑥̇ − 𝑘𝑥 be reformulated into a system of first order
differential equations
1
𝑥̈ = 𝐹 − 𝑐𝑥̇ − 𝑘𝑥
𝑚
𝑥! = Position
We set This gives:
𝑥" = Velocity/Speed
𝑥 = 𝑥! 𝑥̇ ! = 𝑥"
! !
𝑥̇ = 𝑥" 𝑥̇ " = 𝑥=
̈ 𝐹 − 𝑐𝑥̇ − 𝑘𝑥 = 𝐹 − 𝑐𝑥" − 𝑘𝑥!
# #
Finally:
1 𝑥̇ - = 𝑥.
𝑥̈ = 𝐹 − 𝑐 𝑥̇ − 𝑘𝑥 -
𝑚 𝑥̇ . = / 𝐹 − 𝑐𝑥. − 𝑘𝑥-
[Link]
SciPy ODE Solver
Hans-Petter Halvorsen
SciPy
• SciPy is a free and open-source Python library
used for scientific computing and engineering
• SciPy contains modules for optimization, linear
algebra, interpolation, image processing, ODE
solvers, etc.
• SciPy is included in the Anaconda distribution
Python Code
import numpy as np
from [Link] import odeint
import [Link] as plt
# Initialization
tstart = 0
Using SciPy ODE Solver tstop = 60
increment = 0.1
𝑥̇ - = 𝑥. # Initial condition
x_init = [0,0]
-
𝑥̇ . = / 𝐹 − 𝑐𝑥. − 𝑘𝑥-
t = [Link](tstart,tstop+1,increment)
# Function that returns dx/dt
def mydiff(x, t):
𝑥! = Position c = 4 # Damping constant
k = 2 # Stiffness of the spring
𝑥" = Velocity/Speed
m = 20 # Mass
F = 5
dx1dt = x[1]
dx2dt = (F - c*x[1] - k*x[0])/m
dxdt = [dx1dt, dx2dt]
return dxdt
# Solve ODE
x = odeint(mydiff, x_init, t)
x1 = x[:,0]
x2 = x[:,1]
# Plot the Results
[Link](t,x1)
[Link](t,x2)
[Link]('Simulation of Mass-Spring-Damper System')
[Link]('t')
[Link]('x(t)')
[Link](["x1", "x2"])
[Link]()
[Link]()
[Link]
State-space Model
Hans-Petter Halvorsen
State-space Model
𝑥̇ - = 𝑥.
- 0 1 𝑥- 0
𝑥̇ . = / 𝐹 − 𝑐𝑥. − 𝑘𝑥- 𝑥̇ -
= 𝑘 𝑐
𝑥 + 1 𝐹
𝑥̇ . − − .
𝑚 𝑚 𝑚
𝑥̇ - = 𝑥.
0 1 -
𝑥̇ . = − / 𝑥- − / 𝑥.+ / 𝐹
𝑥̇ = 𝐴𝑥 + 𝐵𝑢
0 1 0 𝑥!
𝐴= 𝑘 𝑐 𝐵= 1 𝑥= 𝑥
− − "
𝑚 𝑚 𝑚
Python Control Systems Library
• The Python Control Systems Library (control) is a
Python package that implements basic operations
for analysis and design of feedback control systems.
• Existing MATLAB user? The functions and the
features are very similar to the MATLAB Control
Systems Toolbox.
• Python Control Systems Library Homepage:
[Link]
• Python Control Systems Library Documentation:
[Link]
Installation
The Python Control Systems Library
package may be installed using pip:
pip install control
• PIP is a Package Manager for Python
packages/modules.
• You find more information here:
[Link]
• Search for “control“.
• The Python Package Index (PyPI) is a
repository of Python packages where
you use PIP in order to install them
Python Code
import numpy as np
import [Link] as plt
import control
# Parameters defining the system
State-space Model c = 4 # Damping constant
k = 2 # Stiffness of the spring
0 1 𝑥! 0 m = 20 # Mass
𝑥̇ ! 𝑘 𝑐 + 1 𝐹
F = 5 # Force
= 𝑥
𝑥̇ " − − "
𝑚 𝑚 𝑚 # Simulation Parameters
tstart = 0
𝑥! tstop = 60
𝑦= 1 0 𝑥 increment = 0.1
" t = [Link](tstart,tstop+1,increment)
# System matrices
A = [[0, 1], [-k/m, -c/m]]
B = [[0], [1/m]]
C = [[1, 0]]
sys = [Link](A, B, C, 0)
# Step response for the system
t, y, x = control.forced_response(sys, t, F)
[Link](t, y)
[Link]('Simulation of Mass-Spring-Damper System')
[Link]('t')
[Link]('x(t)')
[Link]()
[Link]()
Python Code
import numpy as np
import [Link] as plt
import control
# Parameters defining the system
State-space Model c
k
= 4 # Damping constant
= 2 # Stiffness of the spring
m = 20 # Mass
0 1 𝑥! 0
𝑥̇ !
F = 5 # Force
= 𝑘 𝑐
𝑥 + 1 𝐹
𝑥̇ " − − "
# Simulation Parameters
𝑚 𝑚 𝑚 tstart = 0
tstop = 60
𝑥! increment = 0.1
𝑦= 1 0 𝑥 t = [Link](tstart,tstop+1,increment)
" # System matrices
A = [[0, 1], [-k/m, -c/m]]
B = [[0], [1/m]]
C = [[1, 0]]
sys = [Link](A, B, C, 0)
# Step response for the system
t, y, x = control.forced_response(sys, t, F)
x1 = x[0 ,:]
x2 = x[1 ,:]
[Link](t, x1, t, x2)
[Link]('Simulation of Mass-Spring-Damper System')
[Link]('t')
[Link]('x(t)')
[Link]()
[Link]()
[Link]
• An alternative to The Python Control Systems Library is
[Link], i.e. the Signal Module in the SciPy Library
• [Link]
SciPy is included with the
Anaconda distribution
Python Code State-space Model
import numpy as np
import [Link] as plt
import [Link] as sig
# Parameters defining the system
c = 4 # Damping constant
k = 2 # Stiffness of the spring
0 1 𝑥! 0
𝑥̇ !
m = 20 # Mass
𝑘 𝑐 + 1 𝐹
F = 5 # Force
= 𝑥 Ft = [Link](610)*F
𝑥̇ " − − "
𝑚 𝑚 𝑚 # Simulation Parameters
tstart = 0
𝑥! tstop = 60
𝑦= 1 0 𝑥 increment = 0.1
" t = [Link](tstart,tstop+1,increment)
# System matrices
A = [[0, 1], [-k/m, -c/m]]
B = [[0], [1/m]]
C = [[1, 0]]
sys = [Link](A, B, C, 0)
# Step response for the system
t, y, x = [Link](sys, Ft, t)
x1 = x[:,0]
x2 = x[:,1]
[Link](t, x1, t, x2)
#[Link](t, y)
[Link]('Simulation of Mass-Spring-Damper System')
[Link]('t')
[Link]('x(t)')
[Link]()
[Link]()
[Link]
Discretization
Hans-Petter Halvorsen
Discretization
Given:
𝑥̇ ! = 𝑥"
! Then we get:
𝑥̇ " = 𝐹 − 𝑐𝑥" − 𝑘𝑥! 𝑥! 𝑘 + 1 = 𝑥! 𝑘 + 𝑇$ 𝑥" 𝑘
#
% & !
𝑥" 𝑘 + 1 = −𝑇$ # 𝑥! 𝑘 + 𝑥" 𝑘 − 𝑇$ # 𝑥" 𝑘 + 𝑇$ # 𝐹(𝑘)
Using Euler:
𝑥 𝑘 + 1 − 𝑥(𝑘)
𝑥̇ ≈ Finally:
𝑇$
𝑥! 𝑘 + 1 = 𝑥! 𝑘 + 𝑇$ 𝑥" 𝑘
Then we get: % & !
𝑥! 𝑘 + 1 − 𝑥! 𝑘 𝑥" 𝑘 + 1 = −𝑇$ # 𝑥! 𝑘 + (1 − 𝑇$ #)𝑥" 𝑘 + 𝑇$ # 𝐹(𝑘)
= 𝑥" 𝑘
𝑇$
𝑥" 𝑘 + 1 − 𝑥" 𝑘 1
= 𝐹(𝑘) − 𝑐𝑥" 𝑘 − 𝑘𝑥! 𝑘
𝑇$ 𝑚
This gives:
𝑥! 𝑘 + 1 = 𝑥! 𝑘 + 𝑇$ 𝑥" 𝑘
1
𝑥" 𝑘 + 1 = 𝑥" 𝑘 + 𝑇$ 𝐹(𝑘) − 𝑐𝑥" 𝑘 − 𝑘𝑥! 𝑘
𝑚
Discrete State-space Model
Discrete System:
1 𝑇#
𝑥! 𝑘 + 1 = 𝑥! 𝑘 + 𝑇$ 𝑥" 𝑘
% & ! 𝐴= 𝑘 𝑐
𝑥" 𝑘 + 1 = −𝑇$ 𝑥! 𝑘 + (1 − 𝑇$ )𝑥" 𝑘 + 𝑇$ 𝐹(𝑘) −𝑇# 1 − 𝑇#
# # # 𝑚 𝑚
We can set it on Discrete state space form:
0
𝑥(𝑘 + 1) = 𝐴' 𝑥(𝑘) + 𝐵' 𝑢(𝑘)
𝐵= 1
𝑇#
This gives: 𝑚
1 𝑇$ 0
𝑥! 𝑘 + 1 𝑥! 𝑘 1 𝐹(𝑘) 𝑥! 𝑘
= 𝑘 𝑐 + 𝑥(𝑘) =
𝑥" 𝑘 + 1 −𝑇$ 1 − 𝑇$ 𝑥" 𝑘 𝑇$ 𝑥" 𝑘
𝑚 𝑚 𝑚
We can also use control.c2d() function
Python Code
# Simulation of Mass-Spring-Damper System
import numpy as np
import [Link] as plt
# Model Parameters
c = 4 # Damping constant
k = 2 # Stiffness of the spring
Discrete System m
F
= 20 # Mass
= 5 # Force
𝑥! 𝑘 + 1 = 𝑥! 𝑘 + 𝑇$ 𝑥" 𝑘
# Simulation Parameters
Ts = 0.1
Tstart = 0
% & ! Tstop = 60
𝑥" 𝑘 + 1 = −𝑇$ 𝑥! 𝑘 + (1 − 𝑇$ )𝑥" 𝑘 + 𝑇$ 𝐹(𝑘) N = int((Tstop-Tstart)/Ts) # Simulation length
# # # x1 = [Link](N+2)
x2 = [Link](N+2)
x1[0] = 0 # Initial Position
x2[0] = 0 # Initial Speed
𝑥! = Position a11 = 1
a12 = Ts
𝑥" = Velocity/Speed a21 = -(Ts*k)/m
a22 = 1 - (Ts*c)/m
b1 = 0
b2 = Ts/m
# Simulation
for k in range(N+1):
x1[k+1] = a11 * x1[k] + a12 * x2[k] + b1 * F
x2[k+1] = a21 * x1[k] + a22 * x2[k] + b2 * F
# Plot the Simulation Results
t = [Link](Tstart,Tstop+2*Ts,Ts)
#[Link](t, x1, t, x2)
[Link](t,x1)
[Link](t,x2)
[Link]('Simulation of Mass-Spring-Damper System')
[Link]('t [s]')
[Link]('x(t)')
[Link]()
[Link](["x1", "x2"])
[Link]()
Additional Python Resources
[Link]
Hans-Petter Halvorsen
University of South-Eastern Norway
[Link]
E-mail: [Link]@[Link]
Web: [Link]