Why do I get the wrong value when I try to evaluate log([-1;1]) this way?

2 views (last 30 days)
I get different results in MATLAB R2017a when I perform the same operation in different ways:
>> v = -1 + 1i; >> log([0;0]./v-1)
ans =
0.0000 - 3.1416i
0.0000 - 3.1416i
This is incorrect
On the other hand, doing the following shows correct output: >> log([0;0]./(-1 + 1i)-1)
ans =
0.0000 + 3.1416i
0.0000 + 3.1416i
This is correct
  1 Comment
Star Strider
Star Strider on 9 Nov 2017
It was apparently fixed in R2017b:
v = -1 + 1i;
log([0;0]./v-1)
log([0;0]./(-1 + 1i)-1)
ans =
0 + 3.1416i
0 + 3.1416i
ans =
0 + 3.1416i
0 + 3.1416i

Sign in to comment.

Answers (2)

Viggnesh Venkatakrishnan
Viggnesh Venkatakrishnan on 17 Dec 2018
Hi Farouk,
I understand that you are getting different results in MATLAB R2017a when you perform the below operation in different ways:
% way 1
>> v = -1 + 1i; >> log([0;0]./v-1)
% way 2
>> log([0;0]./(-1 + 1i)-1)
This is a known issue with MATLAB R2017a where there is inconsistency in handling real values stored in a complex array for log, log2, log10, and log1p. This is a link to the External Bug Report : https://www.mathworks.com/support/bugreports/details/1653779
This issue has been fixed in R2017b.
Thanks and Regards,
Viggnesh Venkatakrishnan

Roger Stafford
Roger Stafford on 9 Nov 2017
Edited: Roger Stafford on 9 Nov 2017
Both answers are correct for log(-1). In fact there are infinitely many possible correct answers of the form (2*n+1)*pi*i for any integer n, positive or negative. In mathematics the logarithm function is said to have infinitely many “branches”. If you started with z = 1 and moved z continuously in the complex plane in a circle around the origin as center in the counterclockwise direction you would arrive at pi*i for log(-1). However, if you moved clockwise instead, you would arrive at -pi*i. If you continued around multiple times you would get all those other possible values.

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!