http://www.mathworks.com/matlabcentral/newsreader/view_thread/304731
MATLAB Central Newsreader  Calculate angle on 0360 scale from positive and negatives x and y vectors.
Feed for thread: Calculate angle on 0360 scale from positive and negatives x and y vectors.
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Sun, 20 Mar 2011 01:05:04 +0000
Calculate angle on 0360 scale from positive and negatives x and y vectors.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/304731#826115
Thomas
Hello,<br>
<br>
I've got matrices of x and y vectors that changes over many timesteps. Currently they are in terms of a coordinate system where the yaxis is north and the xaxis is east. I'd like to rotate them into another coordinate system where my yaxis is about 50 degrees west of north (i.e. rotated 50 degrees counterclockwise) in order to calculate new x and y components for an analysis I'm doing.<br>
<br>
The easy way to shift angles (as a precursor to calculating the new component vector values) seems to be first calculating the original angle of the resultant of the x and y vectors in terms of the unit circle, on the 0 to 360 degree or 0 to 2pi scale. Then I can simply subtract my 50 degree rotation from all angles and take the sin and cosine times magnitude to get the new components.<br>
<br>
Since Matlab's trig functions don't take into account the signs of my input x and y components, it can't tell where on the unit circle the angle would fall and gives a value between 90 and 90. I could search every matrix (78 x 93) at every timestep (of thousands) to find the quadrant each angle falls in and use if statements to calculate the appropriate 0360 angle, but I imagine that would be prohibitively slow.<br>
<br>
Can anybody think of an easier or faster solution? Otherwise I'll have to try and find a 180degree arc in which all my resultant vectors always fall in order to restrict all angles to the 90 to 90 range, and I'm sure that holds for all of my timesteps. Thanks in advance!<br>
<br>
Tom

Sun, 20 Mar 2011 02:13:04 +0000
Re: Calculate angle on 0360 scale from positive and negatives x and y vectors.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/304731#826117
Roger Stafford
"Thomas " <wandernmann@gmail.com> wrote in message <im3js0$it6$1@fred.mathworks.com>...<br>
> Hello,<br>
> <br>
> I've got matrices of x and y vectors that changes over many timesteps. Currently they are in terms of a coordinate system where the yaxis is north and the xaxis is east. I'd like to rotate them into another coordinate system where my yaxis is about 50 degrees west of north (i.e. rotated 50 degrees counterclockwise) in order to calculate new x and y components for an analysis I'm doing.<br>
> <br>
> The easy way to shift angles (as a precursor to calculating the new component vector values) seems to be first calculating the original angle of the resultant of the x and y vectors in terms of the unit circle, on the 0 to 360 degree or 0 to 2pi scale. Then I can simply subtract my 50 degree rotation from all angles and take the sin and cosine times magnitude to get the new components.<br>
> <br>
> Since Matlab's trig functions don't take into account the signs of my input x and y components, it can't tell where on the unit circle the angle would fall and gives a value between 90 and 90. I could search every matrix (78 x 93) at every timestep (of thousands) to find the quadrant each angle falls in and use if statements to calculate the appropriate 0360 angle, but I imagine that would be prohibitively slow.<br>
> <br>
> Can anybody think of an easier or faster solution? Otherwise I'll have to try and find a 180degree arc in which all my resultant vectors always fall in order to restrict all angles to the 90 to 90 range, and I'm sure that holds for all of my timesteps. Thanks in advance!<br>
> <br>
> Tom<br>
           <br>
If you mean you are rotating the coordinate system while keeping the points fixed, the transformation is just this:<br>
<br>
newx = x*cosd(50) + y*sind(50);<br>
newy = x*sind(50) + y*cosd(50);<br>
<br>
If you mean something else please let us know. Your description of what you tried seems rather confusing.<br>
<br>
Roger Stafford

Sun, 20 Mar 2011 03:28:04 +0000
Re: Calculate angle on 0360 scale from positive and negatives x and y vectors.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/304731#826121
Roger Stafford
"Roger Stafford" wrote in message <im3nrg$i68$1@fred.mathworks.com>...<br>
> ........<br>
> newx = x*cosd(50) + y*sind(50);<br>
> newy = x*sind(50) + y*cosd(50);<br>
> .......<br>
        <br>
Addendum: If in the future you really do need the angle of a line from the origin to a point (x,y) with respect to the xaxis that ranges from pi to +pi, just use this:<br>
<br>
a = atan2(y,x); % Angle in radians<br>
<br>
If you want it to range between 0 and 2*pi, just do this:<br>
<br>
a = mod(atan2(y,x),2*pi);<br>
<br>
Roger Stafford

Sun, 20 Mar 2011 22:38:04 +0000
Re: Calculate angle on 0360 scale from positive and negatives x and y vectors.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/304731#826253
Thomas
"Roger Stafford" wrote in message <im3s84$nef$1@fred.mathworks.com>...<br>
> "Roger Stafford" wrote in message <im3nrg$i68$1@fred.mathworks.com>...<br>
> > ........<br>
> > newx = x*cosd(50) + y*sind(50);<br>
> > newy = x*sind(50) + y*cosd(50);<br>
> > .......<br>
>         <br>
> Addendum: If in the future you really do need the angle of a line from the origin to a point (x,y) with respect to the xaxis that ranges from pi to +pi, just use this:<br>
> <br>
> a = atan2(y,x); % Angle in radians<br>
> <br>
> If you want it to range between 0 and 2*pi, just do this:<br>
> <br>
> a = mod(atan2(y,x),2*pi);<br>
> <br>
> Roger Stafford<br>
<br>
Roger,<br>
<br>
Ahh...atan2 and mod were just the functions I was looking for, although it looks like your initial correction would work fine too. Sorry for the confusion, and thanks for the help!<br>
<br>
Tom

Sun, 20 Mar 2011 23:27:36 +0000
Re: Calculate angle on 0360 scale from positive and negatives x and y vectors.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/304731#826260
Ralph Schleicher
"Thomas " <wandernmann@gmail.com> writes:<br>
<br>
>> If you want it to range between 0 and 2*pi, just do this:<br>
>><br>
>> a = mod(atan2(y,x),2*pi);<br>
>><br>
>> Roger Stafford<br>
<br>
> Ahh...atan2 and mod were just the functions I was looking for,<br>
<br>
You should not use 'mod' in combination with multiples of pi because<br>
it may result in ulp errors.<br>
<br>
a = atan2(y, x);<br>
tem = (a < 0);<br>
a(tem) = a(tem) + 2 .* pi;<br>
<br>
is the accurate form.<br>
<br>
>> x = (2*pi:pi/3:2*pi)';<br>
>> [x, sin(x) == sin(mod(x, 2*pi))]<br>
ans =<br>
<br>
6.2832 0<br>
5.236 0<br>
4.1888 0<br>
3.1416 0<br>
2.0944 0<br>
1.0472 0<br>
0 1<br>
1.0472 1<br>
2.0944 1<br>
3.1416 1<br>
4.1888 1<br>
5.236 1<br>
6.2832 0<br>
<br>
 <br>
Ralph Schleicher <<a href="http://ralphschleicher.de">http://ralphschleicher.de</a>><br>
<br>
Development * Consulting * Training<br>
Mathematical Modeling and Simulation<br>
Software Tools