Why the mod function does not work

3 ビュー (過去 30 日間)
Eliska Paulikova
Eliska Paulikova 2022 年 11 月 22 日
回答済み: Walter Roberson 2022 年 11 月 22 日
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 件のコメント
Eliska Paulikova
Eliska Paulikova 2022 年 11 月 22 日
This is the output
Torsten
Torsten 2022 年 11 月 22 日
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);

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 11 月 22 日
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.

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by