LQG controller design with LQR

11 views (last 30 days)
M. M.  Farhad
M. M. Farhad on 8 May 2017
Edited: Pavl M. on 8 Nov 2024 at 18:59
I am using lqg command and got a b c and d value for the controller. And from lqr I have k value. But how to incorporate them in my system together? I mean what is my final optimal systems A, B, C and D value that can be obtained from lqg controller and LQR gain?

Answers (1)

Pavl M.
Pavl M. on 8 Nov 2024 at 18:49
Edited: Pavl M. on 8 Nov 2024 at 18:59
clc
clear all
close all
n_states = 5;
n_controls = 5;
n_outputs = 5;
A = [-0.4628, 0.3217, 0.5789, -0.3823, -0.0507;
0.7593, 1.0396, -0.2008, -0.2438, -0.0678;
0.2106, 0.6490, 0.3973, 0.8735, 0.2070;
0.3128, 0.3380, 0.1524, 0.0914, -0.1088;
0.0353, 0.0419, -0.0355, 0.0682, 0.7576];
B = [0.0026;
0.0088;
-0.0036;
0.0490;
-0.1443];
C = [1.1099, 1.3983, 0.8999, -0.0136, 0.0652];
D = 0;
dt = 0.02;
aug_level = 0;
gain = 4;
sys = ss(A, B, C, D, dt,'InputName','u','OutputName',{'y'});
% define your control, cross and state cost matrices:
R = 1;
N = 1;
Q = [1 0 0 0 0 0;0 2 0 0 0 0;0 0 5 4 0 0;0 0 0 5 0 0; 0 1 0 0 5 1;0 0 0 0 0 1];
Q1 = (Q+Q')/2
Q1 = 6×6
1.0000 0 0 0 0 0 0 2.0000 0 0 0.5000 0 0 0 5.0000 2.0000 0 0 0 0 2.0000 5.0000 0 0 0 0.5000 0 0 5.0000 0.5000 0 0 0 0 0.5000 1.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A = rand(n_states+1, n_states+1);
A_symmetric = tril(A) + triu(A', 1);
Q2 = A_symmetric
Q2 = 6×6
0.6629 0.6854 0.6830 0.7862 0.5655 0.1663 0.6854 0.7813 0.6157 0.7265 0.0305 0.1561 0.6830 0.6157 0.6874 0.8049 0.1267 0.4960 0.7862 0.7265 0.8049 0.8787 0.5216 0.8728 0.5655 0.0305 0.1267 0.5216 0.7036 0.1557 0.1663 0.1561 0.4960 0.8728 0.1557 0.8840
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
QXU = blkdiag(0.1*eye(n_states),sqrt(Q))
QXU = 11×11
0.1000 0 0 0 0 0 0 0 0 0 0 0 0.1000 0 0 0 0 0 0 0 0 0 0 0 0.1000 0 0 0 0 0 0 0 0 0 0 0 0.1000 0 0 0 0 0 0 0 0 0 0 0 0.1000 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 1.4142 0 0 0 0 0 0 0 0 0 0 0 2.2361 2.0000 0 0 0 0 0 0 0 0 0 0 2.2361 0 0 0 0 0 0 0 0 1.0000 0 0 2.2361 1.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
QWV = blkdiag(Q,0.01)
QWV = 7×7
1.0000 0 0 0 0 0 0 0 2.0000 0 0 0 0 0 0 0 5.0000 4.0000 0 0 0 0 0 0 5.0000 0 0 0 0 1.0000 0 0 5.0000 1.0000 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0.0100
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
QI = 1 ; %eye(ny)
KLQG1 = lqg(sys,Q1,Q,QI,'1dof')
Warning: QN is not symmetric and was replaced by (QN+QN')/2.
KLQG1 = A = x1_e x2_e x3_e x4_e x5_e xi1 x1_e -0.6969 0.02017 0.394 -0.3729 -0.06068 0.002294 x2_e 0.2617 0.3901 -0.5879 -0.2157 -0.08427 0.007763 x3_e -0.3262 -0.01802 -0.04461 0.8711 0.1702 -0.003176 x4_e -0.0487 -0.243 -0.04954 0.2184 -0.05898 0.04323 x5_e 0.2911 0.734 -0.09655 -0.2959 0.5634 -0.1273 xi1 0 0 0 0 0 1 B = e1 x1_e -0.2057 x2_e -0.4308 x3_e -0.4908 x4_e -0.2278 x5_e -0.0579 xi1 0.02 C = x1_e x2_e x3_e x4_e x5_e xi1 u -2.218 -5.357 0.06196 2.529 1.32 0.8822 D = e1 u 0 Input groups: Name Channels Error 1 Output groups: Name Channels Controls 1 Sample time: 0.02 seconds Discrete-time state-space model.
KLQG2 = lqg(sys,Q1,Q,QI,'2dof')
Warning: QN is not symmetric and was replaced by (QN+QN')/2.
KLQG2 = A = x1_e x2_e x3_e x4_e x5_e xi1 x1_e -0.6969 0.02017 0.394 -0.3729 -0.06068 0.002294 x2_e 0.2617 0.3901 -0.5879 -0.2157 -0.08427 0.007763 x3_e -0.3262 -0.01802 -0.04461 0.8711 0.1702 -0.003176 x4_e -0.0487 -0.243 -0.04954 0.2184 -0.05898 0.04323 x5_e 0.2911 0.734 -0.09655 -0.2959 0.5634 -0.1273 xi1 0 0 0 0 0 1 B = r1 y x1_e 0 0.2057 x2_e 0 0.4308 x3_e 0 0.4908 x4_e 0 0.2278 x5_e 0 0.0579 xi1 0.02 -0.02 C = x1_e x2_e x3_e x4_e x5_e xi1 u -2.218 -5.357 0.06196 2.529 1.32 0.8822 D = r1 y u 0 0 Input groups: Name Channels Setpoint 1 Measurement 2 Output groups: Name Channels Controls 1 Sample time: 0.02 seconds Discrete-time state-space model.
%csys = ss(A-B*K,B,C,D)
figure
step(KLQG1)
title('1DOF Servo Controller designed')
figure
step(KLQG2)
title('2DOF Servo Controller designed')
%Constructing closed-loop system after designing:
%Ther are many similar questions this answer will fit(answer) to:
%https://www.mathworks.com/matlabcentral/answers/484846-how-to-use-the-generated-lqg-controller
C=KLQG1;
P = sys;
set(C,'InputName','e')
set(C,'OutputName','u')
set(P,'OutputName','y')
set(P,'InputName','u')
Sum_Error = sumblk('e = r - y');
clsys = tf(connect(C,P,Sum_Error,'r','y'))
clsys = From input "r" to output "y": 0.003938 z^9 + 0.00479 z^8 - 0.01414 z^7 - 0.04427 z^6 + 0.1179 z^5 - 0.08028 z^4 - 0.003101 z^3 + 0.01298 z^2 + 0.007207 z - 0.004974 ---------------------------------------------------------------------------------------------------------------------------------------------------- z^11 - 3.254 z^10 + 3.293 z^9 - 0.3241 z^8 - 1.017 z^7 - 0.02715 z^6 + 0.4166 z^5 - 0.0324 z^4 - 0.05791 z^3 - 0.002832 z^2 + 0.005646 z - 4.509e-05 Sample time: 0.02 seconds Discrete-time transfer function.
clsys_m = minreal(clsys)
clsys_m = From input "r" to output "y": 0.003938 z^9 + 0.00479 z^8 - 0.01414 z^7 - 0.04427 z^6 + 0.1179 z^5 - 0.08028 z^4 - 0.003101 z^3 + 0.01298 z^2 + 0.007207 z - 0.004974 ---------------------------------------------------------------------------------------------------------------------------------------------------- z^11 - 3.254 z^10 + 3.293 z^9 - 0.3241 z^8 - 1.017 z^7 - 0.02715 z^6 + 0.4166 z^5 - 0.0324 z^4 - 0.05791 z^3 - 0.002832 z^2 + 0.005646 z - 4.509e-05 Sample time: 0.02 seconds Discrete-time transfer function.
figure
step(clsys_m)
title('Constructed closed loop system response')
%Constructed from needing help code by:
%https://join.skype.com/invite/oXnJhbgys7oW
%https://independent.academia.edu/PMazniker
%+380990535261
%https://diag.net/u/u6r3ondjie0w0l8138bafm095b
%https://github.com/goodengineer
%https://orcid.org/0000-0001-8184-8166
%https://willwork781147312.wordpress.com/portfolio/cp/
%https://www.youtube.com/channel/UCC__7jMOAHak0MVkUFtmO-w
%https://nanohub.org/members/130066
%https://pangian.com/user/hiretoserve/
%https://substack.com/profile/191772642-paul-m

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!