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

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?

 Accepted Answer

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 Comments

Really thanks for your help!!
But I have another question. I want to introduce some disturbances in my system. Do you know how to edit the LQR coding in order for me to define the disturbances as shown in the figure below?
Noted with thanks. By ignoring the disturbance, the coding that you have provided is very useful and easy to understand!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!