Working with complex numbers in the Symbolic Math Toolbox. Why does angle(A) returns atan2(sin(​alpha),cos​(alpha)) instead of just alpha?

조회 수: 37 (최근 30일)
%% 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?

채택된 답변

Dana
Dana 2020년 9월 17일
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
Pablo Tacconi
Pablo Tacconi 2020년 9월 18일
Thank you Dana. It is unclear to me why the angle must be defined between -pi/2 and pi/2, but, at least I am getting the desired results. Thanks again
Pablo
Dana
Dana 2020년 9월 18일
편집: Dana 2020년 9월 18일
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.
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.
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020년 9월 17일
if you want to find the numerical value of alpha, one solution is to use double function:
double(angle(A))

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by