i want to plot two coordinate positions (complex)
Show older comments
Im working on a project (analysis of four bar linkages) i have two vectors that i would like to plot but dont know how. the vectors i wish to plot are R_A and R_P. shown below my code:
clc, clear
global L1 L2 L3 L4 AP delta
L1=5;
L2=1;
L3=5;
L4=7;
AP=5;
delta=50;
% Because we want theta2 to range from 0-360 (1rev) and the increment will be 360/200
theta2=0:360/200:360;
theta1=0;
%loop variables
R2=L2*exp(1i*deg2rad(theta2));
%R3=L3*exp(1i*deg2rad(theta3));
%R4=L4*exp(1i*deg2rad(theta4));
R1=L1*exp(1i*deg2rad(theta1));
Z=R1-R2;
Z_con=conj(Z);
%variables of the quadratic equation
a=L4*Z_con;
b=Z.*Z_con+L4^2-L3^2;
c=Z*L4;
%solution of the equation
T=roots([a b c]);
S=(L4*T+Z)/L3;
%angular positions
theta4=rad2deg(angle(T));
theta3=rad2deg(angle(S));
theta_AP=theta3-delta;
%path coordinates of A and P
R3=L3*exp(1i*deg2rad(theta3));
R4=L4*exp(1i*deg2rad(theta4)); %not needed
R_AP=AP*exp(1i*deg2rad(theta_AP));
R_A=R2;
R_P=R2+R_AP;
size(R_A)
size(R_P)
1 Comment
Do you mean like this?
L1=5;
L2=1;
L3=5;
L4=7;
AP=5;
delta=50;
% Because we want theta2 to range from 0-360 (1rev) and the increment will be 360/200
theta2=0:360/200:360;
theta1=0;
%loop variables
R2=L2*exp(1i*deg2rad(theta2));
%R3=L3*exp(1i*deg2rad(theta3));
%R4=L4*exp(1i*deg2rad(theta4));
R1=L1*exp(1i*deg2rad(theta1));
Z=R1-R2;
Z_con=conj(Z);
%variables of the quadratic equation
a=L4*Z_con;
b=Z.*Z_con+L4^2-L3^2;
c=Z*L4;
%solution of the equation
T=roots([a b c]);
S=(L4*T+Z)/L3;
%angular positions
theta4=rad2deg(angle(T));
theta3=rad2deg(angle(S));
theta_AP=theta3-delta;
%path coordinates of A and P
R3=L3*exp(1i*deg2rad(theta3));
R4=L4*exp(1i*deg2rad(theta4)); %not needed
R_AP=AP*exp(1i*deg2rad(theta_AP));
R_A=R2;
R_P=R2+R_AP;
size(R_A)
size(R_P)
plot(R_A,R_P)
Answers (1)
Vidhi Agarwal
on 1 Oct 2024
Edited: Vidhi Agarwal
on 1 Oct 2024
To plot the vectors “R_A” and “R_P” from your four-bar linkage analysis project, you need to ensure that both vectors have compatible dimensions. In provided code snippet there seems a mismatch in the dimensions of “R_A” and “R_P”. ones the dimensions are consistent using “plot” function you can plot the vectors.
Below are the changes in code snippet to resolve the issue of mismatched vector size:
% Initialize T and S
T = zeros(size(theta2));
S = zeros(size(theta2));
% Solve the quadratic equation for each theta2
for k = 1:length(theta2)
% Coefficients for the quadratic equation
a = L4 * Z_con(k);
b = Z(k) * Z_con(k) + L4^2 - L3^2;
c = Z(k) * L4;
% Solve the quadratic equation
roots_result = roots([a, b, c]);
% Choose the correct root based on your problem's context
% Here, we arbitrarily select the first root
T(k) = roots_result(1);
S(k) = (L4 * T(k) + Z(k)) / L3;
end
Below is the code snippet to plot the vectors:
% Plotting
figure;
plot(real(R_A), imag(R_A), 'b', 'LineWidth', 2); % Plot R_A
hold on;
plot(real(R_P), imag(R_P), 'r', 'LineWidth', 2); % Plot R_P
title('Path of Points A and P');
xlabel('X Coordinate');
ylabel('Y Coordinate');
legend('Path of A', 'Path of P');
grid on;
axis equal;
hold off;
And output for the same will look like:

For better understanding of “plot” function refer to the following documentation:
Hope that Helps!
Categories
Find more on Physics 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!