Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Issues with atan2 function.. please advise!

Subject: Issues with atan2 function.. please advise!

From: DRG

Date: 8 Dec, 2008 10:59:14

Message: 1 of 5

Hey folks; I have written a program that utilises the atan2 function,
only it's giving me some very strange behaviour at certain values; in
essence, I have a grid 1000 x 1000 units, and a circle at the centre
(g,f) = (500,500) with radius 100 units. The program asks the user to
specify an X-value between 0 and 1 and a Y - value between 0 and 1,
corresponding to the units. (For example, a user inputting X = 0.3 and
Y = 0.2 would specify the point [300,200]). With this data, the
program uses the atan2 function to work out the angle gamma between
the point (x,y) and the circle centre (g,f). It then calculates the
half angle bisector of the two tangent points, beta. Using this and
some basic trig, it computes the angle of the point to the lower
tangent line (theta) and the upper tangent line (phi). Then it
analyses all 1000 x 1000 points; if they lie between theta and phi,
then they're assigned an I value of 0, otherwise they are assigned an
I value of 1000/r. The code is here;

------------------------------------------------------------------
%Circle tangent code

format long
Irad=zeros([1000 1000]);
warning off all

g = .50; %Circle centre, X-position metres
f = .50; %Circle centre, Y- Position metres
crad = .10; %Circle radius in metres
gsize=100;
A = 1000;


X = input('X-position (between 0 and 1): ');
Y = input('Y-position (between 0 and 1): ');

tubecent = sqrt((g - X)^2 + (f - Y)^2); %distance from (X,Y) to (g,f)
gamma = atan2((f - Y),(g - X));
beta = asin((crad)/tubecent);
theta = gamma - beta;
phi = gamma + beta;

for j = 1:1000
    for k = 1:1000
r = sqrt( ((j/1000)-(X)).^2 + ((k/1000)-(Y)).^2 );
%r is the distance of the point in question to the point (X,Y)


ang = atan2(((k/1000) - Y),((j/1000) - X));


if ang <= theta || ang >= phi %tests of angle is less than theta
    I = A ./ r;


    else
    I = 0;


end


Ir(j,k) = sum(I);
    end
end
[XM,YM]=meshgrid(0.1:0.1:gsize , 0.1:0.1:gsize);

figure(2)
pcolor(YM,XM,log(Ir)), title('Log of Intensity versus distance graph')
xlabel('Centimetres'), ylabel('Centimetres')
shading interp

----------------------------------------------------------------------------

This works fine, except when you try to run the code at (X,Y) =
(1,0.5). In fact, you get strange results anywhere that satisfies the
following condition.

X > g, and (Y >= f - crad, Y <= f + crad)

Can anyone please help me? I've been trying to weeks to solve this
problem! If you input the same point under symmetry (ie (0,0.5),
(0.5,0), (0.5,1) ) the results are perfect! Any ideas? Thank you so
much in advance.

Subject: Issues with atan2 function.. please advise!

From: DRG

Date: 8 Dec, 2008 14:50:12

Message: 2 of 5

I should mention that the problem may have something to do with the
conditions after the for loop, but I cant see why or how...

Subject: Issues with atan2 function.. please advise!

From: David

Date: 8 Dec, 2008 16:14:02

Message: 3 of 5

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:

theta = gamma - beta
phi = gamma + beta

can generate ranges of theta and phi from 3pi/2 to -3pi/2...

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...

if ang <= theta || ang >= phi %tests of angle is less than theta

but in some cases theta and phi will be outside of the range of ang which seems to cut off part of the triangle.

Subject: Issues with atan2 function.. please advise!

From: Roger Stafford

Date: 8 Dec, 2008 16:59:02

Message: 4 of 5

DRG <grimesd2@gmail.com> wrote in message <6f0f0e86-f955-43bc-a085-588a9cea560e@j32g2000yqn.googlegroups.com>...
> ......
> This works fine, except when you try to run the code at (X,Y) =
> (1,0.5). In fact, you get strange results anywhere that satisfies the
> following condition.
>
> X > g, and (Y >= f - crad, Y <= f + crad)
> ......

  The "issue" 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:

 abs(mod(gamma-ang+pi,2*pi)-pi)

and that is what should be compared with beta.

Roger Stafford

Subject: Issues with atan2 function.. please advise!

From: DRG

Date: 9 Dec, 2008 13:26:51

Message: 5 of 5

Thanks guys, that does indeed seem to work now. Thanks for taking a
look!

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us