why does it not plot the function?

1 次查看(过去 30 天)
shamma aljaberi
shamma aljaberi 2023-11-1
评论: Voss 2023-11-1
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)
x = 4.4335
y=imag(R_P)
y = 3.6347
plot(x,y)
there is no error shown. thank you!
  1 个评论
Dyuman Joshi
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 个)

Voss
Voss 2023-11-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)
x = 1×201
4.4335 4.4617 4.4894 4.5163 4.5424 4.5678 4.5922 4.6156 4.6381 4.6595 4.6797 4.6988 4.7167 4.7333 4.7487 4.7627 4.7753 4.7866 4.7965 4.8049 4.8120 4.8176 4.8218 4.8245 4.8259 4.8258 4.8243 4.8214 4.8172 4.8116
y=imag(R_P)
y = 1×201
3.6347 3.6388 3.6420 3.6444 3.6460 3.6469 3.6470 3.6463 3.6450 3.6429 3.6402 3.6369 3.6330 3.6285 3.6234 3.6179 3.6119 3.6054 3.5986 3.5914 3.5838 3.5760 3.5678 3.5594 3.5508 3.5420 3.5330 3.5238 3.5146 3.5052
plot(x,y)
  2 个评论
Voss
Voss 2023-11-1
You're welcome! Any questions, let me know. Otherwise, please "Accept" this answer. Thanks!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Discrete Data Plots 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by