Maths/formula behind atan2 function
42 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
DC
am 5 Jun. 2017
Kommentiert: Stephen23
am 6 Jun. 2017
Hi all,
I understand issues around this function have been discussed in great detail and I'm very thankful for the many helpful contributions on this topic (particularly from Roger Stafford) to date... they have helped me immensely with my work. But I was hoping to ask just one more question about it (sorry!).
I understand that creating the vector cross product and scalar dot product allows me to pass the sin and cosine to atan2 function to calculate the angle between 2 3D vectors (important for accuracy and also practical if vectors aren't normalised - as I understand it anyway). What I am unsure of is the maths or formula behind the atan2 function. I would like to know what it is and was wondering if anyone could help?
My guesses are that it might be something like:
atan2 = 2arctan(y/x)
where y = dot product and x = norm of cross product if x > 0.
Or
atan2 = 2arctan(y/(sqrt(x.^2+y.^2)+x)
where y = dot product and x = norm of cross product if x > 0.
Hopefully someone can tell me if I'm wrong/right and point me in the right direction.
Thanks in advance!
1 Kommentar
Akzeptierte Antwort
John D'Errico
am 5 Jun. 2017
Bearbeitet: John D'Errico
am 5 Jun. 2017
atan2(x,y) is effectively atan(y/x), with the exception that it caters to all 4 quadrants. So, while we have
y/x == (-y)/(-x)
it is not true that
atan2(y,x) == atan2(-y,-x)
So you see these are the same:
atan(1/3)
ans =
0.32175
atan2(1,3)
ans =
0.32175
Yet ...
atan2(-1,-3)
ans =
-2.8198
The difference is important when you need a 4 quadrant result. This is a major reason why atan2 exists. As well, atan2 is a bit more robust to problems. So, while atan would have survived this one:
atan2(1,0)
ans =
1.5708
atan(1/0)
ans =
1.5708
0/0 would cause a problem.
atan(0/0)
ans =
NaN
atan2(0,0)
ans =
0
So atan2 really is just atan, but with care taken to handle all 4 quadrants properly. The range of atan should be [-pi/2,pi/2]. But the range of atan2 is (-pi,pi]. (I might not have gotten the open and and closed intervals right there, but you should get the idea.)
The complete code (for scalar x and y) might look something like this:
if x == 0 && y == 0
result = 0;
else
offset = 0;
if x < 0
offset = pi;
end
result = atan(y/x) + offset;
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu MATLAB Mobile Fundamentals finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!