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

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 commentaires

wenjun kou
wenjun kou le 8 Mar 2017
Modifié(e) : wenjun kou le 8 Mar 2017
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);
See Daniel Svedbrand's answer for the simplest solution.

Connectez-vous pour commenter.

 Réponse acceptée

Daniel Svedbrand
Daniel Svedbrand le 14 Sep 2018
Modifié(e) : John D'Errico le 3 Août 2023
Adding mod 2*pi to atan2 should work just fine
z = mod(atan2(y,x),2*pi);

6 commentaires

Stephen23
Stephen23 le 27 Oct 2018
Modifié(e) : Stephen23 le 27 Oct 2018
+1 simple and efficient. Someone sees the wood regardless of the trees!
+1, worked for me.
perfect and easy , thank you indeed
This is great! Thank you!
Yes, mod(atan2(y,x),2*pi) worked and its gradients are the same as for (atan2(y,x)).
Edited to remove profanity in the answer.

Connectez-vous pour commenter.

Plus de réponses (5)

Walter Roberson
Walter Roberson le 12 Juin 2011
Use atan2() instead.

5 commentaires

KA
KA le 12 Juin 2011
i am not allowed to use atan2(), which is why im having difficulty
Why the heck not? Why is atan OK but atan2 not? Is this one of those homework problems where the instructor asks you to basically replicate a built-in MATLAB function from lower level functions (in which case Paulo did your homework for you)?
KA
KA le 12 Juin 2011
yes it is one of those "homework problems," and no Paulo did not do my homework, instead it helped me better understand how matlab works, and going through his code i noticed he was missing another if statement which is:
if y==0 && x==0
error('Invalid computation')
end
I also tried doing it a different way by using the derivation of atan2 on the wikipedia link he sent. So his answer actually helped me understand matlab, since it is my first time having to write a program in Matlab.
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 le 12 Juin 2011
ok, did not know that, thanks again

Connectez-vous pour commenter.

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 commentaires

KA
KA le 12 Juin 2011
thanks, very helpful
Mehmet Can Türk
Mehmet Can Türk le 9 Avr 2022
Modifié(e) : Mehmet Can Türk le 9 Avr 2022
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.

Connectez-vous pour commenter.

theodore panagos
theodore panagos le 27 Oct 2018
Modifié(e) : DGM le 17 Oct 2024
You can use the formula:
x = x2-x1;
y = y2-y1;
th = pi/2*(1-sign(x))*(1-sign(y^2)) + pi/4*(2-sign(x))*sign(y) - sign(x*y)*atan((abs(x)-abs(y))/(abs(x)+abs(y)));

1 commentaire

To demonstrate for nonscalar inputs:
% fake xy data
x = randn(10,1);
y = randn(10,1);
% the reference
th0 = atan2(y,x);
% the given implementation
th = pi/2*(1 - sign(x)).*(1 - sign(y.^2)) ...
+ pi/4*(2 - sign(x)).*sign(y) ...
- sign(x.*y).*atan((abs(x)-abs(y))./(abs(x)+abs(y)));
% compare
immse(th0,th)
ans = 1.5484e-32

Connectez-vous pour commenter.

Kent Leung
Kent Leung le 21 Mar 2018
Modifié(e) : Kent Leung le 21 Mar 2018
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
Ali Ali
Ali Ali le 7 Juin 2024
Modifié(e) : Ali Ali le 7 Juin 2024
If you want to use atan2(y,x) (atan2(Y,X), returns values in the closed interval [-pi,pi]), considering that atan(b)=atan(b+pi), you can use this equation (use atan2(Y,X) instead of atan(y/x) in this equation) for your work.

2 commentaires

what?
atan(b) ~= atan(b+pi)
atan(b) ~= atan(b)+pi
atan2(y,x) ~= atan2(y,x)+pi
The angle between the x-axis and a unit vector along x is 0 degrees, not 90 degrees.
atan2d(0,1) + 90 % NO
ans = 90
mod(atan2d(0,1),360) % YES
ans = 0
" atan(b)=atan(b+pi) "
Lets check that right now:
b = linspace(-5,5,100);
X = atan(b);
Y = atan(b+pi);
plot(b(:),[X(:),Y(:)])
Nope, not the same. Not even close.

Connectez-vous pour commenter.

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by