Why the mod function does not work

11 views (last 30 days)
Eliska Paulikova
Eliska Paulikova on 22 Nov 2022
Answered: Walter Roberson on 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 Comments
Torsten
Torsten on 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);

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 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.

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by