2.6. A quarter car suspension#
2.6.1. Question:#
Simulate the time response of the quarter car suspension model equations descibed below.
2.6.2. Mathematical model:#
2.6.3. Solution: Numerical ODE-solver simulation: #
2.6.3.1. Generic solution:#
The Python programming languagecan be used for numerical computing. This implies that every differential equation is solved by performing a numerical approximation instead of specifying a symbolic or algebraic solution (like in for example Maple). This way one is able to solve a set of non-linear differential equations that are impossible to solve with a symbolic solver.
A widely used numerical differential equation solver is solve_ivp that is available in the open source scipy Python library. solve_ivp can be used to solve a system of first order Ordinary Differential Equations (ODEs) \(\frac{d\mathbf{y}}{dt} = \mathbf{f}(t, \mathbf{y})\) when the initial value \(\mathbf{y}_0\) are provided. The first order ODE model is determined by
were t is a 1-dimensional independent time variable , y(t) is an N-dimensional vector-valued function (state), and an N-dimensional vector-valued function f(t, y(t)). The goal is to find y(t) approximately satisfying the differential equations, given an initial value \(\mathbf{y}(t_0)=\mathbf{y}_0\).
The solve_ivp function can be used in Python by executing following command:
scipy.integrate.solve_ivp(ODEFUN,[T0, TFINAL] , \(\mathbf{y}_0\), method=‘RK45’, t_eval=None, dense_output=False, events=None, vectorized=False, args=None, options)
ODEFUN is a function handle that specifies the system of differential equations \(\frac{d\mathbf{y}}{dt} = \mathbf{f}(t, \mathbf{y})\). solve_ivp integrates this system of differential equations \(\frac{d\mathbf{y}}{dt} = \mathbf{f}(t, \mathbf{y})\) from time T0 to TFINAL with initial conditions \(\mathbf{y}_0\). All other inputs are optional. More information about this command can be found here.
(Note that one can also use the scipy.integrate.ode, i.e. a generic interface class to numeric integrators)
2.6.3.2. Detailed solution:#
First load the necessary libraries into Python:
the solve_ivp function that numerically integrates a system of first order ODEs given an initial value,
the numpy library adding support for large, multi-dimensional array
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.integrate import solve_ivp
import numpy as np
from sympy import *
Next we you need to declare the ODEFUN function to make it usable for your numerical solver.
The differential equation for the predefined model is
we can rewrite this equation as a set of first order ODE by specifying
the above defined set of differential equations can be written as ODE equations
when we take the output vector \(y(t) = [ x(t), y(t), v_x(t) , v_y(t)]^T\) and u(t)=r(t). We take the input signal u=r for the bump equal to 0 [m] for 1 seconden and afterworths equal to 0.2 [m]. The set of ordinary differential equations in the ODEFUN then becomes:
def ODEFUN(t, y, m, b, kw, ks ):
if t<1:
u=0
else:
u=0.2
m1=m[0];
m2=m[1];
dx = y[2] # third element of y(t) => vx
dy = y[3] # fourth element of y(t) => vy
dvx= kw/m1*u-b/m1*(dx-dy)-ks/m1*(y[0]-y[1])-kw/m1*y[0]
dvy= -b/m2*(dy-dx)-ks/m2*(y[1]-y[0])
return [dx,dy,dvx,dvy]
where t represent the time, y the time varying state vector \(y=[ x(t), y(t), v_x(t) , v_y(t)]^T\) and u is the input signal r. The mass vector m consisting of \(m_1\) and \(m_2\), the viscous friction coefficient b,the spring constant for the wheel and suspension \(k_w\) and \(K_s\) are defined as parameter (not time-dependent!).
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
For initial conditions, we assume the initial positions and velocities are equalt oto zero \(y(0)= [ 0, 0, 0 , 0]^T\). Then the vector of initial conditions equals
y0=[0,0,0,0]
We will generate a solutions in the interval 0 <= t <= 5. Therefore we will specify a time vector array:
TSPAN = [0,5]
To solve the first order ODE model we need to call the solve_ivp function with as an input the previously specifed ODEFUN,TSPAN and the initial values for the y. The parameters m,b are handed over using the args argument (be aware the args option is only implemented within Scipy version >1.4! Update this version if necessary)
sol = solve_ivp(ODEFUN, TSPAN, y0, args=(m,b,kw,ks), dense_output=True)
The optional input dense_output=True make the integrator interpolate between internal time steps using the polynomial approximation constructed by the integrator. So when you query the output from the integrator (as it generates it in time) between internal time steps, what it is really doing is interpolating between these time steps to get the output.
The returned output y is a bunch object of the ODEResult type with the following fields defined:
t: ndarray, shape (n_points,)
Time points.y: ndarray, shape (n, n_points)
Values of the solution at t.sol: OdeSolution or None
Found solution as OdeSolution instance; None if dense_output was set to False.
We can select 1000 discrete time steps between 0 and 2
t = np.linspace(0, 2, 1000)
We select the simulated positions (x,y) and velocities (vx,vy) for the outputs for mass 1 and 2 of the solver for the previous defined discrete time steps:
x = sol.sol(t)[0,:] # Select the third output vector = x
y = sol.sol(t)[1,:] # Select the third output vector = y_
vx = sol.sol(t)[2,:] # Select the third output vector = v_x
vy = sol.sol(t)[3,:] # Select the third output vector = v_y
Finally we plot the resulted simulated positions (x,y) as a function of time (t)
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(t, x, 'b', label='Car position') # 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='Car position [m]') # label the y-axis
ax0.grid() # plot a grid
ax1.plot(t, y, 'b', label='Car speed') # 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='Car spped [m]') # label the y-axis
ax1.grid() # plot a grid
fig.tight_layout()
plt.show() # show the plot