Why the mod function does not work

조회 수: 6 (최근 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