<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240627</link>
    <title>MATLAB Central Newsreader - Issues with atan2 function.. please advise!</title>
    <description>Feed for thread: Issues with atan2 function.. please advise!</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Mon, 08 Dec 2008 10:59:14 -0500</pubDate>
      <title>Issues with atan2 function.. please advise!</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240627#615634</link>
      <author>DRG</author>
      <description>Hey folks; I have written a program that utilises the atan2 function,&lt;br&gt;
only it's giving me some very strange behaviour at certain values; in&lt;br&gt;
essence, I have a grid 1000 x 1000 units, and a circle at the centre&lt;br&gt;
(g,f) = (500,500) with radius 100 units. The program asks the user to&lt;br&gt;
specify an X-value between 0 and 1 and a Y - value between 0 and 1,&lt;br&gt;
corresponding to the units. (For example, a user inputting X = 0.3 and&lt;br&gt;
Y = 0.2 would specify the point [300,200]). With this data, the&lt;br&gt;
program uses the atan2 function to work out the angle gamma between&lt;br&gt;
the point (x,y) and the circle centre (g,f). It then calculates the&lt;br&gt;
half angle bisector of the two tangent points, beta. Using this and&lt;br&gt;
some basic trig, it computes the angle of the point to the lower&lt;br&gt;
tangent line (theta) and the upper tangent line (phi). Then it&lt;br&gt;
analyses all 1000 x 1000 points; if they lie between theta and phi,&lt;br&gt;
then they're assigned an I value of 0, otherwise they are assigned an&lt;br&gt;
I value of 1000/r. The code is here;&lt;br&gt;
&lt;br&gt;
------------------------------------------------------------------&lt;br&gt;
%Circle tangent code&lt;br&gt;
&lt;br&gt;
format long&lt;br&gt;
Irad=zeros([1000 1000]);&lt;br&gt;
warning off all&lt;br&gt;
&lt;br&gt;
g = .50; %Circle centre, X-position metres&lt;br&gt;
f = .50; %Circle centre, Y- Position metres&lt;br&gt;
crad = .10; %Circle radius in metres&lt;br&gt;
gsize=100;&lt;br&gt;
A = 1000;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
X = input('X-position (between 0 and 1): ');&lt;br&gt;
Y = input('Y-position (between 0 and 1): ');&lt;br&gt;
&lt;br&gt;
tubecent = sqrt((g - X)^2 + (f - Y)^2); %distance from (X,Y) to (g,f)&lt;br&gt;
gamma = atan2((f - Y),(g - X));&lt;br&gt;
beta = asin((crad)/tubecent);&lt;br&gt;
theta = gamma - beta;&lt;br&gt;
phi = gamma + beta;&lt;br&gt;
&lt;br&gt;
for j = 1:1000&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for k = 1:1000&lt;br&gt;
r = sqrt( ((j/1000)-(X)).^2 + ((k/1000)-(Y)).^2 );&lt;br&gt;
%r is the distance of the point in question to the point (X,Y)&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
ang = atan2(((k/1000) - Y),((j/1000) - X));&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
if ang &amp;lt;= theta || ang &amp;gt;= phi %tests of angle is less than theta&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;I = A ./ r;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;I = 0;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Ir(j,k) = sum(I);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
[XM,YM]=meshgrid(0.1:0.1:gsize , 0.1:0.1:gsize);&lt;br&gt;
&lt;br&gt;
figure(2)&lt;br&gt;
pcolor(YM,XM,log(Ir)), title('Log of Intensity versus distance graph')&lt;br&gt;
xlabel('Centimetres'), ylabel('Centimetres')&lt;br&gt;
shading interp&lt;br&gt;
&lt;br&gt;
----------------------------------------------------------------------------&lt;br&gt;
&lt;br&gt;
This works fine, except when you try to run the code at (X,Y) =&lt;br&gt;
(1,0.5). In fact, you get strange results anywhere that satisfies the&lt;br&gt;
following condition.&lt;br&gt;
&lt;br&gt;
X &amp;gt; g, and (Y &amp;gt;= f - crad, Y &amp;lt;= f + crad)&lt;br&gt;
&lt;br&gt;
Can anyone please help me? I've been trying to weeks to solve this&lt;br&gt;
problem! If you input the same point under symmetry (ie (0,0.5),&lt;br&gt;
(0.5,0), (0.5,1) ) the results are perfect! Any ideas? Thank you so&lt;br&gt;
much in advance.</description>
    </item>
    <item>
      <pubDate>Mon, 08 Dec 2008 14:50:12 -0500</pubDate>
      <title>Re: Issues with atan2 function.. please advise!</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240627#615677</link>
      <author>DRG</author>
      <description>I should mention that the problem may have something to do with the&lt;br&gt;
conditions after the for loop, but I cant see why or how...</description>
    </item>
    <item>
      <pubDate>Mon, 08 Dec 2008 16:14:02 -0500</pubDate>
      <title>Re: Issues with atan2 function.. please advise!</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240627#615703</link>
      <author>David </author>
      <description>me thinks its a logic problem, not atan2.  note that atan2 has a range of -pi to +pi.  when you compute theta and phi you are taking gamma (a product of atan2) which therefore has a range of -pi to +pi and adding/subtracting beta (a product of an asin) which has a range of -pi/2 to +pi/2.  so the lines:&lt;br&gt;
&lt;br&gt;
theta = gamma - beta&lt;br&gt;
phi = gamma + beta&lt;br&gt;
&lt;br&gt;
can generate ranges of theta and phi from 3pi/2 to -3pi/2...&lt;br&gt;
&lt;br&gt;
Then you calc ang from an atan2 so it has a range of -pi to pi, but you compare ang to theta and phi to determine if you are inside or outside the triangle... &lt;br&gt;
&lt;br&gt;
if ang &amp;lt;= theta || ang &amp;gt;= phi %tests of angle is less than theta&lt;br&gt;
&lt;br&gt;
but in some cases theta and phi will be outside of the range of ang which seems to cut off part of the triangle.</description>
    </item>
    <item>
      <pubDate>Mon, 08 Dec 2008 16:59:02 -0500</pubDate>
      <title>Re: Issues with atan2 function.. please advise!</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240627#615715</link>
      <author>Roger Stafford</author>
      <description>DRG &amp;lt;grimesd2@gmail.com&amp;gt; wrote in message &amp;lt;6f0f0e86-f955-43bc-a085-588a9cea560e@j32g2000yqn.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; ......&lt;br&gt;
&amp;gt; This works fine, except when you try to run the code at (X,Y) =&lt;br&gt;
&amp;gt; (1,0.5). In fact, you get strange results anywhere that satisfies the&lt;br&gt;
&amp;gt; following condition.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; X &amp;gt; g, and (Y &amp;gt;= f - crad, Y &amp;lt;= f + crad)&lt;br&gt;
&amp;gt; ......&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;The &quot;issue&quot; is not with 'atan2'.  It's the inequality testings in your code that are inappropriate.  If I understand you correctly, you are trying to determine whether two vectors with angles 'gamma' and 'ang' with respect to the x-axis, respectively, have an acute angle between them of less than or greater than beta.  Such an angle can be computed this way:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;abs(mod(gamma-ang+pi,2*pi)-pi)&lt;br&gt;
&lt;br&gt;
and that is what should be compared with beta.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Tue, 09 Dec 2008 13:26:51 -0500</pubDate>
      <title>Re: Issues with atan2 function.. please advise!</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240627#615899</link>
      <author>DRG</author>
      <description>Thanks guys, that does indeed seem to work now. Thanks for taking a&lt;br&gt;
look!</description>
    </item>
  </channel>
</rss>

