2.7. Combustion engine cruise control#
2.7.1. Mathematical model:#
2.7.2. Question:#
Simulate the time response of the combustion engine cruise control model equations descibed below.
2.7.3. Solution: Numerical ODE-solver simulation: #
2.7.3.1. Generic solution:#
he Python programming language can 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.7.3.2. Detailed numerical solution:#
First load the necessary libraries into Python:
the solve_ivp function that numerically integrates a system of ordinary differential equations given an initial value,
the numpy library adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays
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
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 also want to know the position of the car therefore we add an extra ODE equation
when we take the output vector \(y(t) = [ x(t), \nu(t)]^T\) and \(u^*\)(t)=\([ \theta(t) , u(t)] \) then we can specify the differential equation using an ODEFUN in Python:
def ODEFUN(t, y, n, r, beta, omega_n, m , Cr, rho, Cd, A, Tm ):
u=1 # we apply full throttle
theta= 4*np.pi/180# 4 degrees of steep
omega= n/r*y[1]
dx = y[1] # v is second element of y(t)
dv = 1/m*(n*u/r*Tm*(1-beta*(omega/omega_n-1)**2) - m*g*np.sin(theta) - m*g*Cr*np.sign(y[1]) - 0.5*rho*Cd*A*y[1]**2)
return [dx,dv]
where t represent the time, y the time varying state vector y(t)=[x(t),\(\nu(t)\)]T and \(u^*\) is the input vector signal. The gear ratio n, wheel radius r, motor parameters \(\beta\) aand \(\omega_n\), the vehicle mass m, rol friction coefficient \(C_r\), density of air \(\rho\), the frontal surface A and the engine torque constant \(T_m\) are defined as parameter (not time-dependent!).
We assume following parameter values:
n=10 #Gear ratio
r=0.5 #Wheel radius [m]
Tm=190 #engine torque constant [Nm]
omega_n=420 #peak torque rate [rad/sec]
m=1000 #mass of the car [kg]
beta = 0.4 #Torque coefficient
Cr=0.01 #roll friction coefficient
rho=1.3 #density of air [kgm^3]
Cd=0.32 #drag coefficient
A=2.4 #Frontal surface car [m^2]
g=9.8 #Gravity constant [N/kg]
For initial conditions, we assume the initial positions and velocities are equal to zero \(y(0)= [ 0, 0 ]^T\). Then the vector of initial conditions equals
y0=[0,0]
We will generate a solutions in the interval 0 <= t <= 40.
Therefore we will specify a time vector array:
TSPAN = [0,50]
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=( n, r, beta, omega_n, m , Cr, rho, Cd, A, Tm ), 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 40 seconds
t = np.linspace(0, 40, 1000)
We select the simulated velocity output for mass 1 of the solver for the previous defined discrete time steps:
v = sol.sol(t)[1,:] #Select the second number in the output vector = v (velocity)
Finally we plot the resulted simulated velocities (v) as a function of time (t)
import matplotlib.pyplot as plt # Load the pyplot object from the matplotlib library
ax = plt.subplot()
ax.plot(t, v.T, 'b', label='Combustion cruise control simulation') # plot the time signal and create a label for the legend
ax.legend(loc='best') # position the legend at a minimal overlapping position
ax.set(xlabel='t[s]', ylabel='Velocity of the car = v [m/s]') # label the y-axis
ax.grid() # plot a grid
plt.show() # show the plot