3.8. A quarter car suspension#
3.8.1. Question:#
Simulate the time response of the quarter car suspension model equations descibed below.
3.8.2. Mathematical model:#
3.8.3. Numerical solution:#
First load the necessary libraries into Python:
The SciPy library is one of the core packages that make up the SciPy stack. It provides many user-friendly and efficient numerical routines, such as routines for numerical integration, interpolation, optimization, linear algebra, and statistics. The signal processing toolbox currently contains some filtering functions, a limited set of filter design tools, and a few B-spline interpolation algorithms for 1- and 2-D data.
NumPy is a python library used for working with arrays.
3.8.4. Solution: Transfer function model: #
3.8.4.1. Write set of ODE’s as a state space model:#
We can rewrite the previously describe model as a set of Ordinary Differential Equations (ODE’s) by substituting \(\dot x\) with \(v_x\) and \(\dot y\) with \(v_y\)
if we define a state vector \(X = \begin{bmatrix} x \\ y \\ v_y \\ v_y \end{bmatrix}\) and \(U = \begin{bmatrix} r \end{bmatrix}\) then we can rewrite the previous equation as follows
with \(A = \begin{bmatrix} a^1_1& \dots& a^1_4 \\ \vdots & \vdots &\vdots \\ a^4_1& \dots& a^4_4 \end{bmatrix} = \begin{bmatrix} 0&0&1&0 \\ 0&0&0&1\\ \frac{-k_s-k_w}{m_1} & \frac{k_s}{m_1} & \frac{-b}{m_1} & \frac{b}{m_1} \\ \frac{k_s}{m_2} & \frac{-k_s}{m_2} & \frac{b}{m_2} & \frac{-b}{m_2} \end{bmatrix}\) and \(B = \begin{bmatrix} b^1_1\\ \vdots\\ b^4_1 \end{bmatrix}= \begin{bmatrix}0\\0\\ \frac{k_w}{m_1} \\ 0 \end{bmatrix}\)
3.8.4.1.1. Determine the transfer function models from the state space model:#
Based on the equation above we can now specify a transfer function between a single input (in the example below we choose our only input U=r) and a single output. This is conceptually done by eliminating all other states from previous equation.
First we convert the set of ordinary differential equations in \ref{eq:Lin_model} to the Laplace domain resulting in following set of equations:
Secondly we convert this set of equations to a matrix notation,
We can rewrite this equation where
resulting in
We can now solve this set of equation using basic matrix operations:
and because U is single input signal we can divide the left part by U to obtain the different transfer functions
3.8.5. Detailed numerical solution:#
First load the necessary libraries into Python:
The SciPy library is one of the core packages that make up the SciPy stack. It provides many user-friendly and efficient numerical routines, such as routines for numerical integration, interpolation, optimization, linear algebra, and statistics. The signal processing toolbox currently contains some filtering functions, a limited set of filter design tools, and a few B-spline interpolation algorithms for 1- and 2-D data.
NumPy is a python library used for working with arrays.
SymPy is a Python library for symbolic mathematics.
CAS_lib contains custom made functions that are required by this course
import psutil
L = psutil.Process().parent().cmdline()
if any("jupyter-notebook" in string for string in L):
%matplotlib notebook
elif any("jupyter-labhub" in string for string in L):
%matplotlib widget
else:
%matplotlib inline
from scipy import signal
import numpy as np
from sympy import *
from CAS_lib import minreal
We assume following parameter values: \(m=[m_1,m_2]^T\) with \(m_1=20\) kg ,\(m_2=375\) kg together with \(b=9800\) N/(m/s) and the spring constants \(K_w=1e6=1000000\), \(K_s=1.3*e5=130000\):
m1=20 #kg
m2=375 #kg
m = [m1,m2]
b=9800 #N/(m/s)
kw=1e6 #N/m
ks=1.3e5 #N/m
Equation (1) derived above specifies a linear relation \(\dot X = AX+BU\) with \(A = \begin{bmatrix} 0&0&1&0 \\ 0&0&0&1\\ \frac{-k_s-k_w}{m_1} & \frac{k_s}{m_1} & \frac{-b}{m_1} & \frac{b}{m_1} \\ \frac{k_s}{m_2} & \frac{-k_s}{m_2} & \frac{b}{m_2} & \frac{-b}{m_2} \end{bmatrix}\) and \(B =\begin{bmatrix}0\\0\\ \frac{k_w}{m_1} \\ 0 \end{bmatrix}\)
A = Matrix([[0,0,1,0 ], [0,0,0,1],[(-ks-kw)/m1, ks/m1, -b/m1, b/m1],[ks/m2,-ks/m2,b/m2,-b/m2]])
B = Matrix([[0],[0],[kw/m1],[0]])
Create different transfer function models based on the previous result (\(\dot X = A X + BU\)) with as input r(t) using equation (1) we can derive a transfer function if we assume \(x(0) = y(0) =v_x(0) = v_y(0) =0\) using the following set of equations:
We can now solve this set of equation using matrix operations:
s = symbols('s')
TFs =(s*eye(4)-A).inv()*B
n, d = TFs[0].as_numer_denom() # Take the first element in the list: X(s)/U(s)
num = Poly(n).all_coeffs() #collect the coefficients of the polynomial in s
den = Poly(d).all_coeffs() #collect the coefficients of the polynomial in s
num = [float(item) for item in num] #convert all elements in the list to floating points
den = [float(item) for item in den] #convert all elements in the list to floating points
H1 = signal.TransferFunction(num,den) #Create the transfer function
H1=minreal(H1) #Minimal realization or pole-zero cancellation
n, d = TFs[1].as_numer_denom() # Take the second element in the list: Y(s)/U(s)
num = Poly(n).all_coeffs() #collect the coefficients of the polynomial in s
den = Poly(d).all_coeffs() #collect the coefficients of the polynomial in s
num = [float(item) for item in num] #convert all elements in the list to floating points
den = [float(item) for item in den] #convert all elements in the list to floating points
H2 = signal.TransferFunction(num,den) #Create the transfer function
H2=minreal(H2) #Minimal realization or pole-zero cancellation
Simulate the response to a step input of 0.2 after 1 second:
T=np.linspace(0,2,100)
y0=[0,0,0,0] #Initial values for the states
a= 0*np.ones(49) # 1 second zero values for input
b= 0.2*np.ones(51) # 1 second 0.2 values for input
U = np.concatenate((a, b), axis=0) # combine both vectors a and b
We want to simulate the response of the H1 and H2 to the specified input (u) assuming zero initial values for both position and velocity. We will use the lsim function by the SciPy library :
T, yout, xout = signal.lsim(system, U, T, X0=None, interp=True)
- system: an instance of the LTI class or a tuple describing the system.
- u (array-like, optional) –
An input array describing the input at each time T (interpolation is assumed between given times). If there are multiple inputs, then each column of the rank-2 array represents an input. If u = 0 or None, a zero input is used.
- T (array-like) – The time steps at which the input is defined and at which the output is desired. Must be nonnegative, increasing, and equally spaced.
- x0 (array-like, optional) – Initial condition (default = 0) where the number of states is equal to the number of poles in the transfer function.
- interp (bool, optional) – Whether to use linear (True, the default) or zero-order-hold (False) interpolation for the input array.
- T (1D ndarray) – Time values of the output.
- yout (1D ndarray) – Response of the system.
- xout (ndarray) – Time evolution of the state vector.
T1,yout1, vout1 = signal.lsim(H1, U, T, y0)
T2,yout2, vout2 = signal.lsim(H2, U, T, y0)
import matplotlib.pyplot as plt # Load the pyplot object from the matplotlib library
fig, axes = plt.subplots(nrows=1, ncols=2)
ax0, ax1 = axes.flatten()
ax0.plot(T1, yout1, 'b', label='x') # plot the time signal and create a label for the legend
ax0.legend(loc='best') # position the legend at a minimal overlapping position
ax0.set(xlabel='t[s]', ylabel='Position mass 1 [m]') # label the y-axis
ax0.grid() # plot a grid
ax1.plot(T2, yout2, 'b', label='y') # plot the time signal and create a label for the legend
ax1.legend(loc='best') # position the legend at a minimal overlapping position
ax1.set(xlabel='t[s]', ylabel='Position mass 2 [m]') # label the y-axis
ax1.grid() # plot a grid
fig.tight_layout()
plt.show() # show the plot