# Linear Quadratic Regulator (LQR) controller for time-varying matrix B

27 vues (au cours des 30 derniers jours)
Linee Teh le 19 Avr 2020
Commenté : Ameer Hamza le 20 Avr 2020
Hey, I am here to ask for help in the coding for LQR controller. The code is provided as follow.
t = linspace(0, 5640); % default 100 points
i = 83*pi/180; % inclination angle in rad
w0 = 0.0011; % angular velocity in rad/s
b_1 = sin(i)*cos(w0*t);
b_2 = -cos(i)*ones(size(t));
b_3 = 2*sin(i)*sin(w0*t);
J1 = 4;
J2 = 4.2;
J3 = 4;
% choose Q & R
Q = diag([0.001 0.001 0.001 0.001 0.001 0.001]);
R = diag([1e3 1e3 1e3]);
% Define system
dfw = [0 0 w0*(-J1+J2-J3)/J1; 0 0 0; w0*(J1-J2+J3)/J3 0 0];
dfq = [2*(w0^2)*(J3-J2)/J1 0 0; 0 0 0; 0 0 2*(w0^2)*(J1-J2)/J3];
ZERO = zeros(1,1,numel(b_1));
b1 = reshape(b_1, 1,1,[]);
b2 = reshape(b_2, 1,1,[]);
b3 = reshape(b_3, 1,1,[]);
A = [dfw dfq; 0.5*eye(3) zeros(3)];
C = eye(6);
D = zeros(6,3);
for n = 1:length(t)
B = [ ZERO b3/J1 b2/J1;
-b3/J2 ZERO b1/J2;
b2/J3 -b1/J3 ZERO;
zeros(3,3,numel(b_1))]
[K, S, e] = lqr(A,B,Q,R);
end
The state-space model has constant A, C and D matrices but B matrix is varying with time.
When I computed this code, error coming out by saying that "Error using lqr (line 43): The "lqr" command operates on a single model."
Can any expert on LQR controller help me to solve this question?
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Ameer Hamza le 19 Avr 2020
Since your matrix B has 100 slices in the 3rd dimension, you will have 100 K, S, and e matrices. Replace your for loop with
B = [ ZERO b3/J1 b2/J1;
-b3/J2 ZERO b1/J2;
b2/J3 -b1/J3 ZERO;
zeros(3,3,numel(b_1))];
for n = 1:length(t)
[K(:,:,n), S(:,:,n), e(:,:,n)] = lqr(A,B(:,:,n),Q,R);
end
##### 4 commentairesAfficher 2 commentaires plus anciensMasquer 2 commentaires plus anciens
Linee Teh le 20 Avr 2020
Noted with thanks. By ignoring the disturbance, the coding that you have provided is very useful and easy to understand!
Ameer Hamza le 20 Avr 2020
I am glad to be of help.

Connectez-vous pour commenter.

### Catégories

En savoir plus sur State-Space Control Design and Estimation dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by