Why is nothing showing up on my plots?
4 views (last 30 days)
Show older comments
All three plots won't show the iterations from my calculation.
% Simulate the system
for k = 1:(tf - t0)/dt
% Update the reference joint positions
q_r = q + dt*qdr;
% Calculate the Jacobian
J = calc_jacobian(q, L1, L2, L3);
% Update the actual joint positions
q = q_r + dt*J'*qdr;
% Plot the results
if k == 1
plot_results(q, J);
end
end
function plot_results(q, J)
% Plot the joint positions
subplot(3, 1, 1)
hold on
plot(q(1), 'b')
plot(q(2), 'r')
plot(q(3), 'g')
xlabel('Time (s)')
ylabel('Joint angle (deg)')
% Plot the Jacobian determinant
subplot(3, 1, 2)
plot(J'*J)
xlabel('Time (s)')
ylabel('Jacobian determinant')
% Plot the end-effector pose
subplot(3, 1, 3)
plot(q(1), q(2), 'b*')
xlabel('x (m)')
ylabel('y (m)')
axis([-2, 4, -2, 4])
end
9 Comments
Sandeep Mishra
on 5 Jul 2023
I have answered that for example case with 3 different figures.
If you still face any error, do comment
Answers (3)
Steven Lord
on 5 Jul 2023
When you call plot with just a color in the line specification, what you get is a plot with no markers.
h = plot(1, 'b');
h.Marker
What does a line "connecting" one point look like? Nothing. If instead you'd specified a marker in addition to a color, you'd get a line "connecting" one point and a marker on that point.
figure
h = plot(1, 'bx');
If you want to create a plot where you add points to a line iteratively, I'd use the animatedline function to create the line then call addpoints on the animated line. You won't be able to see the iterative process in Answers, but if you run the code below in an interactive session of MATLAB you'll see the plot appear, point by point.
x = 0:360;
y = sind(x);
figure
axis([0 360 -1 1])
h = animatedline('LineStyle', '-', 'Color', 'b');
for k = 1:numel(x)
addpoints(h, x(k), y(k))
pause(1/32)
end
Sandeep Mishra
on 5 Jul 2023
Edited: Torsten
on 5 Jul 2023
Hello Jade,
I understood from our discussion in the comments that you are trying to plot multiple plots (in your case = 50)
You can refer the below example code (plot with 3 figures)
clear;
% Define the parameters
L1 = 4;
L2 = 3;
L3 = 2;
dt = 0.1;
t0 = 0;
tf = 5;
q0 = [10, 20, 30]';
qdr = [0.2, -0.3, -0.2]';
% Initialize the variables
q = q0;
J = zeros(3, 3);
% for k = 1:(tf - t0)/dt
% Example
for k = 1:3
% Update the reference joint positions
q_r = q + dt*qdr;
% Calculate the Jacobian
J = calc_jacobian(q, L1, L2, L3)
% Update the actual joint positions
q = q_r + dt*J'*qdr
% Plot the results
figure(k)
plot_results(q, J);
end
function J = calc_jacobian(q,L1,L2,L3)
% Calculate the Jacobian
J = [-L1*sin(q(1)) - L2*sin(q(1) + q(2)) - L3*sin(q(1) + q(2) + q(3));
L1*cos(q(1)) + L2*cos(q(1) + q(2)) + L3*cos(q(1) + q(2) + q(3));
1];
end
function plot_results(q, J)
% Plot the joint positions
subplot(3, 1, 1)
hold on
plot(q(1), 'b')
plot(q(2), 'r')
plot(q(3), 'g')
xlabel('Time (s)')
ylabel('Joint angle (deg)')
% Plot the Jacobian determinant
subplot(3, 1, 2)
plot(J'*J)
xlabel('Time (s)')
ylabel('Jacobian determinant')
% Plot the end-effector pose
subplot(3, 1, 3)
plot(q(1), q(2), 'b*')
xlabel('x (m)')
ylabel('y (m)')
axis([-2, 4, -2, 4])
end
You can refer to the below documentation to learn more about "figure" in MATLAB.
2 Comments
Star Strider
on 5 Jul 2023
It now fills the matrices corrrectly and plots something, however I cannot determine what you want it to plot —
% Regarding the missing values:
% Define the parameters
L1 = 4;
L2 = 3;
L3 = 2;
dt = 0.1;
t0 = 0;
tf = 5;
q0 = [10, 20, 30]';
qdr = [0.2, -0.3, -0.2]';
% Initialize the variables
q(:,1) = q0;
J = zeros(3, 3);
kv = 1:(tf - t0)/dt; % Create Vector
for k = 1:numel(kv)
% Update the reference joint positions
q_r(:,k+1) = q(:,k) + dt*qdr; % Subscript Result (Create Matrix Of Column Vectors)
% Calculate the Jacobian
J(:,k+1) = calc_jacobian(q(:,k), L1, L2, L3); % Subscript Result (Create Matrix Of Column Vectors)
% Update the actual joint positions
q(:,k+1) = q_r(:,k) + dt*J(:,k)'*qdr; % Subscript Result (Create Matrix Of Column Vectors)
end
% Plot the results
figure
plot(q, J)
grid
xlabel('q')
ylabel('J')
title('Original Matrices')
figure
plot(q.', J.')
grid
xlabel('q')
ylabel('J')
title('Transposed Matrices')
function J = calc_jacobian(q,L1,L2,L3)
% Calculate the Jacobian
J = [-L1*sin(q(1)) - L2*sin(q(1) + q(2)) - L3*sin(q(1) + q(2) + q(3));
L1*cos(q(1)) + L2*cos(q(1) + q(2)) + L3*cos(q(1) + q(2) + q(3));
1];
end
I defer to you to figure out how to plot the results.
.
0 Comments
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!