Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Find angles between two vectors
Date: Wed, 23 Jan 2008 21:16:03 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 38
Message-ID: <fn8aqj$88o$1@fred.mathworks.com>
References: <fn7egt$447$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1201122963 8472 172.30.248.35 (23 Jan 2008 21:16:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 23 Jan 2008 21:16:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:447293



"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