Path: news.mathworks.com!newsfeed-00.mathworks.com!news.kjsl.com!newsfeed.stanford.edu!headwall.stanford.edu!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread2.news.pas.earthlink.net.POSTED!5f968bd6!not-for-mail From: ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford) Newsgroups: comp.soft-sys.matlab Subject: Re: Angle between two vectors Message-ID: <ellieandrogerxyzzy-0907071957360001@dialup-4.232.57.215.dial1.losangeles1.level3.net> References: <ef5ce9c.-1@webcrossing.raydaftYaTP> Organization: - Lines: 32 Date: Tue, 10 Jul 2007 02:57:36 GMT NNTP-Posting-Host: 4.232.57.215 X-Complaints-To: abuse@earthlink.net X-Trace: newsread2.news.pas.earthlink.net 1184036256 4.232.57.215 (Mon, 09 Jul 2007 19:57:36 PDT) NNTP-Posting-Date: Mon, 09 Jul 2007 19:57:36 PDT Xref: news.mathworks.com 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