why does similar code generate complex numbers?
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (1)
John D'Errico
el 1 de Ag. de 2020
Editada: John D'Errico
el 1 de Ag. de 2020
What, for example, are the THREE cube roots of a negative number? We can find them all as solutions to the equation
syms x
xsol = solve(x^3 == -4,'maxdegree',3)
xsol =
-2^(2/3)
2^(2/3)*((3^(1/2)*1i)/2 + 1/2)
-2^(2/3)*((3^(1/2)*1i)/2 - 1/2)
vpa(xsol)
ans =
-1.5874010519681994748
0.79370052598409973738 + 1.3747296369986026264i
0.79370052598409973738 - 1.3747296369986026264i
So -4 has 3 cube roots. We can plot them in the complex plane.
plot(double(xsol),'o')
xline(0);
yline(0);

Which one has the smallest phase angle in the complex plane?
angle(xsol)
ans =
pi
pi/3
-pi/3
So the second root it found is the one that lives at an angle of pi/3 in the complex plane, or 60 degrees for the degree fanatics. This is the root with the smallest complex phase angle, and is the one chosen by power.
NTHROOT explicitly looks for the real root.
nthroot(-4,3)
ans =
-1.5874
It tells you that.
help nthroot
nthroot Real n-th root of real numbers.
nthroot(X, N) returns the real Nth root of the elements of X.
Both X and N must be real, and if X is negative, N must be an odd integer.
1 comentario
IB00
el 2 de Ag. de 2020
Categorías
Más información sobre Exponents and Logarithms en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!