๐ŸŒ
MathWorks
mathworks.com โ€บ matlab โ€บ mathematics โ€บ elementary math โ€บ trigonometry
atan2 - Four-quadrant inverse tangent - MATLAB
This MATLAB function returns the four-quadrant inverse tangent (tan-1) of Y and X, which must be real.
๐ŸŒ
Johns Hopkins University
math.jhu.edu โ€บ ~shiffman โ€บ 370 โ€บ help โ€บ techdoc โ€บ ref โ€บ atan2.html
atan2 (MATLAB Function Reference)
Description P = atan2(Y,X) returns an array P the same size as X and Y containing the element-by-element, four-quadrant inverse tangent (arctangent) of the real parts of Y and X. Any imaginary parts are ignored. Elements of P lie in the closed interval [-pi,pi], where pi is MATLAB's floating- ...
Find elsewhere
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 343418-maths-formula-behind-atan2-function
Maths/formula behind atan2 function - MATLAB Answers - MATLAB Central
June 5, 2017 - 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.
Top answer
1 of 1
1
It seems the answers provided so far are missing the point. If we want to see a 4 quadrant atan2 version, that works for complex X and Y, can we use what Alpha suggests? Thus atan2(U,V) = -i*log(U+i*V)./sqrt(U.^2 + V.^2) I'll modify what @Stephen23 did to see what happens, for complex U and V. A serious problem is I would need to have a hyper-dimensional monitor to show the complete behavior. (My own personal holodeck is broken, awaiting parts. But shipping is very slow, coming from the future.) I would need to display essentially 6 dimensional behavior, and a regular monitor is just not up to snuff there. Nor are my eyes or my brain. So I can only do my best to look at what happens. fun = @(V,U) -1i*log((U+1i*V)./sqrt(U.^2+V.^2)); First, what happens for real U and V? utest = randn(3);vtest = randn(3); fun(vtest,utest) And that is clearly a problem, since for real U and V, we want the result to return a purely real result. We need to patch it for that case. function Z = atan2alt(V,U) % extension of atan2(V,U) into the complex plane Z = -1i*log((U+1i*V)./sqrt(U.^2+V.^2)); % check for purely real input. if so, zero out the imaginary part. realInputs = (imag(U) == 0) & (imag(V) == 0); Z(realInputs) = real(Z(realInputs)); end atan2alt(vtest,utest) so atan2alt now returns purely real output for purely real inputs. Next, is it consistent with atan2? norm(atan2(vtest,utest) - atan2alt(vtest,utest)) And that is (as we sometimes said in the deep past) good enough for government work. The difference is down into the realm of floating point trash. Now I am at least a little happy, since the two are consistent for real input. Next is this a 4 quadrant result? Again, that will be difficult to show in detail. But perhaps we can do a little, by plotting the result along a simple path in the complex plane. V = (-10:10); U = V(:); v = V - 0.1i; u = U + 0.2i; So u and v both now each live along lines in the complex plane, where the imaginary part is constant, and non-zero. Z = atan2alt(v,u); And of course, since atan2alt will now return a complex result, we need to look at both the real and imaginary part. surf(U,V,real(Z)) xlabel "Real(U)" ylabel "Real(V)" zlabel "Real(Z)" surf(U,V,imag(Z)) xlabel "Real(U)" ylabel "Real(V)" zlabel "imag(Z)" The result is a 4 quadrant version. Here we see the imaginary component is indeed non-zero. surf(U,V,abs(Z)) xlabel "Real(U)" ylabel "Real(V)" zlabel "abs(Z)" Of course, this would need a great deal more thought, to see if there were additional problems other than the need for a real result. What happens when one or both of the arguments are zero? Again, unless the two are consistent, we have a problem. atan2([0 1],[0;1]) atan2alt([0 1],[0;1]) Hey, that worked. I was actually just a little surprised. I was going to guess the code would fail when U==V==0, and perhaps return a NaN. But as it turns out, the result from the formula is then 0+infi, with an infinite imaginary part. But since it has a zero real part, the check at the end of atan2alt saved me. I noticed that @M A wanted to see it work also for symbolic inputs. Again, I was wondering for just a second if it would survive here, but it did. syms u v atan2alt(v,u) Some might suggest plots I generated do not form a very strong test, since they presumed constant imaginary part. And, while it is not too difficult to conjure up a pair of paths for U and V that are more complicated, this seems a good point to stop. 'nuff said.
๐ŸŒ
MathWorks
la.mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 2085323-quat2angle-atan2-strange-quadrant-change-when-calculating-angle
quat2angle / atan2 strange quadrant change when calculating angle - MATLAB Answers - MATLAB Central
February 22, 2024 - I encountered a problem I believe in the atan2 function. As you can see in the first figure below the computed angle becomes negative. ... In blu, the angle was computed using the quat2angle function with the rotation sequence 'YZX', (it is the first output). In orange, the angle was computed with an Android app (Dart language). In dashed yellow, the angle was computed with my Dart algorithm translated into Matlab code.
๐ŸŒ
Northwestern University
ece.northwestern.edu โ€บ local-apps โ€บ matlabhelp โ€บ techdoc โ€บ ref โ€บ atan2.html
atan2 (MATLAB Functions)
P = atan2(Y,X) returns an array P the same size as X and Y containing the element-by-element, four-quadrant inverse tangent (arctangent) of the real parts of Y and X. Any imaginary parts are ignored. Elements of P lie in the closed interval [-pi,pi], where pi is the MATLAB floating-point ...
๐ŸŒ
MathWorks
mathworks.com โ€บ matlab โ€บ mathematics โ€บ elementary math โ€บ trigonometry
atan - Inverse tangent in radians - MATLAB
This definition of the atan function returns angles in radians within the interval [-ฯ€/2, ฯ€/2]. To find the four-quadrant inverse tangent, where the returned angles are in the interval [-ฯ€, ฯ€], use atan2. ... The atan function fully supports tall arrays. For more information, see Tall Arrays.
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 495790-issue-with-atan2
Issue with atan2(.) - MATLAB Answers - MATLAB Central
December 9, 2019 - The atan2 function included in MATLAB requires exactly two input arguments. Therefore you're likely calling a different atan2.m function.