Why is my plot not working in my for loop?

9 views (last 30 days)
Masiel Chanis
Masiel Chanis on 13 Oct 2021
Edited: KSSV on 13 Oct 2021
%% Calculations for a 4 bar linkage
% Orgin
Ox=0;
Oy=0;
%% Link Lengths
L1=6;
L2=2;
L3=7;
L4=9;
RP=6;
theta2=linspace(0,2*pi,2*pi/360);
%theta2=pi/4;
%for theta2=
%% K values
K1= L1/L2;
K2=L1/L4;
K3=(L2^2-L3^2+L4^2+L1^2)/(2*L2*L4);
K4= L1/L3;
K5= (L4^2-L3^2-L2^2-L1^2)/(2*L2*L3);
for i=1:length(theta2);
%% Calculate OTHER variables here.
A(i)= cos(theta2(i))-K1-K2*cos(theta2(i))+K3;
B(i)= -2*sin(theta2(i));
C(i)= K1-(K2+1)*cos(theta2(i))+K3;
D(i)= cos(theta2(i))-K1+K4*cos(theta2(i))+K5;
E(i)= -2*sin(theta2(i));
F(i)= K1+(K4-1)*cos(theta2(i))+K5;
%% Calculate your thetas
theta3(i)= 2*atan((-E-(E^2-4*D*F)^(1/2))/(2*D));
theta4(i) = 2*atan((-B+(B^2-4*A*C)^(1/2))/(2*A));
%theta_rp= theta2-theta3; or
theta_rp=30*(pi/180);
%% Determine the ABSOLUT position of the ENDS of each link (for plotting)
L1X(i)=L1;
L2X(i)= L2*cos(theta2(i));
L2Y(i)= L2*sin(theta2(i));
L3X(i)= L2X(i)+L3*cos(theta3(i));
L3Y(i)= L2Y(i)+L3*sin(theta3(i));
L4X(i)= L1-L4*sin(theta4(i));
L4Y(i)= L4*sin(theta4(i));
RPX(i)=L2X(i)+RP*cos(theta3(i)+theta_rp);
RPY(i)=L2Y(i)+RP*sin(theta3(i)+theta_rp);
%% Plot variables
line_width = 4.5;
marker_size = 9;
%% WHAT INDEX VALUE SHOULD THIS BE IF YOU WANT TO PLOT theta2 = 30 degrees?
%ii = pi/4; % CHANGE THIS NUMBER BUT USE IT TO CREATE THE CORRECT PLOT POSITION AS REQUIRED
figure(1)
hold on
plot([0 L1X(i)],[0 0],'-^k') % Plot the ground link as black with triangles at each end. Change the line width and markersize so the the link is visible
plot([0 L2X(i)],[0 L2Y(i)],'-or') % Plot the crank link as red with circles at each end. Change the line width and markersize so the the link is visible
plot([L2X(i) L3X(i)],[L2Y(i) L3Y(i)],'-og') % Plot the coupler link as green with circles at each end. Change the line width and markersize so the the link is visible
plot([L1X(i) L4X(i)],[0 L4Y(i)],'-ob') % Plot the output link as blue with circles at each end. Change the line width and markersize so the the link is visible
plot([L2X(i) RPX(i)],[L2Y(i) RPY(i)],'-om') % Plot ALL OF THE COUPLER POINTS
grid on;
drawnow;
holf off;
end

Answers (2)

Dave B
Dave B on 13 Oct 2021
Edited: Dave B on 13 Oct 2021
this isn't running a loop because you're iterating from 1 to length(theta2) and length(theta2) is 0
theta2=linspace(0,2*pi,2*pi/360)
theta2 = 1×0 empty double row vector
Why does theta2 have a length of 0? you specified the number of points as:
2*pi/360
ans = 0.0175

KSSV
KSSV on 13 Oct 2021
Edited: KSSV on 13 Oct 2021
This line:
theta2=linspace(0,2*pi,2*pi/360);
gives you theta2 as an empty matrix, as you have used this for loop indexing, your code is not executing the loop and thus no plot and errors shown. Change that line to:
theta2=linspace(0,2*pi);
You will get plot and some errors. Try to rectify the errors using the error messages.
This is the complete working code but you need to rething on your code.
clc; clear all ;
%% Calculations for a 4 bar linkage
% Orgin
Ox=0;
Oy=0;
%% Link Lengths
L1=6;
L2=2;
L3=7;
L4=9;
RP=6;
% theta2=linspace(0,2*pi,2*pi/360);
theta2=linspace(0,2*pi);
%theta2=pi/4;
%for theta2=
%% K values
K1= L1/L2;
K2=L1/L4;
K3=(L2^2-L3^2+L4^2+L1^2)/(2*L2*L4);
K4= L1/L3;
K5= (L4^2-L3^2-L2^2-L1^2)/(2*L2*L3);
for i=1:length(theta2)
%% Calculate OTHER variables here.
A(i)= cos(theta2(i))-K1-K2*cos(theta2(i))+K3;
B(i)= -2*sin(theta2(i));
C(i)= K1-(K2+1)*cos(theta2(i))+K3;
D(i)= cos(theta2(i))-K1+K4*cos(theta2(i))+K5;
E(i)= -2*sin(theta2(i));
F(i)= K1+(K4-1)*cos(theta2(i))+K5;
%% Calculate your thetas
theta3(i)= 2*atan((-E-(E.^2-4*D.*F).^(1/2))/(2*D));
theta4(i) = 2*atan((-B+(B.^2-4*A.*C).^(1/2))/(2*A));
%theta_rp= theta2-theta3; or
theta_rp=30*(pi/180);
%% Determine the ABSOLUT position of the ENDS of each link (for plotting)
L1X(i)=L1;
L2X(i)= L2*cos(theta2(i));
L2Y(i)= L2*sin(theta2(i));
L3X(i)= L2X(i)+L3*cos(theta3(i));
L3Y(i)= L2Y(i)+L3*sin(theta3(i));
L4X(i)= L1-L4*sin(theta4(i));
L4Y(i)= L4*sin(theta4(i));
RPX(i)=L2X(i)+RP*cos(theta3(i)+theta_rp);
RPY(i)=L2Y(i)+RP*sin(theta3(i)+theta_rp);
%% Plot variables
line_width = 4.5;
marker_size = 9;
%% WHAT INDEX VALUE SHOULD THIS BE IF YOU WANT TO PLOT theta2 = 30 degrees?
%ii = pi/4; % CHANGE THIS NUMBER BUT USE IT TO CREATE THE CORRECT PLOT POSITION AS REQUIRED
figure(1)
hold on
plot([0 L1X(i)],[0 0],'-^k') % Plot the ground link as black with triangles at each end. Change the line width and markersize so the the link is visible
plot([0 L2X(i)],[0 L2Y(i)],'-or') % Plot the crank link as red with circles at each end. Change the line width and markersize so the the link is visible
plot([L2X(i) L3X(i)],[L2Y(i) L3Y(i)],'-og') % Plot the coupler link as green with circles at each end. Change the line width and markersize so the the link is visible
plot([L1X(i) L4X(i)],[0 L4Y(i)],'-ob') % Plot the output link as blue with circles at each end. Change the line width and markersize so the the link is visible
plot([L2X(i) RPX(i)],[L2Y(i) RPY(i)],'-om') % Plot ALL OF THE COUPLER POINTS
grid on;
drawnow;
hold off;
end

Community Treasure Hunt

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

Start Hunting!

Translated by