I have the following Matlab codes to generate a circle for a given radius Rand angle $\theta$,
m = 37;
for jj=1:m
theta(jj,:) = (2*pi/m).*jj;
end
R = 1;
x = R*cosd(theta);
y = R*sind(theta);
Now, suppose I want to back-calculate the angle $\theta$ from the [x,y]coordinate, what is the best way to do it? I have came up with the following code without success, where is the mistake?
% Get theta
for ii=1:length(x)
if y(ii)>=0 && x(ii)>=0
thetapred(ii) = 180+atand(y(ii)./x(ii));
elseif y(ii)>0 && x(ii)<0
thetapred(ii) = 360-atand(y(ii)./x(ii));
elseif y(ii)<0 && x(ii)<0
thetapred(ii) = atand(y(ii)./x(ii));
elseif y(ii)<0 && x(ii)>0
thetapred(ii) = 180-atand(y(ii)./x(ii));
end
end
Using $\arctan$ you should distinguish two main cases: $x>0$ and $x<0$ and then consider a part $x=0$.
As a simpler alternative you can calculate