Why the mod function does not work

8 vues (au cours des 30 derniers jours)
Eliska Paulikova
Eliska Paulikova le 22 Nov 2022
My code started with 0 degree but when it gets only 180 and than it get 170, 160 and so on ... I would like it to 2pi (360 degrees)
for f=1:h
t=(f-1)*k/20;
m=f+1;
P0=[70 70]; %střed [x,y]
P1=[htabulka(1,1),htabulka(1,2)]; %XY1 [x,y]
P2=[htabulka(f,1),htabulka(f,2)]; %XY2 [x,y]
%angstn=wrapTo2Pi(atan2(abs((htabulka(1,1)-60)*(htabulka(f,2)-60)-(htabulka(f,1)-60)*(htabulka(1,2)-60)),(htabulka(1,1)-60)*(htabulka(f,1)-60)+(htabulka(1,2)-60)*(htabulka(f,2)-60)));
angstn =mod(atan2(norm(det([P2-P0;P1-P0])),dot(P2-P0,P1-P0)),2*pi);
%angst= angstn * (angstn >= 0) + (angstn + 2 * pi) * (angstn < 0);
ang=angstn*180/pi
cosf=cos(ang);
  2 commentaires
Eliska Paulikova
Eliska Paulikova le 22 Nov 2022
This is the output
Torsten
Torsten le 22 Nov 2022
Is it better what you get from this code ?
for f=1:h
t=(f-1)*k/20;
m=f+1;
P1=[70 70]; %střed [x,y]
P2=[htabulka(1,1),htabulka(1,2)]; %XY1 [x,y]
P3=[htabulka(f,1),htabulka(f,2)]; %XY2 [x,y]
angstn = atan2(P3(2) - P1(2), P3(1) - P1(1)) - atan2(P2(2) - P1(2), P2(1) - P1(1));
if angstn < 0
angstn = angstn + 2*pi;
end
ang=angstn*180/pi
cosf=cos(ang);

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 22 Nov 2022
angstn =mod(atan2(norm(det([P2-P0;P1-P0])),dot(P2-P0,P1-P0)),2*pi);
norm() is always non-negative, so you are taking atan2() in the case where y is positive and x is potentially either positive or negative. That is always going to give you a result between 0 and pi, so the mod() against 2*pi is always going to just return the same value.

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by