Clear Filters
Clear Filters

Error with atan?

8 views (last 30 days)
Neena
Neena on 6 Jun 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 Comments
Walter Roberson
Walter Roberson on 6 Jun 2012
Consider using atan2()
Neena
Neena on 7 Jun 2012
Hi Walter. I still need to fit a line to the first four points...How can I incorporate atan2?

Sign in to comment.

Answers (2)

Wayne King
Wayne King on 6 Jun 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 Comment
Neena
Neena on 7 Jun 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

Sign in to comment.


Walter Roberson
Walter Roberson on 7 Jun 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 Comment
Neena
Neena on 7 Jun 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.

Sign in to comment.

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!