Path: news.mathworks.com!newsfeed-00.mathworks.com!news.mv.net!news.glorb.com!news2.glorb.com!feeder.erje.net!newsfeed01.sul.t-online.de!newsmm00.sul.t-online.de!t-online.de!news.t-online.com!not-for-mail
From: Ralph Schleicher <rs+usenet@ralph-schleicher.de>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Calculate angle on 0-360 scale from positive and negatives x and y vectors.
Date: Mon, 21 Mar 2011 00:27:36 +0100
Organization: Ralph Schleicher
Lines: 43
Message-ID: <87hbax2x07.fsf@bravo.mueller-schleicher.i>
References: <im3js0$it6$1@fred.mathworks.com>
	<im3nrg$i68$1@fred.mathworks.com> <im3s84$nef$1@fred.mathworks.com>
	<im5vkc$cju$1@fred.mathworks.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Trace: news.t-online.com 1300666082 01 n1805 BuUKCXhO2Pq5pg0 110321 00:08:02
X-Complaints-To: usenet-abuse@t-online.de
X-ID: Tlnhd6ZcQectgDShtcm0KzrGzoHPoUTPs+66uhwJjRQoBKbfg86poM
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Cancel-Lock: sha1:/qdhNSIQ8uK3StwxhAWvkMLkxH8=
Xref: news.mathworks.com comp.soft-sys.matlab:717018

"Thomas " <wandernmann@gmail.com> writes:

>> If you want it to range between 0 and 2*pi, just do this:
>>
>>  a = mod(atan2(y,x),2*pi);
>>
>> Roger Stafford

> Ahh...atan2 and mod were just the functions I was looking for,

You should not use 'mod' in combination with multiples of pi because
it may result in ulp errors.

     a = atan2(y, x);
     tem = (a < 0);
     a(tem) = a(tem) + 2 .* pi;

is the accurate form.

>> x = (-2*pi:pi/3:2*pi)';
>> [x, sin(x) == sin(mod(x, 2*pi))]
ans =

     -6.2832           0
      -5.236           0
     -4.1888           0
     -3.1416           0
     -2.0944           0
     -1.0472           0
           0           1
      1.0472           1
      2.0944           1
      3.1416           1
      4.1888           1
       5.236           1
      6.2832           0

-- 
Ralph Schleicher  <http://ralph-schleicher.de>

Development * Consulting * Training
Mathematical Modeling and Simulation
Software Tools