Path: news.mathworks.com!newsfeed-00.mathworks.com!newscon02.news.prodigy.net!prodigy.net!news.glorb.com!postnews.google.com!n60g2000hse.googlegroups.com!not-for-mail
From:  Greg Heath <heath@alumni.brown.edu>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Angle between two vectors
Date: Tue, 10 Jul 2007 14:55:26 -0700
Organization: http://groups.google.com
Lines: 62
Message-ID: <1184104526.774530.54800@n60g2000hse.googlegroups.com>
References: <ef5ce9c.-1@webcrossing.raydaftYaTP>
NNTP-Posting-Host: 68.32.133.5
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
X-Trace: posting.google.com 1184104526 27335 127.0.0.1 (10 Jul 2007 21:55:26 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Tue, 10 Jul 2007 21:55:26 +0000 (UTC)
In-Reply-To: <ellieandrogerxyzzy-1007070127090001@dialup-4.232.57.130.dial1.losangeles1.level3.net>
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; AT&T CSM6.0; AT&T CSM 6; (R1 1.3); .NET CLR 1.0.3705),gzip(gfe),gzip(gfe)
Complaints-To: groups-abuse@google.com
Injection-Info: n60g2000hse.googlegroups.com; posting-host=68.32.133.5;
Xref: news.mathworks.com comp.soft-sys.matlab:418354


On Jul 10, 4:27 am, ellieandrogerxy...@mindspring.com.invalid (Roger
Stafford) wrote:
> In article <1184052626.324470.175...@n2g2000hse.googlegroups.com>, Greg
>
>
>
>
>
> Heath <h...@alumni.brown.edu> wrote:
> > On Jul 9, 5:16 pm, "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));
>
> > Invalid since it is possible that abs(dot(a,b)) > 1.
>
> > costheta = dot(a,b)/(norm(a)*norm(b));
> > theta = acos(costheta);
>
> > will give you the anser in the interval [0,pi].
>
> > > However, how do I know whether the angle is
> > > actually theta, or -theta or pi-theta or pi+theta??
>
> > Angles between vectors only lie in the interval [0,pi].
>
> > > Notice that the vectors are in three dimension (3d).
>
> > Dimensionality of the original space is irrelevant. As long as
> > norm(a)*norm(b) > 0, the vectors uniquely define a 2-d space when
> > dot(a,b) ~= 0 and a unique 1-d space otherwise.
>
> > Hope this helps.
>
> > Greg
>
> ---------------
>   Greg, Y Mehta did specifically state that a and b are unit vectors, so
> his formula is in fact correct as it stands, though subject to increasing
> errors as its dot product approaches +1 or -1.  When a and b are nearly
> parallel, the same kind of trouble occurs with the formula you have given
> here.  In such cases there is a real need to combine the scalar dot
> product with the vector cross product in order to make use of both the
> sine and cosine in calculating the angle accurately, which is what the
> 'tan2' formula does.

Thanks.

For some problem in the past (probably single precision?) I got better
accuracy using

sign(sintheta)*acos(costheta)

instead of atan2.

Hope this helps.

Greg

Hope this helps.