why does the figure doesnt show the two vectors?

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_real = 1×301
1.0000 0.3624 -0.7374 -0.8968 0.0875 0.9602 0.6084 -0.5193 -0.9847 -0.1943 0.8439 0.8059 -0.2598 -0.9942 -0.4607 0.6603 0.9392 0.0204 -0.9245 -0.6903 0.4242 0.9977 0.2989 -0.7811 -0.8650 0.1543 0.9768 0.5536 -0.5756 -0.9707
A_imag=imag(RA)
A_imag = 1×301
0 0.9320 0.6755 -0.4425 -0.9962 -0.2794 0.7937 0.8546 -0.1743 -0.9809 -0.5366 0.5921 0.9657 0.1078 -0.8876 -0.7510 0.3433 0.9998 0.3813 -0.7235 -0.9056 0.0672 0.9543 0.6244 -0.5018 -0.9880 -0.2143 0.8328 0.8178 -0.2401
plot(A_real,A_imag)
hold on
P_real=real(RP)
P_real = 1×301
-3.8675 2.9171 -5.5663 -1.7785 -4.8323 1.6248 3.3844 1.8513 -1.5249 -2.4793 -0.2238 0.0130 4.5774 -2.5654 4.4553 5.4299 0.2248 2.6124 3.0542 -2.5154 -3.9619 -3.8939 -3.5367 -5.6547 -1.6590 -0.4626 5.1432 5.1124 -0.0507 1.6366
P_imag=imag(RP)
P_imag = 1×301
1.1434 -3.3660 1.9724 4.4791 -0.1042 4.6762 -3.3649 5.2569 -5.1451 3.4664 -5.4213 5.5288 -0.2998 4.8545 -1.8001 0.7494 -4.6054 -3.2759 -2.6469 -5.3785 1.4949 1.1027 -2.2532 -0.4928 4.4348 3.9738 2.5499 -1.2209 5.7901 -4.5064
plot(P_real,P_imag)

Réponses (2)

Les Beckham
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

Im expecting a circular shape for RA and more like an Ellipse shape for RP
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?

Connectez-vous pour commenter.

dpb
dpb le 2 Nov 2023
Modifié(e) : dpb le 2 Nov 2023
%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

Im expecting a circular shape for RA and more like an Ellipse shape for RP
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])
dpb
dpb le 2 Nov 2023
@Voss -- thanks, I got interrupted before I got a chance to look more to see if that was the only place -- maybe the alternative fix would be to simply remove the calls to rad2deg() unless the angles are being displayed in those units elsewhere.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Produits

Version

R2022a

Commenté :

dpb
le 2 Nov 2023

Community Treasure Hunt

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

Start Hunting!

Translated by