How to plot the input which is R for LQR?
15 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am getting 2 outputs my system is inverted pendulum with 4 states.
How can I get the input plot the value of R as well?
further I am gettign only two states the cart position and teh angle of the penduum. How I can plot the other two states velocity and angular velocity in teh same plot where I am getting the position and the pendulum angle?
1 comentario
Image Analyst
el 1 de Ag. de 2021
Here's another chance to read the posting guidelines.
Maybe then somebody will be able to answer.
Respuestas (1)
Hitesh
el 23 de Ag. de 2024
Editada: Hitesh
el 23 de Ag. de 2024
Hello Inam!
According to your question, I am assuming that you are able to plot the cart position and pendulum angle on a plot and you want to plot the Control input “u”, velocity, and the angular velocity on the same axes.
To achieve this, you can set “hold” to “on” before plotting the new lines. For more details, please refer to the following documentation of “hold”: https://www.mathworks.com/help/matlab/ref/hold.html
Given below is an example for control problem of an inverted pendulum on a cart using MATLAB's “lqr” function and plotted all four state variables on a single axes. You can modify the values of the “A”,”B”,”Q” and “R” matrices as needed. I hope this resolves the issue for you.
Please refer to the below code:
% Define system matrices
A = [0 1 0 0; 0 0 -9.81 0; 0 0 0 1; 0 0 9.81 0];
B = [0; 1; 0; 1];
% Define LQR weighting matrices
Q = diag([10, 1, 10, 1]);
R = 0.1;
% Compute LQR gain matrix
K = lqr(A, B, Q, R);
% Simulation parameters
dt = 0.01; % Time step
t_final = 3; % Final time
time = 0:dt:t_final; % Time vector
% Initial state (small perturbation)
x0 = [0.1; 0; 0.1; 0]; % Initial position and angle perturbation
% Initialize state and input vectors
x = x0;
u = zeros(size(time)); % Control input vector
% Simulate the system
x_hist = zeros(length(x0), length(time)); % To store state history
for i = 1:length(time)
% Store current state
x_hist(:, i) = x;
% Compute control input using LQR
u(i) = -K * x;
% Update state using discrete-time approximation
x = x + (A * x + B * u(i)) * dt;
end
% Plot all data on the same axes
figure;
plot(time, u, 'k', 'LineWidth', 1.5, 'DisplayName', 'Control Input (u)');
hold on;
plot(time, x_hist(1, :), 'r', 'LineWidth', 1.5, 'DisplayName', 'Cart Position');
plot(time, x_hist(3, :), 'b', 'LineWidth', 1.5, 'DisplayName', 'Pendulum Angle');
plot(time, x_hist(2, :), 'g', 'LineWidth', 1.5, 'DisplayName', 'Cart Velocity');
plot(time, x_hist(4, :), 'm', 'LineWidth', 1.5, 'DisplayName', 'Angular Velocity');
title('LQR Control Input and System States');
xlabel('Time (s)');
ylabel('Values');
legend;
grid on;
Please refer to the resources for understanding of “lqr” function and Control of an Inverted Pendulum on a Cart:
- Control of an Inverted Pendulum on a Cart - MATLAB & Simulink Example (mathworks.com)
- Linear-Quadratic Regulator (LQR) design - MATLAB lqr (mathworks.com)
2 comentarios
Sam Chak
el 23 de Ag. de 2024
Hello Inam!
According to your question, I am assuming that ... you want to plot the input “R”, velocity, and the angular velocity on the same axes.
Hi @Hitesh
You mentioned that you assumed the OP wanted to plot the input "R". However, your proposed solution is unable to plot the input "R" weight in the LQR algorithm. Please rectify the issue to increase the likelihood of your answer being accepted by the OP.
Hitesh
el 26 de Ag. de 2024
Hi Sam!
I have already plotted the control input and the four state variables: Cart Position, Pendulum Angle, Cart Velocity, and Angular Velocity, as shown in the solution above. Initially, I used "R" to represent the control input, as Inam had done, but I have now changed it to "u," which is the standard notation for control input. I hope this answer your question.
Ver también
Categorías
Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!