i am trying to find the lqr controller system Transfer function, but getting error as "Arrays have incompatible sizes for this operation."
4 views (last 30 days)
Show older comments
% 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;
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);
0 Comments
Accepted Answer
Sam Chak
on 28 Apr 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:');
sys_tf
%% plot step response
S = stepinfo(sys_tf) % <-- add it here
step(sys_tf), grid on % <-- add it here
1 Comment
Mr. Pavl M.
on 20 Nov 2024
Edited: Mr. Pavl M.
on 20 Nov 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)?
More Answers (1)
Sam Chak
on 28 Apr 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
%% Display the transfer function of the Command Compensated System
disp('Transfer Function of the Command Compensated System:');
Gcc = minreal(Gf*sys_tf) % <-- fix it here
%% plot step response
S = stepinfo(Gcc) % <-- add it here
step(Gcc), grid on % <-- add it here
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
