why does it not plot the function?
显示 更早的评论
hi! im trying to plot the real(x) and imaginary parts(y) of R_P. but the plot doesnt show any points in the graph. here is my code:
clc, clear
% Values from table for even ID number
global L1 L2 L3 L4 AP delta theta1
L1=5;
L2=1;
L3=5;
L4=7;
AP=5;
delta=50;
theta1=0;
for i=0:360/200:360
%loop variables
R2=L2*exp(1i*deg2rad(i));
%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;
T=(-b+sqrt(-(b.^2)-(4.*a.*c)))/(2.*a);
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));
%assuming that the global axis is at point O2
R_A=R2;
R_P=R2+R_AP;
end
x=real(R_P)
y=imag(R_P)
plot(x,y)
there is no error shown. thank you!
1 个评论
Dyuman Joshi
2023-11-1
Remove this line, as it is redundant. And it is best to avoid the use of global variables.
global L1 L2 L3 L4 AP delta theta1
回答(1 个)
Each iteration of the for loop, you are overwriting the value of R_P, so after the loop R_P is a scalar (i.e., it only has one element: the value from the final iteration of the loop).
(Arrays with only one element don't show up when you plot them, unless you use a data marker, so that's why the plot doesn't show up.)
To store the values from all iterations, make R_P a vector and store one element in it each time through the loop.
Something like this:
% Values from table for even ID number
global L1 L2 L3 L4 AP delta theta1
L1=5;
L2=1;
L3=5;
L4=7;
AP=5;
delta=50;
theta1=0;
vals = 0:360/200:360;
n_vals = numel(vals);
R_P = zeros(1,n_vals);
for i=1:n_vals
%loop variables
R2=L2*exp(1i*deg2rad(vals(i)));
%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;
T=(-b+sqrt(-(b.^2)-(4.*a.*c)))/(2.*a);
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));
%assuming that the global axis is at point O2
R_A=R2;
R_P(i)=R2+R_AP;
end
x=real(R_P)
y=imag(R_P)
plot(x,y)
2 个评论
shamma aljaberi
2023-11-1
Voss
2023-11-1
You're welcome! Any questions, let me know. Otherwise, please "Accept" this answer. Thanks!
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

