3.9. Combustion engine cruise control#

3.9.1. Question:#

Simulate response of the cruise control model equations using a transfer function model in Python.

3.9.2. Mathematical model:#

Combustion engine cruise control model equations

3.9.3. Solution: Transfer function model: #

3.9.3.1. Generic solution:#

Why do we prefer linear systems?

An important and necessary condition for a linear system is superposition. When a system at rest is subjected to an excitation \(x_1(t)\), it exhibits a response \(y_1(t)\) and if it is subjected to an excitation \(x_2(t)\), it provides a corresponding response \(y_2(t)\). For a linear system, it is fundamental that the excitation \(x_1(t)+x_2(t)\) corresponds with a response \(y_1(t)+y_2(t)\). This important characteristic is called superposition.

Furthermore, it is necessary that a magnitude scale factor is preserved by a linear system. Meaning that an input of \(\beta\) times \(x_1(t)\) results in an response of \(\beta\) times the \(y_1(t)\). This is called the property of homogenity.

How do we linearize a system?

The linearity of many mechanical and electrical systems can be assumed over a reasonable large range of the variables. Almost all physical systems become non-linear as the variables are increased without limitations. One can linearize non-linear elements assuming small signal conditions.

Consider a set of non-linear ordinary differential equations (ODE) where \(\dot x_1(t), \ldots, \dot x_n(t)\) are functions of the state variables \(x_1(t), \ldots, x_n(t)\) and input variables \(u_1(t), \ldots, u_m(t)\):

(3.46)#\[\begin{align} \dot x_1(t) &=& g_1(x_1(t), \ldots,x_n(t),u_1(t), \ldots,u_m(t)) \\ \vdots \\ \dot x_n(t) &=& g_n(x_1(t), \ldots,x_n(t),u_1(t), \ldots,u_m(t )) \end{align}\]

where \(g(\ldots)\) represent a generalized function.

To perform a linearization we first need to determine an operation point based on the context. For a controlled system this implies the desired control values and for a simulation this implies the assumed working point of the system.

If we assume an operation point for the state \(x_{1}^0\), \(\ldots\) , \(x_{n}^0\) and for the input signal \(u_{1}^0\), \(\ldots\) , \(u_{m}^0\) , then a Taylor series expansion around this operation point can be specified

(3.47)#\[\begin{align} \dot x_1(t) &= g_1(x_1(t), \ldots,x_n(t),u_1(t), \ldots,u_m(t)) \\ &\approx g_1(x_{1}^0,\ldots,x_{n}^0,u_{1}^0,\ldots,u_{m}^0) + \left. \frac{\partial g_1}{\partial x_1} \right\|_{x=x_{1}^0\ldots,x_{n}^0,u=u_{1}^0, \ldots , u_{m}^0} (x_1(t)-x_{1}^0) + \dots + \left. \frac{\partial g_1}{\partial x_n} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u=u_{1}^0, \ldots , u_{m}^0} (x_n(t)-x_{n}^0) \\ & \ + \left. \frac{\partial g_1}{\partial u_1} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} (x_1(t)-u_{1}^0) + \dots + \left. \frac{\partial g_1}{\partial u_m} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u=u_{1}^0, \ldots , u_{m}^0} (u_m(t)-u_{m}^0) \\ & \vdots \\ \dot x_n(t) &= g_n(x_1(t), \ldots,x_n(t),u_1(t), \ldots,u_m(t)) \\ &\approx g_n(x_{1}^0,\ldots,x_{n}^0,u_{1}^0,\ldots,u_{m}^0) + \left. \frac{\partial g_n}{\partial x_1} \right\|_{x=x_{1}^0\ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} (x_1(t)-x_{1}^0) + \dots + \left. \frac{\partial g_n}{\partial x_n} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u=u_{1}^0, \ldots , u_{m}^0} (x_n(t)-x_{n}^0) \\ & \ + \left. \frac{\partial g_n}{\partial u_1} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u=u_{1}^0, \ldots , u_{m}^0} (x_1(t)-u_{1}^0) + \dots + \left. \frac{\partial g_n}{\partial u_m} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u=u_{1}^0, \ldots , u_{m}^0} (u_m(t)-u_{m}^0) \end{align}\]

When we transform the variables to introduce new state variables:

(3.48)#\[\begin{align} x'_1(t) &= x_1(t)-x_{1}^0\\ &\vdots \\ x'_n(t) &= x_n(t)-x_{n}^0 \end{align}\]

and for the input variables

(3.49)#\[\begin{align} u'_1 &= u_1(t)-u_{1}^0\\ &\vdots \\ u'_m &= u_m(t)-u_{m}^0. \end{align}\]

The linearization point should be achievable therefore when we feed the operation point to our set of differential equations, then a linearization point for the state derivatives can be specified:

(3.50)#\[\begin{align} \dot x'^0_1 &= g_1(x_{1}^0,\ldots,x_{n}^0,u_{1}^0,\ldots,u_{m}^0) \\ &\vdots \\ \dot x'^0_n &= g_n(x_{1}^0,\ldots,x_{n}^0,u_{1}^0,\ldots,u_{m}^0). \end{align}\]

When we then substitute the state derivative by

(3.51)#\[\begin{align} \dot x'_1(t) &= \dot x_1(t)-\dot x'^0_{1}\\ &\vdots \\ \dot x'_n(t) &= \dot x_n(t)-\dot x'^0_{n} \end{align}\]

we obtain a set of linear differential equations without an offset

(3.52)#\[\begin{align} \dot x'_1(t) &\approx \left. \frac{\partial g_1}{\partial x_1} \right\|_{x=x_{1}^0\ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} x'_1(t) + \dots + \left. \frac{\partial g_1}{\partial x_n} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} x'_n(t) \\ & + \left. \frac{\partial g_1}{\partial u_1} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} u'_1(t) + \dots + \left. \frac{\partial g_1}{\partial u_m} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} u'_m(t) \\ &\approx a_1^1 x'_1 + \ldots + a_n^1 x'_{n} + b_1^1 u'_1 + \ldots + b_m^1 u'_{m}\\ & \vdots \\ \dot x'_n(t) &\approx \left. \frac{\partial g_n}{\partial x_1} \right\|_{x=x_{1}^0\ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} x'_1(t) + \dots + \left. \frac{\partial g_n}{\partial x_n} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} x'_n(t) \\ & + \left. \frac{\partial g_n}{\partial u_1} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} u'_1(t) + \dots + \left. \frac{\partial g_n}{\partial u_m} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} u'_m(t) \\ &\approx a_1^n x'_1 + \ldots + a_n^n x'_{n} + b_1^n u'_1 + \ldots + b_m^n u'_{m} \end{align}\]

This set of linear differential equations can now be rewritten using a matrix notation:

(3.53)#\[\begin{align} \begin{bmatrix} \dot x'_1 \\ \vdots \\ \dot x'_n \end{bmatrix} &= \begin{bmatrix} a_1^1 & \dots& a_n^1 \\ \vdots& \vdots& \vdots \\ a_1^n& \dots& a_n^n \end{bmatrix} \begin{bmatrix} \dot x'_1 \\ \vdots \\ \dot x'_n \end{bmatrix} + \begin{bmatrix} b_1^1 & \dots& b_m^1 \\ \vdots& \vdots& \vdots \\ b_1^n& \dots& b_m^n \end{bmatrix} \begin{bmatrix} u'_1 \\ \vdots \\ u'_m \end{bmatrix} \\ &= A \begin{bmatrix} \dot x'_1 \\ \vdots \\ \dot x'_n \end{bmatrix} + B \begin{bmatrix} u'_1 \\ \vdots \\ u'_m \end{bmatrix} \label{eq:Lin_model} \tag{1} \end{align}\]

3.9.3.1.1. Determine the transfer function model:#

Based on \ref{eq:Lin_model} we can now specify a transfer function between a single input (in the example below we choose the first input) and a single output by taking all other input values equal to zero. We can then create a transfer function model based on the previous result (\(\dot x = A x + Bu\)) by eliminating all other states from equation \ref{eq:Lin_model}. Using a matrix notation this elimination is performed by determining the inverse of a matrix:

(3.54)#\[\begin{align} s X'_1(s) &= a^1_1 X'_1(s) + \dots + a^1_n X'_n(s)+ b^1_1 U'(1) \\ \vdots &= \vdots \\ s X'_n(s) &= a^n_1 X'_1(s) + \dots + a^n_n X'_n(s)+ b^n_1 U'(1) \\ \end{align}\]

We can now solve this set of equation using basic matrix operations:

\[\begin{split}\begin{bmatrix} \frac{X'_1(s)}{U'_1(s)} \\ \vdots \\ \frac{X_n(s)}{U'_1(s)} \end{bmatrix} = \left ( sI - A \right )^{-1} \begin{bmatrix} b^1_1 \\ \vdots \\ b^n_1 \end{bmatrix} \end{split}\]

3.9.3.1.2. Simulate the transfer function model:#

3.9.3.2. 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;

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 sympy import Function, Matrix,symbol,symbols,solve,sin,pprint
from scipy import signal
import numpy as np 
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[1], line 10
      7 else: 
      8     get_ipython().run_line_magic('matplotlib', 'inline')
---> 10 from sympy import Function, Matrix,symbol,symbols,solve,sin,pprint
     11 from scipy import signal
     12 import numpy as np 

ImportError: cannot import name 'symbol' from 'sympy' (/builds/fet-edu-em/control-systems-and-applications-t4scs2/course-material/modeling-and-simulation-book/venv/lib/python3.8/site-packages/sympy/__init__.py)

The differential equation for the predefined model is

(3.55)#\[\begin{equation} \dot \nu = \frac{1}{m} \left ( \frac{n u}{r} T_m \left (1- \beta \left ( \frac{\omega}{\omega_n} -1 \right )^2 \right ) - mg\sin\theta - m g C_r sgn(\nu) -\frac{1}{2} \rho C_d A \nu^2 \right ) \end{equation}\]

with \(\nu\) the velocity of the car.

Specify the time as a variable

t = symbols('t')

we need to declare all time variable functions ν(t)=V

V = Function('v')(t)

Specify the parameter values:

n=5 #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]

Define u and \(\theta\) as two input variables and put them in a symbolic matrix U

u, theta = symbols('u theta', Real=True)
U = Matrix ([[ u], [theta ]])

Setup a vector dX consisting of all the differential equation in the ODE

(3.56)#\[\begin{align} \frac{dV}{dt} &= \frac{1}{m} \left ( \frac{n u}{r} T_m \left (1- \beta \left ( \frac{\omega}{\omega_n} -1 \right )^2 \right ) - mg\sin\theta - m g C_r sgn(\nu) -\frac{1}{2} \rho C_d A \nu^2 \right ) \end{align}\]

We will assume that \(\nu\) is positive therefore we will assume \(sign(\nu)=1\).

dX = Matrix ([1/m*(n*u/r*Tm*(1-beta*(n/r*V/omega_n-1)**2)-m*g*sin(theta)-m*g*Cr-1/2*rho*Cd*V**2*A)])

specify a vector X with the time variable functions

X = Matrix ([ V ])

Calculate the jacobian of dX with respect to the time variables defined in X

A_sym = dX.jacobian(X)
print(A_sym)
Matrix([[1.9*u*(0.019047619047619 - 0.000453514739229025*v(t)) - 0.0009984*v(t)]])

Calculate the jacobian of dX with respect to the time variables defined in U

B_sym = dX.jacobian(U)
print(B_sym)
Matrix([[1.9 - 0.76*(0.0238095238095238*v(t) - 1)**2, -9.8*cos(theta)]])

Linearize the differential equation around a stable linearisation point around state values \(V^0,U^0,theta^0\) that fit the model equations

(3.57)#\[\begin{align} \frac{dV}{dt} &= \frac{1}{m} \left ( \frac{n u}{r} T_m \left (1- \beta \left ( \frac{\omega}{\omega_n} -1 \right )^2 \right ) - mg\sin\theta - m g C_r sgn(\nu) -\frac{1}{2} \rho C_d A \nu^2 \right ) \end{align}\]

We choice \(V^0 =25\), \(\theta^0=0.01\)

V0=25 # Stable working point for the car speed is 25 m/s  
theta0=0.01 # Stable Working point for the steep angle is 0.01 rad 

then \(U^0\) can be solved because we know that a stable working point implies that \(\dot v = g(v,u) =0\). This equation can be solved using the symbolic solver included in the Sympy library:

q = dX.subs(V,V0).subs(theta,theta0) #substitute V and theta with V^0=25 and theta^0=0.01 in the equation dX 
sol=solve(q,u)  #find the U0 value that fits equation q=0  
U0= float(sol[u]) # Working point for the input 
print(sol[u]) 
0.286117676730995

We now substitute these linearization values in our symbolic matrices A and B

A=A_sym.subs(V,V0).subs(theta,theta0).subs(u,sol[u])
print(A)
B=B_sym.subs(V,V0).subs(theta,theta0).subs(u,sol[u])
print(B)
Matrix([[-0.0207687977512875]])
Matrix([[1.77548752834467, -9.79951000408332]])

Create a transfer function model based on the previous result (\(\dot x' = A x' + Bu'\)) with for example as input u’(t). Using equation (1) we can derive a transfer function if we assume \(x'_1(0)=0\) using the folowing equation:

(3.58)#\[\begin{align} s V(s) &= a^1_1 V(s) + b^1_1 U(s) + b^1_2 \Theta(s) \\ \end{align}\]

We can now solve easily solve this equation by taking \(\Theta(s)=0\):

(3.59)#\[\begin{align} \frac{V(s)}{U(s)} &= \frac{b^1_1}{s-a^1_1} \\ \end{align}\]
H = signal.TransferFunction(float(B[0]),[1,-float(A[0])])
print(H)
TransferFunctionContinuous(
array([1.77548753]),
array([1.       , 0.0207688]),
dt: None
)

We will generate a solutions in the interval 0 <= t <= 100.

Therefore we will specify a time vector array:

t=np.linspace(0,100,1000)
U=0.1 * np.ones(1000) # step input of 0.1 [m] 
X0=[0] #Initial value for the velocity
T, yout, xout = signal.lsim(H, U, t, X0)

Finally we plot the resulted simulated velocities (yout) 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, yout, 'b', label='TF: Linearized velocity of the car') # 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='Velocity v\' [m/s]') # label the y-axis 
ax0.grid() # plot a grid 

ax1.plot(T, yout+V0, 'b', label='TF: Real velocity of the car') # 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='Velocity v [m/s]') # label the y-axis 
ax1.grid() # plot a grid 

fig.tight_layout()
plt.show() # show the plot

3.9.4. → Check out this animated example#