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:
Angles flipping at 0 and 180 degrees

Subject: Angles flipping at 0 and 180 degrees

From: Lydia_300

Date: 8 Jun, 2012 15:46:12

Message: 1 of 7

I am trying to calculate angles between two vectors. However when the angle reaches 0 or 180 degrees the answer is flipped and instead of increasing it decreases. Is there a code I can use to prevent this from happening? I am currently using the magnitudes of the vectors along with Acos to calculate the angles in radians and then convert them into degrees using the radtodeg function.

Any help with this would be much appreciated!

Subject: Angles flipping at 0 and 180 degrees

From: Nasser M. Abbasi

Date: 8 Jun, 2012 16:02:29

Message: 2 of 7

On 6/8/2012 10:46 AM, Lydia_300 wrote:
> However when the angle reaches 0 or 180 degrees the answer is flipped
....
>Is there a code I can use to prevent this from happening?

I doubt it. I think what you are seeing is becuase the cos
function is periodic and also cos(Pi+x) is the same as
-cos(x) so you see "things" flipping as one vector rotates?

I dont think it is possible to change this.

>I am currently using the magnitudes of the vectors along with Acos
>to calculate the angles in radians and then convert them into
>degrees using the radtodeg function.
>

I do not have radtodeg function.

EDU>> help radtodeg

radtodeg not found.

> Any help with this would be much appreciated!

--Nasser

Subject: Angles flipping at 0 and 180 degrees

From: Steven_Lord

Date: 8 Jun, 2012 17:06:52

Message: 3 of 7



"Lydia_300 " <angelwings300@hotmai.com> wrote in message
news:jqt6o4$9vl$1@newscl01ah.mathworks.com...
> I am trying to calculate angles between two vectors. However when the
> angle reaches 0 or 180 degrees the answer is flipped and instead of
> increasing it decreases. Is there a code I can use to prevent this from
> happening? I am currently using the magnitudes of the vectors along with
> Acos to calculate the angles in radians and then convert them into degrees
> using the radtodeg function.

Why not just use ACOSD?

http://www.mathworks.co.uk/help/techdoc/ref/acosd.html

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Angles flipping at 0 and 180 degrees

From: Star Strider

Date: 8 Jun, 2012 20:20:21

Message: 4 of 7



If you want the angle in all four quadrants (relative to your initial reference vector), there is a way. All vectors are from the origin here:

If v1 is your first vector defined as:

        v1 = [v1(1) v1(2)]'; % v1, v2 are both column vectors

with v2 defined similarly,

        a1r = atan2(v1(2),v1(1)); % radian angle of v1

        rotmtx = @(theta)([cos(theta) -sin(theta); sin(theta) cos(theta)]); % rotation matrix
        rota1r = rotmtx(-a1r); % rotation matrix to -a1r

        v1rot = rota1r * v1; % check rotated v1
        v2rot = rota1r * v2; % calculate rotated v2
        
Using atan2 on the rotated v2 vector will give you angles (0, pi) and (0, -pi). Add 2*pi to the negative angles to put all of them into (0, 2*pi).

I sketched this out to get you started, although I coded it in full to be sure it produced correct results. Experiment with it to see how it works. There may be better and more efficient methods, but this one was fun to work through.

Subject: Angles flipping at 0 and 180 degrees

From: Greg Heath

Date: 8 Jun, 2012 21:07:17

Message: 5 of 7

On Jun 8, 1:06

Subject: Angles flipping at 0 and 180 degrees

From: Matt J

Date: 9 Jun, 2012 10:28:07

Message: 6 of 7

"Lydia_300" wrote in message <jqt6o4$9vl$1@newscl01ah.mathworks.com>...
>
> I am trying to calculate angles between two vectors. However when the angle reaches 0 or 180 degrees the answer is flipped and instead of increasing it decreases. Is there a code I can use to prevent this from happening?
============

Why would you want to prevent it from happening? Once the angle reaches 0 or 180, the vectors start to become closer together again. It makes perfect physical sense that the angle starts to decrease.

Subject: Angles flipping at 0 and 180 degrees

From: Star Strider

Date: 9 Jun, 2012 17:26:07

Message: 7 of 7

> Why would you want to prevent it from happening? Once the angle reaches 0 or 180, the vectors start to become closer together again. It makes perfect physical sense that the angle starts to decrease.


Agreed, but my guess is that she needs to know where the second vector is relative to the first, not simply the included angle, and that's how she chose to define it.

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