Path: news.mathworks.com!not-for-mail
From: "Bruno Luong" <b.luong@fogale.findmycountry>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Angle between two vectors
Date: Thu, 10 Mar 2011 07:56:04 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 42
Message-ID: <ila06k$t8g$1@fred.mathworks.com>
References: <ef5ce9c.-1@webcrossing.raydaftYaTP> <1184052626.324470.175540@n2g2000hse.googlegroups.com> <ellieandrogerxyzzy-1007070127090001@dialup-4.232.57.130.dial1.losangeles1.level3.net> <1184104526.774530.54800@n60g2000hse.googlegroups.com> <ellieandrogerxyzzy-1007071622370001@dialup-4.232.15.192.dial1.losangeles1.level3.net> <idlqpm$rc7$1@fred.mathworks.com> <il9t55$en6$1@fred.mathworks.com>
Reply-To: "Bruno Luong" <b.luong@fogale.findmycountry>
NNTP-Posting-Host: www-01-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1299743764 29968 172.30.248.46 (10 Mar 2011 07:56:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 10 Mar 2011 07:56:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: news.mathworks.com comp.soft-sys.matlab:714967

"Marwan N" wrote in message <il9t55$en6$1@fred.mathworks.com>...
> hey guys,
> 
> I went thro the codes u all gave several times....all are different ways for the same pupose....which finally gave me a head spin after all those cods in my head. As a simple conclusion can someone answer me the following:
> 
> 1. how do we calculate the angle (consider both the obtuse and acute angle) between two vectors (consider that they intersect at origin or in other words, they originate from origin)
> 
> 2.the vectors start at the origin and terminate at the points X=[coresponding coeff of i j k] and Y=[corresponding coeff of i j k]
> 
> 3.the results (obtuse and acute angle) should lie between 0 and pi.
> 
> any bright ideas anyone??

>> a=randn(2,1)

a =

    1.4897
    1.4090

>> b=randn(2,1)

b =

    1.4172
    0.6715

% Pad 0 to extend in 3D if needed
>> a(3)=0
>> b(3)=0

% Roger method

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

angle =

    0.3151

angle will be [0,pi] since norm(cross(a,b)) >= 0.

Bruno