In the following code, the array 'f1' contains real numbers while the array 'f2' contains complex numbers. From what I understand and am expecting, both arrays should contain identical data, consisting only of real numbers. But this is not what I get. Instead, the array 'f2', generated using exponential notation, contains complex numbers and the question is why is this happening?
x = [-27 -8 -1 1 8 27];f1 = nthroot(x, 3);f2 = x .^ (1/3);
The array 'f1' contains the expected results:
-3 -2 -1 1 2 3
For the array 'f2', here are the numbers generated:
1.5000 + 2.5981i 1.0000 + 1.7321i 0.5000 + 0.8660i 1.0000 + 0.0000i 2.0000 + 0.0000i 3.0000 + 0.0000i
The array 'f2' unexpectedly contains complex numbers. The last 3 numbers give the correct/expected result but the first 3 numbers are not the correct/expected result. That is, the first 3 numbers in 'f2' are NOT the cubed root of the first 3 numbers in 'x'. Why is this happening?
In trying to debug/understand the problem, when I manually enter the negative numbers of 'x' in the command window then I get the expected results:
-27 ^ (1/3) —> ans = -3
-8 ^ (1/3) —> ans = -2
-1 ^ (1/3) —> ans = -1
However, when I use array syntax in the command window and select the same negative numbers from 'x' then I get unexpected and incorrect results:
x(1) ^ (1/3) —> ans = 1.5000 + 2.5981i
x(2) ^ (1/3) —> ans = 1.0000 + 1.7321i
x(3) ^ (1/3) —> ans = 0.5000 + 0.8660i
I have no experience working with complex numbers. The question I am asking is why do I get incorrect results (i.e. in complex number format) when using exponential notation in my script?
Thanks to all who respond
Best Answer