Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Calculate angle on 0-360 scale from positive and negatives x and y vectors.

Subject: Calculate angle on 0-360 scale from positive and negatives x and y vectors.

From: Thomas

Date: 20 Mar, 2011 01:05:04

Message: 1 of 5

Hello,

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 y-axis is north and the x-axis is east. I'd like to rotate them into another coordinate system where my y-axis is about 50 degrees west of north (i.e. rotated 50 degrees counter-clockwise) in order to calculate new x and y components for an analysis I'm doing.

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.

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 0-360 angle, but I imagine that would be prohibitively slow.

Can anybody think of an easier or faster solution? Otherwise I'll have to try and find a 180-degree 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!

Tom

Subject: Calculate angle on 0-360 scale from positive and negatives x and y vectors.

From: Roger Stafford

Date: 20 Mar, 2011 02:13:04

Message: 2 of 5

"Thomas " <wandernmann@gmail.com> wrote in message <im3js0$it6$1@fred.mathworks.com>...
> Hello,
>
> 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 y-axis is north and the x-axis is east. I'd like to rotate them into another coordinate system where my y-axis is about 50 degrees west of north (i.e. rotated 50 degrees counter-clockwise) in order to calculate new x and y components for an analysis I'm doing.
>
> 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.
>
> 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 0-360 angle, but I imagine that would be prohibitively slow.
>
> Can anybody think of an easier or faster solution? Otherwise I'll have to try and find a 180-degree 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!
>
> Tom
- - - - - - - - - - - -
  If you mean you are rotating the coordinate system while keeping the points fixed, the transformation is just this:

 newx = x*cosd(50) + y*sind(50);
 newy = -x*sind(50) + y*cosd(50);

  If you mean something else please let us know. Your description of what you tried seems rather confusing.

Roger Stafford

Subject: Calculate angle on 0-360 scale from positive and negatives x and y vectors.

From: Roger Stafford

Date: 20 Mar, 2011 03:28:04

Message: 3 of 5

"Roger Stafford" wrote in message <im3nrg$i68$1@fred.mathworks.com>...
> ........
> newx = x*cosd(50) + y*sind(50);
> newy = -x*sind(50) + y*cosd(50);
> .......
- - - - - - - - -
  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 x-axis that ranges from -pi to +pi, just use this:

 a = atan2(y,x); % Angle in radians

If you want it to range between 0 and 2*pi, just do this:

 a = mod(atan2(y,x),2*pi);

Roger Stafford

Subject: Calculate angle on 0-360 scale from positive and negatives x and y vectors.

From: Thomas

Date: 20 Mar, 2011 22:38:04

Message: 4 of 5

"Roger Stafford" wrote in message <im3s84$nef$1@fred.mathworks.com>...
> "Roger Stafford" wrote in message <im3nrg$i68$1@fred.mathworks.com>...
> > ........
> > newx = x*cosd(50) + y*sind(50);
> > newy = -x*sind(50) + y*cosd(50);
> > .......
> - - - - - - - - -
> 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 x-axis that ranges from -pi to +pi, just use this:
>
> a = atan2(y,x); % Angle in radians
>
> If you want it to range between 0 and 2*pi, just do this:
>
> a = mod(atan2(y,x),2*pi);
>
> Roger Stafford

Roger,

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!

Tom

Subject: Calculate angle on 0-360 scale from positive and negatives x and y vectors.

From: Ralph Schleicher

Date: 20 Mar, 2011 23:27:36

Message: 5 of 5

"Thomas " <wandernmann@gmail.com> writes:

>> If you want it to range between 0 and 2*pi, just do this:
>>
>> a = mod(atan2(y,x),2*pi);
>>
>> Roger Stafford

> Ahh...atan2 and mod were just the functions I was looking for,

You should not use 'mod' in combination with multiples of pi because
it may result in ulp errors.

     a = atan2(y, x);
     tem = (a < 0);
     a(tem) = a(tem) + 2 .* pi;

is the accurate form.

>> x = (-2*pi:pi/3:2*pi)';
>> [x, sin(x) == sin(mod(x, 2*pi))]
ans =

     -6.2832 0
      -5.236 0
     -4.1888 0
     -3.1416 0
     -2.0944 0
     -1.0472 0
           0 1
      1.0472 1
      2.0944 1
      3.1416 1
      4.1888 1
       5.236 1
      6.2832 0

--
Ralph Schleicher <http://ralph-schleicher.de>

Development * Consulting * Training
Mathematical Modeling and Simulation
Software Tools

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us