December 4, 2024

Tutorial on How to Simulate Sliding Mode Control Algorithm in MATLAB and Simulink


In this control engineering tutorial, we explain how to simulate a sliding mode controller in MATLAB and Simulink. We will not go too deep into the theory of sliding mode control and we will mainly focus on the implementation in MATLAB and Simulink. We will briefly formulate the system we want to control and the control law. We will also show that the system controlled by using the sliding control law satisfies a stability condition. In the next tutorial, we will provide a deeper theoretical explanation of the sliding mode control.

Model Definition and Sliding Mode Control

The system is described as follows

(1)   \begin{align*}m\ddot{x}(t)=u(t)+d(t)\end{align*}

where x(t)\in \mathbb{R} is a scalar variable that we want to control, u(t)\in \mathbb{R} is the control input, m is the model parameter (mass for example), and d(t)\in \mathbb{R} is the disturbance. We use a single dot above variables to denote the first-time derivative and a double dot to denote the second-time derivative. The system (1) is a double integrator. In reality, this system can be the mass m with the control force u and under the presence of the disturbance force d.

We assume that the disturbance signal d(t) is not known, however, we assume that the bound on the uncertainty is known

(2)   \begin{align*}|d(t)|\le \alpha\end{align*}

That is, we assume that the scalar \alpha\in \mathbb{R} is known. In fact, we can consider \alpha as a control design parameter.

Our control objective is to design a control algorithm that will make sure that x(t) follows a time-varying reference trajectory denoted by x_{D}(t) and that will reject the time-varying disturbance d(t).

In this tutorial, we design a sliding mode control algorithm. To formulate the control problem, we first introduce the control error as follows

(3)   \begin{align*}\varepsilon (t) =x_{D}(t)-x(t)\end{align*}

For notation brevity, in the sequel we will omit the time dependence. It is understood that all variables and signals depend on time. First of all, we select the sliding function which is also known as the switching function as follows:

(4)   \begin{align*}\sigma =c \varepsilon + \dot{\varepsilon}\end{align*}

where \sigma is the switching function and c>0 is a positive scalar. The parameter c is a design parameter. Then, we select the control law as follows

(5)   \begin{align*}u=m\Big( c\dot{\varepsilon}+\ddot{x}_{D}+\frac{1}{m}\big(k \sigma+\alpha \text{sgn}(\sigma) \big) \Big)\end{align*}

In the above control law

  • m is the dynamical system model parameter
  • c is the parameter defining the switching function (4)
  • \dot{\varepsilon} is the first derivative of the control error that is defined in (3)
  • \ddot{x}_{D} is the second derivative of the reference (desired) trajectory
  • k>0 is a positive control design parameter (selected by the user)
  • \sigma is the switching function (sliding function) defined in (4)
  • \alpha is the disturbance bound defined in (2)
  • \text{sgn} is the sign function

The type of the control law (5) belongs to the class of the variable structure control laws. Its structure and value depend on the value of the switching function \sigma. Due to the sign function, this control law will have three forms. For positive values of the switching function, it will have the first form. Then, for the zero values of the switching function, it will have the second form, and finally, for the negative values of the switching function, it will have the third form.

Next, we will show that the control law (5) satisfies the stability condition. Namely, we will show that this type of control law will guarantee that the sliding mode control will work in practice. The main condition for guaranteeing that the sliding mode controller will work is given by the following equation:

(6)   \begin{align*}\sigma \cdot \dot{\sigma} < 0, \;\; \text{except for}\;\;  \sigma =0\end{align*}

To show this, let us take the first and second derivatives of the error equation (3)

(7)   \begin{align*}\dot{\varepsilon} (t) & =\dot{x}_{D}-\dot{x} \\\ddot{\varepsilon} (t) & =\ddot{x}_{D}-\ddot{x} \end{align*}

Let us take the first derivative of the switching function (4)

(8)   \begin{align*}\dot{\sigma} =c \dot{\varepsilon} + \ddot{\varepsilon}\end{align*}

By substituting the second equation from (7) in (8), we obtain

(9)   \begin{align*}\dot{\sigma} =c \dot{\varepsilon} +\ddot{x}_{D}-\ddot{x} \end{align*}

On the other hand, from (1), we have

(10)   \begin{align*}\ddot{x}=\frac{1}{m}\big( u+d \big)\end{align*}

By substituting (10) in (9), we obtain

(11)   \begin{align*}\dot{\sigma} =c \dot{\varepsilon} +\ddot{x}_{D}-\frac{1}{m}\big( u+d \big)\end{align*}

On the other hand, by substituting the control law (5) in (11), we obtain

(12)   \begin{align*}\dot{\sigma} & =c \dot{\varepsilon} +\ddot{x}_{D}-\frac{1}{m}\Big(m\big( c\dot{\varepsilon}+\ddot{x}_{D}+\frac{1}{m}\big(k \sigma+\alpha \text{sgn}(\sigma) \big) \big) +d \Big) \\\dot{\sigma} & =c \dot{\varepsilon} +\ddot{x}_{D}-c\dot{\varepsilon}-\ddot{x}_{D}-\frac{1}{m}\big(k \sigma+\alpha \text{sgn}(\sigma)\big)-\frac{1}{m}d \\\dot{\sigma} & = -\frac{1}{m}k\sigma -\frac{1}{m}\alpha\text{sgn}(\sigma)-\frac{1}{m}d\end{align*}

Next, from the last equation of (12) we have

(13)   \begin{align*}\sigma \dot{\sigma}= -\frac{1}{m}k\sigma^{2} -\frac{1}{m}\alpha\sigma \text{sgn}(\sigma)-\frac{1}{m}d\sigma\end{align*}

Next, we have

(14)   \begin{align*}\sigma \text{sgn}(\sigma) = |\sigma| \end{align*}

On the other hand, from (2), we have

(15)   \begin{align*}-\frac{1}{m}d\sigma \le \frac{1}{m}\alpha |\sigma|\end{align*}

By using (14) and (15) in (13), we have

(16)   \begin{align*}\sigma \dot{\sigma} \le -\frac{1}{m}k\sigma^{2}-\frac{1}{m}\alpha|\sigma|+\frac{1}{m}\alpha |\sigma|\end{align*}

From the last equation, we have

(17)   \begin{align*}\sigma \dot{\sigma} \le -\frac{1}{m}k\sigma^{2}\end{align*}

Consequently, since k>0, we see that the last equation implies that the following equation

(18)   \begin{align*}\sigma \dot{\sigma} <0\end{align*}

is satisfied everywhere except at \sigma=0. This shows that the sliding control law is stable.

Simulink and MATLAB Implementation of the Sliding Mode Control