MATLAB: How can I make atan output unlimited

atanMATLAB and Simulink Student Suite

Hi guys, I'm having a bit of a mathematical/programming problem here, I hope u could help me. somewhere in my simulink files when I'm trying to solve a control problem. here goes a simple description. I've got something that is equal to sin(theta) and something that is equal to cos(theta). in other words I have two equations sin(theta)=y and cos(theta)=x. both equations can be solved at the same time by diving that we get tan(theta)=y/x. so when u want to find theta, u'll have; theta=atan(y/x) the only problem is that as I know the system, theta goes from zero to ascending to infinity (if u don't shut it down) but the output of atan function is limited between -pi/2 and pi/2. so what should I do yo let it ascend after pi/2 till pi, 2*pi, 3pi and so on? I need the output to be continuous. thanks in advance.

Best Answer

You need to use the atan2 function. If you want the angles it reports to be continuous from 0 to 2*pi or to 360°, use one of these anonymous functions:
Angles360 = @(a) rem(360+a, 360); % For ‘atan2d’
Angles2pi = @(a) rem(2*pi+a, 2*pi); % For ‘atan2’