When I try to get the cubic root of -1 (which should be -1) I get this:
(-1).^(1/3)
ans = 0.5000 + 0.8660i
How do I fix this?

1 commentaire

Dyuman Joshi
Dyuman Joshi le 10 Sep 2023
What is there to fix? That is one of the 3 cube roots of -1.
Do you wish to obtain -1 as the output?

Connectez-vous pour commenter.

 Réponse acceptée

Sam Chak
Sam Chak le 10 Sep 2023
nthroot(-1, 3)
ans = -1

4 commentaires

Walter Roberson
Walter Roberson le 10 Sep 2023
Modifié(e) : Walter Roberson le 11 Sep 2023
Note that MATLAB defines A.^B as being as if it were calculated as exp(B * log(A)) .
MATLAB is not required to use that calculation internally, and there is evidence (such as from timings) that it does not actually use that calculation in some circumstances, but that is the mathematical model that is followed.
When A is negative, then log(A) is log(-A)+log(-1) which is log(-A)+1i*pi . Multiply that by 1/3 for cube root and you get an imaginary component that is not an integer multiple of pi. exp() of a number that has an imaginary component that is not an integer multiple of pi gives a complex result.
Therefore, it is not a bug that (-1).^(1/3) gives a complex result: it follows from the MATLAB definition of what the .^ operator does. It provides a useful and consistent meaning to the .^ operator.
If you happen to be taking a negative number to 1/N where N is an odd positive integer, and you want the real-valued result, then @Sam Chak suggestion of nthroot is the way to go.
Bruno Luong
Bruno Luong le 10 Sep 2023
Modifié(e) : Bruno Luong le 10 Sep 2023
@Walter Roberson "log(A) is log(-A)+log(-1) "
Are you sure this is correct? I mean what rigouruous identity allows you to write this?
Look at this: If I do the same, but now for A>0
log(A) is log(-(-A)) = log(-A)+log(-1) = log(A) + log(-1) + log(-1) = log(A) + 1i*pi + 1i*pi
so 1i*2*pi must be 0 !!!
Obviously you cannot apply without care the identity log(A*B) = log(A)+log(B) for any A, B that are complex numbers.
This identify can only to be true (without careful checking) for real positive numbers (or more generally complex with real part positives).
Walter Roberson
Walter Roberson le 10 Sep 2023
Modifié(e) : Walter Roberson le 10 Sep 2023
I put in the condition that when A is negative
A = -rand(1,1e6) * 100000;
l1 = log(A);
l2 = log(-A) + log(-1);
nnz(l1 ~= l2)
ans = 0
Bit for bit equality.
I know it is true for negative A, but readers might wonder out the blue where this come from: "log(A) is log(-A)+log(-1)"?
And next why log(-1) is 1i*pi (and not -1i*pi)? Of course one can check it with MATLAB command.
The MATLAB log doc states
log(A) = log(abs(A)) + 1i*angle(A)
why not start with that?
And btw log(A) = log(abs(A)) + 1i*angle(A) is not entirely true either in some special values of A.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by