i am trying to find the lqr controller system Transfer function, but getting error as "Arrays have incompatible sizes for this operation."

3 visualizaciones (últimos 30 días)
% Define system matrices
A = [0 20.95; -709.22 -106.85];
B = [3771.21; 12765.96];
C = [1 0];
D = 0;
% Define the LQR weighting matrices
Q = [0.0057312 0; 0 1]; % Weighting matrix for states
R = 1; % Weighting matrix for control inputs
% Compute LQR gain matrix
K = lqr(A, B, Q, R);
% Augment the system
[n, ~] = size(A);
AA = [A, zeros(n, 1); -C, 0];
BB = [B; 0];
% Construct the closed-loop system
Ac = AA - BB * K;
Arrays have incompatible sizes for this operation.
Bc = BB;
Cc = C;
Dc = D;
% Create state-space model of the closed-loop system
sys_cl = ss(Ac, Bc, Cc, Dc);
% Convert state-space model to transfer function
sys_tf = tf(sys_cl);
% Display the transfer function
disp('Transfer Function of the Closed-Loop System with LQR Controller:');
disp(sys_tf);

Respuesta aceptada

Sam Chak
Sam Chak el 28 de Abr. de 2024
I have marked the lines that require fixing. The step response characteristics have also been displayed. The selected Q and R weights of the LQR design resulted in a settling time of 0.0105 seconds and a percent overshoot of over 16%.
Are these the desired control performance requirements? If not, then pole placement, also known as eigenvalue assignment, would be a better design option for a second-order system.
% Define system matrices
A = [0 20.95; -709.22 -106.85];
B = [3771.21; 12765.96];
C = [1 0];
D = 0;
% Define the LQR weighting matrices
Q = [0.0057312 0; 0 1]; % Weighting matrix for states
R = 1; % Weighting matrix for control inputs
% Compute LQR gain matrix
K = lqr(A, B, Q, R);
% % Augment the system % no reason to augment it unless LQR not what you want
% [n, ~] = size(A);
% AA = [A, zeros(n, 1); -C, 0];
% BB = [B; 0];
% Construct the closed-loop system
Ac = A - B*K; % <-- fix it here
Bc = B; % <-- fix it here
Cc = C;
Dc = D;
% Create state-space model of the closed-loop system
sys_cl = ss(Ac, Bc, Cc, Dc);
op = findop(sys_cl, y=1); % <-- add it here
% Convert state-space model to transfer function
sys_tf = tf(op.u*sys_cl); % <-- fix it here
% Display the transfer function
disp('Transfer Function of the Closed-Loop System with LQR Controller:');
Transfer Function of the Closed-Loop System with LQR Controller:
sys_tf
sys_tf = 1.505e04 s + 2.675e06 --------------------------- s^2 + 1.298e04 s + 2.675e06 Continuous-time transfer function.
%% plot step response
S = stepinfo(sys_tf) % <-- add it here
S = struct with fields:
RiseTime: 1.0662e-04 TransientTime: 0.0105 SettlingTime: 0.0105 SettlingMin: 0.9067 SettlingMax: 1.1617 Overshoot: 16.1749 Undershoot: 0 Peak: 1.1617 PeakTime: 4.7615e-04
step(sys_tf), grid on % <-- add it here
  1 comentario
Mr. Pavl M.
Mr. Pavl M. el 20 de Nov. de 2024
Editada: Mr. Pavl M. el 20 de Nov. de 2024
Why did you do system matrices augmentation after calling to lqr function, while in theory they do augmentation for lqr and lqi before calling to lqr/lqi function, what is the sense of doing so?
Let me sharpen thinking, improve understanding to catch apprehend as in nature all the subleties.
How will such controller be possible to implement in reality in real hardware embedded C MCU, like A-B*K, the gain is just amplifier, do you need Kalman state estimator to add to it, how the blockscheme of the closed loop system will look like (without integrator and with just Kalman or other State Estimator)?

Iniciar sesión para comentar.

Más respuestas (1)

Sam Chak
Sam Chak el 28 de Abr. de 2024
If fast-rising spike and the overshoot are undesired and you would like to eliminate it, I suggest adding a prefilter at the reference input path.
% Define system matrices
A = [0 20.95; -709.22 -106.85];
B = [3771.21; 12765.96];
C = [1 0];
D = 0;
% Define the LQR weighting matrices
Q = [0.0057312 0; 0 1]; % Weighting matrix for states
R = 1; % Weighting matrix for control inputs
% Compute LQR gain matrix
K = lqr(A, B, Q, R);
% % Augment the system % no reason to augment it unless LQR not what you want
% [n, ~] = size(A);
% AA = [A, zeros(n, 1); -C, 0];
% BB = [B; 0];
% Construct the closed-loop system
Ac = A - B*K; % <-- fix it here
Bc = B; % <-- fix it here
Cc = C;
Dc = D;
% Create state-space model of the closed-loop system
sys_cl = ss(Ac, Bc, Cc, Dc);
% Convert state-space model to transfer function
sys_tf = tf(sys_cl); % <-- fix it here
[n, d] = tfdata(sys_tf, 'v'); % <-- add it here
%% Pre-filter
Gf = tf(d(end), n) % <-- add it here
Gf = 2.675e06 ----------------- 3771 s + 6.704e05 Continuous-time transfer function.
%% Display the transfer function of the Command Compensated System
disp('Transfer Function of the Command Compensated System:');
Transfer Function of the Command Compensated System:
Gcc = minreal(Gf*sys_tf) % <-- fix it here
Gcc = 2.675e06 --------------------------- s^2 + 1.298e04 s + 2.675e06 Continuous-time transfer function.
%% plot step response
S = stepinfo(Gcc) % <-- add it here
S = struct with fields:
RiseTime: 0.0105 TransientTime: 0.0187 SettlingTime: 0.0187 SettlingMin: 0.9029 SettlingMax: 0.9993 Overshoot: 0 Undershoot: 0 Peak: 0.9993 PeakTime: 0.0349
step(Gcc), grid on % <-- add it here

Community Treasure Hunt

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

Start Hunting!

Translated by