In this control engineering and control theory tutorial, we explain how to model and simulate Linear Quadratic Regulator (LQR) optimal controller in Simulink and MATLAB. We augment the basic LQR controller with an integral control action to improve the tracking performance of the LQR regulator. The YouTube tutorial is given below. In our next tutorial, whose link is given here, we explain how to design a pole placement controller in Simulink.
As a test case, we consider the mass-spring-damper system shown below.
In the figure above, is the position of the point mass from its equilibrium point, is the damper constant, is the spring constant, is the mass, and is the external force applied to the point mass. The derivation of the state-space model of this system is thoroughly explained in our previous video tutorial, which can be found here. Here, for brevity and completeness of this tutorial, we briefly summarize the state-space model. By using the following state-space variables:
(1)
and the equation of motion, we obtain the following state-space model
(2)
where
(3)
Let denote the reference signal. This is the signal that is set by the user and the goal is to design a feedback control algorithm that will track this signal. For that purpose, we introduce the control error (also known as the tracking error):
(4)
Next, we introduce the integral of the control error
(5)
By taking the first derivative of the last equation, and by substituting the output equation of the state-space model in the resulting equation, we obtain
(6)
By combining this equation with the state equation, we obtain the augmented system:
(7)
The last two equations can be written in the compact form
(8)
The last equation can be written compactly as follows
(9)
where
(10)
Here, is the state of the augmented system. Here, for simplicity, while calculating the LQR controller, we will not explicitly take into account the constant reference signal . This signal is implicitly taken into account by the variable . The LQR controller aims at minimizing the following cost function
(11)
with respect to the control input . The matrices , , and are the weighting matrices selected by the user. In this tutorial, we set . The solution is given by the feedback control algorithm
(12)
where is the feedback control matrix. As we will explain later, this matrix can easily be computed by using the MATLAB function “lqr()”.
By partitioning as follows
(13)
and by using the definition of given in (10), we obtain
(14)
By using (5), the last equation can be written as follows
(15)
We can clearly observe that the controller explicitly takes into account the integral of the control error. To develop a Simulink model and simulate it, we need to create a block diagram of the system and the controller. The block diagram is given below.
This block diagram is important for developing the Simulink model. The MATLAB script that is used to design the LQR controller is given below.
ks=1
kd=0.1
m=10
A=[0 1; -ks/m -kd/m];
B=[0; 1/m];
C=[1 0]
D=[0]
% compute the open loop poles
openLoopPoles=eig(A)
% form the augmented system
Aa=[A [0; 0]; -C 0]
Ba=[B;0]
Ca=[C, 0]
ssModel=ss(Aa,Ba,Ca,[])
% design the optimal controller
Q=[3 0 0;
0 3 0;
0 0 3;]
R=1;
N=zeros(3,1)
[K,~,~] = lqr(ssModel,Q,R,N)
% check the closed loop poles
eig(Aa-Ba*K)
% extract feedback matrices
Kx=K(:,1:2)
Ki=K(3)
This code, as well as the Simulink model, are explained in the YouTube tutorial given at the top of this tutorial.