Asked by KA
on 12 Jun 2011

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

Answer by Paulo Silva
on 12 Jun 2011

Accepted Answer

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

Answer by Daniel Svedbrand
on 14 Sep 2018

Adding mod 2*pi to atan2 should work just fine

shit = mod(atan2(y,x),2*pi);

Stephen Cobeldick
on 27 Oct 2018

+1 simple and efficient. Someone sees the wood regardless of the trees!

Sign in to comment.

Answer by Walter Roberson
on 12 Jun 2011

KA
on 12 Jun 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.

Paulo Silva
on 12 Jun 2011

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
on 12 Jun 2011

ok, did not know that, thanks again

Sign in to comment.

Answer by Ali
on 9 Jan 2015

Sign in to comment.

Answer by Kent Leung
on 21 Mar 2018

Edited by 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=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

Sign in to comment.

Answer by 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

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## wenjun kou (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/9330-changing-the-atan-function-so-that-it-ranges-from-0-to-2-pi#comment_435339

## Stephen Cobeldick (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/9330-changing-the-atan-function-so-that-it-ranges-from-0-to-2-pi#comment_628832

Sign in to comment.