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,
(1)
and the equation of motion, we obtain the following state-space model
(2)
where
(3)
Let
(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,
(11)
with respect to the control input
(12)
where
By partitioning
(13)
and by using the definition of
(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.