Why is nothing showing up on my plots?

4 views (last 30 days)
Jade Cruz
Jade Cruz on 5 Jul 2023
Answered: Star Strider on 5 Jul 2023
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
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
Jade Cruz
Jade Cruz on 5 Jul 2023
@Sandeep Mishra Thank you for your help! I have posted a comment below your answer. I appreciate the continuous support.

Sign in to comment.

Answers (3)

Steven Lord
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
ans = 'none'
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
  1 Comment
Jade Cruz
Jade Cruz on 5 Jul 2023
Thank you so much. This was what I was thinking. I will try this out!

Sign in to comment.


Sandeep Mishra
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)
In MATLAB, you can use figure(n) to create a figure with number property is equal to n.
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
J = 3×1
5.7498 -4.7984 1.0000
q = 3×1
10.2589 20.2089 30.2189
J = 3×1
7.0790 -2.0237 1.0000
q = 3×1
10.4612 20.3612 30.3812
J = 3×1
7.1168 0.3353 1.0000
q = 3×1
10.5935 20.4435 30.4735
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
Jade Cruz
Jade Cruz on 5 Jul 2023
Edited: Jade Cruz on 5 Jul 2023
That is perfect but I do notice that all the figures do not show any sort of information or plot. Would the problem be my equations and not my code?

Sign in to comment.


Star Strider
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.
.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!