Error with atan?

조회 수: 19 (최근 30일)
Neena
Neena 2012년 6월 6일
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
Walter Roberson
Walter Roberson 2012년 6월 6일
Consider using atan2()
Neena
Neena 2012년 6월 7일
Hi Walter. I still need to fit a line to the first four points...How can I incorporate atan2?

댓글을 달려면 로그인하십시오.

답변 (2개)

Wayne King
Wayne King 2012년 6월 6일
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
Neena
Neena 2012년 6월 7일
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

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2012년 6월 7일
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
Neena
Neena 2012년 6월 7일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by