How to plot the input which is R for LQR?

조회 수: 13 (최근 30일)
INAM QUTUB UL DIN SHAH
INAM QUTUB UL DIN SHAH 2021년 8월 1일
댓글: Hitesh 2024년 8월 26일
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
Image Analyst
Image Analyst 2021년 8월 1일
Here's another chance to read the posting guidelines.
Maybe then somebody will be able to answer.

댓글을 달려면 로그인하십시오.

답변 (1개)

Hitesh
Hitesh 2024년 8월 23일
편집: Hitesh 2024년 8월 23일
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:
  댓글 수: 2
Sam Chak
Sam Chak 2024년 8월 23일
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.
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
Hitesh 2024년 8월 26일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by