Error with atan?
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (2)
Wayne King
il 6 Giu 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
Walter Roberson
il 7 Giu 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.
Vedere anche
Categorie
Scopri di più su Line Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!