Working with complex numbers in the Symbolic Math Toolbox. Why does angle(A) returns atan2(sin(alpha),cos(alpha)) instead of just alpha?
20 views (last 30 days)
Show older comments
%% Complex number using TRIGONOMETRIC form
% Let's define A as a complex number in polar form, with magnitude M and angle alpha
M = sym('M',{'real','positive'}); % magnitude
alpha = sym('alpha','real'); % angle
assume(alpha>-pi & alpha<pi)
A = M*(cos(alpha) + i*sin(alpha)); % complex number in polar form
% Now, I would expect abs(A) to return M. It does not.
abs(A)
% But, using rewrite and combine, I can get the it to return M
combine(rewrite(abs(A), 'sqrt'), 'sincos')
% Similarly, I would expect the angle(A) to return alpha, but instead we get atan2(sin(alpha), cos(alpha)).
angle(A)
% I tried the simplify and rewrite function, but I could not get the angle(A) to return alpha. Is there a way around this?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/362224/image.png)
0 Comments
Accepted Answer
Dana
on 17 Sep 2020
This seems to be the result of a gap in MATLAB's symbolic logic. Your assumption
assume(alpha>-pi & alpha<pi)
should be enough to conclude that atan2(sin(alpha),cos(alpha))=alpha. However, for some reason MATLAB seems to be applying the one-argument atan rules (rather than the 2-argument atan2 ones) to this problem, wherein atan(sin(alpha)/cos(alpha))=alpha only if -pi/2<alpha<pi/2. You can check this by replacing the above assumption with
assume(alpha>-pi/2 & alpha<pi/2)
and then
>> simplify(angle(A))
ans =
alpha
as desired.
I'm not sure if this is a bug, or if there's some good reason for it.
2 Comments
Dana
on 18 Sep 2020
Edited: Dana
on 18 Sep 2020
To be clear, depending on your application, you don't necessarily want to restrict the angle to be between -pi/2 and pi/2. There are perfectly good reasons why you might be interested in allowing for any angle between -pi and pi. It's just that, if you don't make the -pi/2,pi/2 restriction then MATLAB won't recognize that atan2(sin(alpha),cos(alpha))=alpha. Under your assumption that alpha is between -pi and pi, it should recognize that, but for some reason it doesn't.
To understand this, for the case of the one-argument atan function it's pretty straightforward. The tan function is not actually invertible: for any angle θ and any integer k,
. Thus, for any number a, there are an infinite number of angles ω such that
, and this means that tan isn't invertible.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/362746/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/362749/image.png)
In practice, though, we can pick some b and then define the "inverse"
to be the unique angle
such that
. Ultimately the choice of b is arbitrary, and you can see different choices in different contexts. For the case of the atan function in MATLAB,
, so that the relevant range is
. This is where that restriction above is coming from.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/362752/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/362755/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/362749/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/362761/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/362764/image.png)
However, for the atan2 function, we have more information, which expands the range of unique angles from
to
. MATLAB uses
in this case, so we should be able to recover any
, but as I say, for some reason MATLAB's symbolic logic isn't recognizing that.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/362767/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/362770/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/362773/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/362776/image.png)
More Answers (1)
Asad (Mehrzad) Khoddam
on 17 Sep 2020
if you want to find the numerical value of alpha, one solution is to use double function:
double(angle(A))
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!