Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Find angles between two vectors

Subject: Find angles between two vectors

From: Justin Morehouse

Date: 23 Jan, 2008 13:13:01

Message: 1 of 6

Hi there, I have a two vectors (3,5) and (5,6) and I was
wondering how do I get the angle between them in matlab.

on paper I would multiply both vectors to get (15+30) to =
45 and then square both (3,5) and (5,6) to get (9+25) and
(25+36). Then I would get the square root of both (34) and
(61) and multiply them together and then divide 45 by that.

Afterwhich I would then use a trusty calc to do cos theta
of that, but how can math lab do that?

Thanks!

Subject: Re: Find angles between two vectors

From: tpl@eng.cam.ac.uk (Tim Love)

Date: 23 Jan, 2008 13:34:44

Message: 2 of 6

"Justin Morehouse" <norman_batez@MSN.com> writes:

>Hi there, I have a two vectors (3,5) and (5,6) and I was
>wondering how do I get the angle between them in matlab.

There's a cart2pol function. Or you could use
angle(complex(5,6)-complex(3,5))

Subject: Re: Find angles between two vectors

From: us

Date: 23 Jan, 2008 13:53:02

Message: 3 of 6

"Justin Morehouse":
<SNIP many many words...

> Hi there, I have a two vectors (3,5) and (5,6) and I was
> wondering how do I get the angle between them in matlab.
> on paper I would multiply both vectors to get (15+30) to
=
> 45 and then square both (3,5) and (5,6) to get (9+25) and
> (25+36). Then I would get the square root of both (34)
and
> (61) and multiply them together and then divide 45 by
that.
> Afterwhich I would then use a trusty calc to do cos
theta
> of that, but how can math lab do that...

ML obeys your words... (as one of the solutions...)

     v1=[3,5];
     v2=[5,6];
     a=acosd(dot(v1,v2)/(norm(v1)*norm(v2)))

us

Subject: Re: Find angles between two vectors

From: Anh Huy Phan

Date: 23 Jan, 2008 14:08:02

Message: 4 of 6

"us " <us@neurol.unizh.ch> wrote in message
<fn7gru$7jd$1@fred.mathworks.com>...
> "Justin Morehouse":
> <SNIP many many words...
>
> > Hi there, I have a two vectors (3,5) and (5,6) and I
was
> > wondering how do I get the angle between them in matlab.
> > on paper I would multiply both vectors to get (15+30) to
> =
> > 45 and then square both (3,5) and (5,6) to get (9+25)
and
> > (25+36). Then I would get the square root of both (34)
> and
> > (61) and multiply them together and then divide 45 by
> that.
> > Afterwhich I would then use a trusty calc to do cos
> theta
> > of that, but how can math lab do that...
>
> ML obeys your words... (as one of the solutions...)
>
> v1=[3,5];
> v2=[5,6];
> a=acosd(dot(v1,v2)/(norm(v1)*norm(v2)))
>
> us

The same question was posted and available at the following
address

http://www.mathworks.com/matlabcentral/newsreader/
view_thread/151925

It is better that you search your topic before posting a
question about it.

Anh Huy Phan
RIKEN - BSI


Subject: Re: Find angles between two vectors

From: Roger Stafford

Date: 23 Jan, 2008 21:16:03

Message: 5 of 6

"Justin Morehouse" <norman_batez@MSN.com> wrote in message <fn7egt
$447$1@fred.mathworks.com>...
> Hi there, I have a two vectors (3,5) and (5,6) and I was
> wondering how do I get the angle between them in matlab.
>
> on paper I would multiply both vectors to get (15+30) to =
> 45 and then square both (3,5) and (5,6) to get (9+25) and
> (25+36). Then I would get the square root of both (34) and
> (61) and multiply them together and then divide 45 by that.
>
> Afterwhich I would then use a trusty calc to do cos theta
> of that, but how can math lab do that?
>
> Thanks!
------------
  Another possible solution:

 x1 = 3; y1 = 5;
 x2 = 5; y2 = 6;
 ang = atan2(abs(x1*y2-y1*x2),x1*x2+y1*y2);

where ang is measured in radians. (Multiply by 180/pi to get degrees.)

  This method has a slight advantage over the arccosine method. The acos
function suffers an inherent loss of accuracy near angles 0 and pi, whereas
the atan2 function maintains full accuracy for such cases. (Make a plot of the
acos curve from -1 to +1 to see why.)

  It is important to distinguish between two possible definitions of an angle
between vectors in the x-y plane. Above, the angle is considered as a non-
negative quantity lying between 0 and pi. It can also be defined as the angle
measured counterclockwise from the first vector to the second one, which in
general would be an angle ranging from 0 to 2*pi, or else from -pi to +pi
with clockwise being considered negative. For this latter meaning one would
remove the 'abs' in the above expression.

Roger Stafford

Subject: Re: Find angles between two vectors

From: Justin Morehouse

Date: 23 Jan, 2008 23:25:04

Message: 6 of 6

"Justin Morehouse" <norman_batez@MSN.com> wrote in message
<fn7egt$447$1@fred.mathworks.com>...

Wow, matlab is pretty neat, thanks for your help guys!


Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
atan2 Roger Stafford 23 Jan, 2008 16:27:39
norm us 23 Jan, 2008 08:55:09
dot us 23 Jan, 2008 08:55:08
code us 23 Jan, 2008 08:55:08
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics