Inconsistency of angle function in finding angle of vectors

조회 수: 11 (최근 30일)
Aleem Andrew
Aleem Andrew 2021년 4월 12일
답변: Steven Lord 2021년 4월 12일
Typing the command angle(1+i) yields an angle of 45 degrees, but typing angle(-1-2i) yields -2.0344. Why is the angle being measured clockwise from the x axis in one case and and counterclockwise in the other? Is there any more consistent way to find the angle of vectors, either measured clockwise or counterclockwise?

채택된 답변

the cyclist
the cyclist 2021년 4월 12일
I would not characterize this as an inconsistency, so much as a convention to report the value in the range [-pi,pi], rather than the [0,2pi] that you expected. (I feel obligated to point out that this is quite explicit in the documentation.)
Regardless of your opinion on that, all you need to do is calculate the result modulus 2pi, to get what you want
mod(angle(1+i),2*pi)
ans = 0.7854
mod(angle(-2-i),2*pi)
ans = 3.6052

추가 답변 (1개)

Steven Lord
Steven Lord 2021년 4월 12일
Typing the command angle(1+i) yields an angle of 45 degrees,
No it doesn't. It returns the equivalent angle in radians, however.
A1 = angle(1+1i)
A1 = 0.7854
A2 = deg2rad(45)
A2 = 0.7854
abs(A2-A1)
ans = 0
but typing angle(-1-2i) yields -2.0344.
A3 = angle(-1-2i)
A3 = -2.0344
Correct.
Why is the angle being measured clockwise from the x axis in one case and and counterclockwise in the other?
From the documentation page for the angle function:
"theta = angle(z) returns the phase angle in the interval [-π,π] for each element of a complex array z. The angles in theta are such that z = abs(z).*exp(i*theta)."
If you want the angles to be in the interval [0, π] instead add the negative angle to 2*pi.
A3pos = 2*pi+A3
A3pos = 4.2487
theAngles = rad2deg([A3; A3pos])
theAngles = 2×1
-116.5651 243.4349
These angles are:
diff(theAngles)
ans = 360
360 degrees apart.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by