From: (Roger Stafford)
Newsgroups: comp.soft-sys.matlab
Subject: Re: Angle between two vectors
Message-ID: <>
References: <ef5ce9c.-1@webcrossing.raydaftYaTP>
Organization: -
Lines: 32
Date: Tue, 10 Jul 2007 02:57:36 GMT
X-Trace: 1184036256 (Mon, 09 Jul 2007 19:57:36 PDT)
NNTP-Posting-Date: Mon, 09 Jul 2007 19:57:36 PDT
Xref: comp.soft-sys.matlab:418116

In article <ef5ce9c.-1@webcrossing.raydaftYaTP>, "y Mehta"
<mehtayogesh@gmail.(DOT).com> wrote:

> How do I find the angle between two unit vectors a and b? I know I
> can find cosine theta by the following formula:
> theta = acos(dot(a,b));
> However, how do I know whether the angle is actually theta, or -theta
> or pi-theta or pi+theta??
> Notice that the vectors are in three dimension (3d).
> Thanks,
> -YM
  It is usually understood that the angle between two three-dimensional
vectors is measured by the shortest great circle path between them, which
means that it must lie between 0 and pi radians.  To get such an answer,
the best method, in my opinion, is this:

 angle = atan2(norm(cross(a,b)),dot(a,b));

Since the first argument must be non-negative, the angle will lie
somewhere in the two first quadrants, and thus be between 0 and pi.  This
formula remains valid even if a and b are not unit vectors.

  Your 'acos' formula gives the correct answer with unit vectors as it
stands, but it encounters an accuracy problem for angles that are near 0
or pi.  This is the main reason for my preference for the 'atan2' method.

Roger Stafford