atan2 does not accept complex numbers as input
Show older comments
Good Morning
If I'm not mistaken I think that there is a definition of the atan2() function also in the complex field.
If so , It is possible to implement a complex version of the atan2() function ?
i mean in a way that i can compute atan2(a+i*b , c+i*d) , where a,b,c,d are real and i =sqrt(-1) ,
this both numerically and symbolically
thankyou :-)
Kind regards
Manu1965@wail.ch
4 Comments
"If I'm not mistaken I think that there is a definition of the atan2() function also in the complex field."
I am not aware of any common definition for this: do you have a reference?
"It is possible to implement a complex version of the atan2() function ?"
Hmmm, interesting question. In a quick search of various tools I could not find any such thing implemented.
This seems like an interesting extension to ATAN2 and a valid enhancement request:
Val
on 13 Sep 2024
I am not sure what the meaning or expected output for complex inputs might be, but for real inputs at least the formula seems to give the same output as ATAN2:
v = 1;
u = 3;
atan2(v,u)
-1i*log((u+1i*v)./sqrt(u.^2+v.^2))
V = -10:10;
U = V(:);
A = atan2(V,U);
B = -1i*log((U+1i*V)./sqrt(U.^2+V.^2));
surf(A)
max(abs(imag(B(:)))) % only floating point noise
surf(real(B))
Do you have any complex test values with known outputs?
If that formula works for you, then perhaps you can save it in a function and use it.
Val
on 13 Sep 2024
Accepted Answer
More Answers (1)
nick
on 12 Sep 2024
Hi MA,
I see you're interested in working with complex numbers and calculating angles using MATLAB. You can use 'atan2' in conjunction with the 'imag' and 'real' functions to extract the imaginary and real parts of the complex number.
Here's a simple MATLAB script to demonstrate how you can do this:
z = 4 + 3i;%example complex number
theta = atan2(imag(z),real(z));
This script calculates the angle theta of the complex number z, where 'imag(z)' gives the imaginary part and 'real(z)' gives the real part.
You can refer to the following documentation to learn more about the functions 'imag' and 'real':
7 Comments
John D'Errico
on 12 Sep 2024
Edited: John D'Errico
on 12 Sep 2024
You are missing the point here, in that the question is NOT how to compute atan(z) in a 4 quadrant sense. That correctly uses atan2, and you could do so in the way you show. The question is how to compute atan2(u,v), where both u and v are (or could be) complex numbers, and either or both could be zero.
And, yes, you might compute the ratio of the two complex numbers, separate that into real and complex parts, then use atan2 as you show. But that would fail for the case of atan2(1+i,0), where you would encounter a divide by zero. It would also fail to properly deal with all 4 quadrants for even the simple case of 2 real numbers.
Val
on 12 Sep 2024
My understanding of the function 'atan2(z1,z2)' is that the angle formed by z1/z2 in complex plane. With this understanding, I demonstrated the angle computation of the complex variable for a simple case. As suggested, the user can use complex number in the form of z1/z2 and then compute the angle with real and imaginary parts of z1/z2.
I understand that this would not work for the cases when z2=0. However, it works correctly for the case of 2 real numbers as shown below:
z1=1;
z2=-2;
atan2(imag(z1/z2),real(z1/z2))
Kindly let me know if I am missing something in the above case.
"Kindly let me know if I am missing something in the above case."
You are missing the fact that the user requires both inputs to be complex. This generalisation is explained here for various inverse trigonometric functions:
ATAN already supports complex inputs:
x = 1+2i;
y = 3-4i;
atan(y./x)
which matches exactly the formula given on Wikipedia:
z = y./x;
-1i*log((1-1i*z)./sqrt(1+z.^2))
For what it is worth, numpy and Wolfram Alpha gave the same result.
Whereas ATAN2 does not support complex inputs (yet the OP would like it to):
try
atan2(y,x)
catch ME
ME.message
end
By using REAL and IMAG your approach provides two real inputs to ATAN2, which will therefore return another real result (due to the domain of ATAN2 being anyway defined for the entire real plane):
atan2(imag(y/x),real(y/x))
So there is no way to get a complex output using your approach. It is unrelated to what the OP asked about.
nick
on 13 Sep 2024
Thank you @Stephen23
"If I'm not mistaken I think that there is a definition of the atan2() function also in the complex field." I misunderstood the OP's statement that the user wanted to use complex number as input to the function and obtain the angle as a real valued output.
Val
on 13 Sep 2024
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


