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

489 views (last 30 days)
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

Sign in to comment.

Accepted Answer

Daniel Svedbrand
Daniel Svedbrand on 14 Sep 2018
Adding mod 2*pi to atan2 should work just fine
shit = mod(atan2(y,x),2*pi);

Sign in to comment.

More Answers (5)

Walter Roberson
Walter Roberson on 12 Jun 2011
Use atan2() instead.

Sign in to comment.

Paulo Silva
Paulo Silva on 12 Jun 2011
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)
if x>0
if y>=0 & x<0
if y<0 & x<0
if y>0 & x==0
if y<0 & x==0
if v<0
Mehmet Can Türk
Mehmet Can Türk on 9 Apr 2022
Edited: Mehmet Can Türk on 9 Apr 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.

Sign in to comment.

Ali on 9 Jan 2015
Hi all, I actually want to regenerate the results presented in figure 10 (part b) and figure 11 (part b) of this paper: Can you please help me for the case of 0 to 3*pi (or generally n*pi), namely for the n-fold complex plane? this figure might be also helpful to elucidate my question: Many thanks

Kent Leung
Kent Leung on 21 Mar 2018
Edited: Kent Leung on 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(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;

theodore panagos
theodore panagos on 27 Oct 2018
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

Community Treasure Hunt

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

Start Hunting!

Translated by