why does the figure doesnt show the two vectors?
Afficher commentaires plus anciens
Hi! i am working on a project using the while loop (similar to my question regarding for loop) the figure doesnt show the plot as desired. what is wrong regarding the code?
code below:
%Links length
L1=5; L2=1; L3=5; L4=7; AP=5;
% ternary link angle (degree)
delta=50;
th2= 0:1.2:360;
range_th2=300;
RP=zeros(1,range_th2);
RA=zeros(1,range_th2);
i=1;
while i<=301
% the loop variables
R1=L1;
R2=L2*(cos(th2(i))+sin(th2(i))*1i);
Z=R1-R2;
Zconj=conj(Z);
% Quadratic equation parameters
a=L4.*Zconj;
b=Z.*Zconj+L4.^2-L3.^2;
c=L4*Z;
T=roots([a b c]);
T_pos=T(2);
%T_pos=(-b+sqrt(b.^2-4.*c.*a))/(2.*a);
S=(L4*T_pos+Z)/L4;
% solution
th3(i)=rad2deg(angle(T_pos));
th4(i)=rad2deg(angle(S));
R3=L3*(cos(th3(i))+sin(th3(i))*1i);
R4=L4*(cos(th4(i))+sin(th4(i))*1i);
thAP=th3(i)-delta;
RAP=AP*(cos(thAP)+sin(thAP)*1i);
%position of A and P
RA(i)=R2;
RP(i)=R2+RAP;
i=i+1;
end
A_real=real(RA)
A_imag=imag(RA)
plot(A_real,A_imag)
hold on
P_real=real(RP)
P_imag=imag(RP)
plot(P_real,P_imag)
Réponses (2)
Les Beckham
le 2 Nov 2023
Modifié(e) : Les Beckham
le 2 Nov 2023
It looks like it is working to me (see below). What are you expecting and how is that different from the results you are seeing?
%Links length
L1=5; L2=1; L3=5; L4=7; AP=5;
% ternary link angle (degree)
delta=50;
th2= 0:1.2:360;
range_th2=300;
RP=zeros(1,range_th2);
RA=zeros(1,range_th2);
i=1;
while i<=301
% the loop variables
R1=L1;
R2=L2*(cos(th2(i))+sin(th2(i))*1i);
Z=R1-R2;
Zconj=conj(Z);
% Quadratic equation parameters
a=L4.*Zconj;
b=Z.*Zconj+L4.^2-L3.^2;
c=L4*Z;
T=roots([a b c]);
T_pos=T(2);
%T_pos=(-b+sqrt(b.^2-4.*c.*a))/(2.*a);
S=(L4*T_pos+Z)/L4;
% solution
th3(i)=rad2deg(angle(T_pos));
th4(i)=rad2deg(angle(S));
R3=L3*(cos(th3(i))+sin(th3(i))*1i);
R4=L4*(cos(th4(i))+sin(th4(i))*1i);
thAP=th3(i)-delta;
RAP=AP*(cos(thAP)+sin(thAP)*1i);
%position of A and P
RA(i)=R2;
RP(i)=R2+RAP;
i=i+1;
end
A_real=real(RA);
A_imag=imag(RA);
plot(A_real,A_imag);
hold on
P_real=real(RP);
P_imag=imag(RP);
plot(P_real,P_imag)
axis equal
2 commentaires
shamma aljaberi
le 2 Nov 2023
Les Beckham
le 2 Nov 2023
I edited my answer to add "axis equal" so the x and y scales are equal. They are both pretty circular in shape. Have you double checked the equations?
%Links length
L1=5; L2=1; L3=5; L4=7; AP=5;
% ternary link angle (degree)
delta=50;
th2= 0:1.2:360;
range_th2=300;
RP=zeros(1,range_th2);
RA=zeros(1,range_th2);
i=1;
while i<=301
% the loop variables
R1=L1;
R2=L2*(cos(th2(i))+sin(th2(i))*1i);
Z=R1-R2;
Zconj=conj(Z);
% Quadratic equation parameters
a=L4.*Zconj;
b=Z.*Zconj+L4.^2-L3.^2;
c=L4*Z;
T=roots([a b c]);
T_pos=T(2);
%T_pos=(-b+sqrt(b.^2-4.*c.*a))/(2.*a);
S=(L4*T_pos+Z)/L4;
% solution
th3(i)=rad2deg(angle(T_pos));
th4(i)=rad2deg(angle(S));
R3=L3*(cos(th3(i))+sin(th3(i))*1i);
R4=L4*(cos(th4(i))+sin(th4(i))*1i);
thAP=th3(i)-delta;
RAP=AP*(cos(thAP)+sin(thAP)*1i);
%position of A and P
RA(i)=R2;
RP(i)=R2+RAP;
i=i+1;
end
A_real=real(RA);
A_imag=imag(RA);
plot(A_real,A_imag);
hold on
P_real=real(RP);
P_imag=imag(RP);
plot(P_real,P_imag);
axis equal
legend('A','P')
figure, subplot(2,1,1) % look versus order instead
plot(A_real), hold on, plot(A_imag)
xlim([1 100])
legend('A_R','A_I')
subplot(2,1,2) % look versus order instead
plot(P_real), hold on, plot(P_imag)
legend('P_R','P_I')
xlim([1 100])
What we observe is that the "A" traces are pretty clean sinusoids with just a phase difference; hence the real vs imaginary parts follow a decent trace as shown on the first.
On the other hand, the "P" traces are much more random appearing; movements aren't smooth from one observation to the next; hence the two plotted against each other reflect that...
What you were expecting is unknown to us...we don't know the problem statement.
4 commentaires
shamma aljaberi
le 2 Nov 2023
dpb
le 2 Nov 2023
Well, that'll all be dependent upon the path taken or the change in th based on the new solution each iteration.
One thing I notice that would make me wonder is
% solution
th3(i)=rad2deg(angle(T_pos));
th4(i)=rad2deg(angle(S));
R3=L3*(cos(th3(i))+sin(th3(i))*1i);
R4=L4*(cos(th4(i))+sin(th4(i))*1i);
in which you convert th from radians to degrees but then call sin/cos on them instead of using sind/cosd.
Are you sure that's what you intended?
Fixing the degree/radian units issue, based on @dpb's observation, produces a more interesting plot. Maybe that's closer to what's intended, @shamma aljaberi?
%Links length
L1=5; L2=1; L3=5; L4=7; AP=5;
% ternary link angle (degree)
delta=50;
th2= 0:1.2:360;
range_th2=300;
RP=zeros(1,range_th2);
RA=zeros(1,range_th2);
i=1;
while i<=301
% the loop variables
R1=L1;
R2=L2*(cos(th2(i))+sin(th2(i))*1i);
Z=R1-R2;
Zconj=conj(Z);
% Quadratic equation parameters
a=L4.*Zconj;
b=Z.*Zconj+L4.^2-L3.^2;
c=L4*Z;
T=roots([a b c]);
T_pos=T(2);
%T_pos=(-b+sqrt(b.^2-4.*c.*a))/(2.*a);
S=(L4*T_pos+Z)/L4;
% solution
th3(i)=rad2deg(angle(T_pos));
th4(i)=rad2deg(angle(S));
R3=L3*(cosd(th3(i))+sind(th3(i))*1i);
R4=L4*(cosd(th4(i))+sind(th4(i))*1i);
thAP=th3(i)-delta;
RAP=AP*(cosd(thAP)+sind(thAP)*1i);
%position of A and P
RA(i)=R2;
RP(i)=R2+RAP;
i=i+1;
end
A_real=real(RA);
A_imag=imag(RA);
plot(A_real,A_imag)
hold on
P_real=real(RP);
P_imag=imag(RP);
plot(P_real,P_imag)
axis equal
legend('A','P')
figure, subplot(2,1,1) % look versus order instead
plot(A_real), hold on, plot(A_imag)
xlim([1 100])
legend('A_R','A_I')
subplot(2,1,2) % look versus order instead
plot(P_real), hold on, plot(P_imag)
legend('P_R','P_I')
xlim([1 100])
Catégories
En savoir plus sur Mathematics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





