Changing the atan function so that it ranges from 0 to 2*pi

346 ビュー (過去 30 日間)
KA
KA 2011 年 6 月 12 日
コメント済み: theodore panagos 2023 年 8 月 6 日
I know that the matlab atan function returns values in the range of -pi/2 to pi/2. How do i change it so that it goes over the full range 0 to 2*pi?
My first attempt was using a while loop, but it was incorrect.
I need to write a function mfile to set the built-in matlab function atan in the range of 0 to 2*pi without using atan2. im new to matlab so im unsure of what to do.
Thank you
  2 件のコメント
wenjun kou
wenjun kou 2017 年 3 月 8 日
編集済み: wenjun kou 2017 年 3 月 8 日
Although you don't want to use atan2, I thought I might just put this out there since atan2 returns a range between -pi to pi:
a = atan2(y, x);
a = a .* (a >= 0) + (a + 2 * pi) .* (a < 0);
Stephen23
Stephen23 2018 年 10 月 27 日
See Daniel Svedbrand's answer for the simplest solution.

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

採用された回答

Daniel Svedbrand
Daniel Svedbrand 2018 年 9 月 14 日
編集済み: John D'Errico 2023 年 8 月 3 日
Adding mod 2*pi to atan2 should work just fine
z = mod(atan2(y,x),2*pi);
  6 件のコメント
Feruza Amirkulova
Feruza Amirkulova 2023 年 8 月 3 日
Yes, mod(atan2(y,x),2*pi) worked and its gradients are the same as for (atan2(y,x)).
John D'Errico
John D'Errico 2023 年 8 月 3 日
Edited to remove profanity in the answer.

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

その他の回答 (4 件)

Walter Roberson
Walter Roberson 2011 年 6 月 12 日
Use atan2() instead.
  5 件のコメント
Paulo Silva
Paulo Silva 2011 年 6 月 12 日
I didn't include that statement on purpose, when none of the others if statements are true the value of v is NaN, you could also do this:
if isnan(v)
error('Arguments must be different from zero')
end
KA
KA 2011 年 6 月 12 日
ok, did not know that, thanks again

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


Paulo Silva
Paulo Silva 2011 年 6 月 12 日
The Wikipedia got all explained, you just need to do the code, it's very simple.
function v=myatan(y,x)
if nargin==1 %just in case the user only gives the value of y myatan(y)
x=1;
end
v=nan;
if x>0
v=atan(y/x);
end
if y>=0 & x<0
v=pi+atan(y/x);
end
if y<0 & x<0
v=-pi+atan(y/x);
end
if y>0 & x==0
v=pi/2;
end
if y<0 & x==0
v=-pi/2;
end
if v<0
v=v+2*pi;
end
end
  2 件のコメント
KA
KA 2011 年 6 月 12 日
thanks, very helpful
Mehmet Can Türk
Mehmet Can Türk 2022 年 4 月 9 日
編集済み: Mehmet Can Türk 2022 年 4 月 9 日
I checked the Wikipedia link and tested the code. First of all, thank you so much for the contribution.
I wanted to convert atan2 function from Matlab into another environment which supports only atan function. So I deleted the if block and everything worked perfectly.

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


Kent Leung
Kent Leung 2018 年 3 月 21 日
編集済み: Kent Leung 2018 年 3 月 21 日
Better late than never. (Also posting as a future reference to myself.) The function below accepts y & x as vectors in Matlab. Rather than using 'if' statements, the below might be faster if there is some parallelization implemented in the built-in index searching.
Note: I have a slight disagreement with the above for the x>0 & y<0 case, as well as the for x=0 & y<0 case. The code below gives 0 to 2pi.
function v=myatan(y,x)
%---returns an angle in radians between 0 and 2*pi for atan
v=zeros(size(x));
v(x>0 & y>=0) = atan( y(x>0 & y>=0) ./ x(x>0 & y>=0) );
v(x>0 & y<0) = 2*pi+atan( y(x>0 & y<0) ./ x(x>0 & y<0) );
v(x<0 & y>=0) = pi+atan( y(x<0 & y>=0) ./ x(x<0 & y>=0) );
v(x<0 & y<0) = pi+atan( y(x<0 & y<0) ./ x(x<0 & y<0) );
v(x==0 & y>=0) = pi/2;
v(x==0 & y<0) = 3/2*pi;
end
  1 件のコメント
Sharad Keshari
Sharad Keshari 2020 年 11 月 26 日
Thank you very much. Your code was helpful.

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


theodore panagos
theodore panagos 2018 年 10 月 27 日
You can use the formula:
atan(x,y)=pi-pi/2*(1+sgn(x))*(1-sgn(y^2))-pi/4*(2+sgn(x))*sgn(y) -sgn(x*y)*atan((abs(x)-abs(y))/(abs(x)+abs(y)))
x=x2-x1 and y=y2-y1
  1 件のコメント
theodore panagos
theodore panagos 2023 年 8 月 6 日
atan2(x,y)=pi/2*(1-sign(x))*(1-sgn(y^2))+pi()/4*(2-sgn(x))*sign(y)-sign(x*y)*atan((abs(x)-abs(y))/(abs(x)+abs(y)))

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

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by