Error with atan?

22 visualizaciones (últimos 30 días)
Neena
Neena el 6 de Jun. de 2012
Hi all,
I am wondering if atan is calculating the correct angle in my code. I want to calculate the angle of a segment with respect to the positive X axis. Even though the line with X and Y coordinates points along the negative Y axis, I get an angle of about 20 degrees from the atan computation. Can anyone tell me what is wrong.
% Input X and Y coordinates
X=[0 -0.1705 -0.1630 -0.0060 -0.0308];
Y=[0 -1.0382 -2.2907 -3.2725 -3.6321];
x1=[X;Y];
%the vector is the line that fits the first four points
poly=polyfit(x1(1,1:4),x1(2,1:4),1);
theta=atan(poly(1));
%this is the way I find the correct angle of rotation.
sumx=sum(x1(1,:));
if (sumx>0)
theta=-theta;
else
theta=-(pi+theta);
end
%computation of rotation angle
rot=[cos(theta) -sin(theta); sin(theta) cos(theta)];
x2=rot*x1;
plot(x1(1,:),x1(2,:))
hold on
plot(x2(1,:),x2(2,:),'*r')
I want x1 to point along the positive X axis after rotation.
Thanks for your help.
Neena
  2 comentarios
Walter Roberson
Walter Roberson el 6 de Jun. de 2012
Consider using atan2()
Neena
Neena el 7 de Jun. de 2012
Hi Walter. I still need to fit a line to the first four points...How can I incorporate atan2?

Iniciar sesión para comentar.

Respuestas (2)

Wayne King
Wayne King el 6 de Jun. de 2012
As Walter states in his comment, I'm guessing you should use atan2() so you can tell the difference between (1,-1) and (-1,1)
atan2(-1,1) % negative y-value, positive x-value
atan2(1,-1) % positive y-value, negative x-value
Note that you aren't able to get that distinction with atan()
atan(-1/1) % negative y-value, positive x-value
atan(1/-1) % positive y-value, negative x-value
  1 comentario
Neena
Neena el 7 de Jun. de 2012
Thanks Wayne. How can I incorporate atan2 in my code using the slope of the fitted line. If I remember the syntax, dont you need two inputs for atan2?
Thanks for the help.
Neena

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 7 de Jun. de 2012
Your X coordinates are not monotonic. Your line series does not "point" in any particular direction.
The 2nd, 3rd, and 4th coordinates are below and left of the 1st coordinates, When examined in increasing X order, the implication is you are going from negative Y towards 0 Y, which is a positive slope, generating a positive angle.
The fitting over the first 4 points is different from the fitting over all 5 points, but when you are doing the angle fix-up you are examining all 5 points. That is why your fix-up does not detect that your fitted line should point downwards for your purpose.
  1 comentario
Neena
Neena el 7 de Jun. de 2012
I get it. Can you suggest a better way to do this. I dont have much control over the co-ordinates X and Y. They are generated by one of the softwares purchased by the organization.

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by