cube root of -1

3 次查看(过去 30 天)
Victor Hugoson
Victor Hugoson 2022-2-3
Why does the cube root of -1 not equal -1 in matlab. Instead you have to use nthroot, why is that?

回答(3 个)

Rik
Rik 2022-2-3
The nth root of a value has n values:
x=-1;
root=3;
R=abs(x);
theta=angle(x);
k=linspace(0,2,root+1);k(end)=[];
new_R=R^(1/root);
new_theta=theta/root+pi*k;
z=( new_R.*exp(1i*new_theta) ).' % flip for visual clarity
z =
0.5000 + 0.8660i -1.0000 + 0.0000i 0.5000 - 0.8660i
%confirm result
z.^3,imag(z.^3)/eps
ans =
-1.0000 + 0.0000i -1.0000 + 0.0000i -1.0000 + 0.0000i
ans = 3×1
1.7500 1.6546 10.2500
As you can see, the three values are all -1 (with a rounding error). The nthroot function just guarantees to return the real root.

Walter Roberson
Walter Roberson 2022-2-3
In MATLAB, the .^ operator is defined as
A.^B == exp(log(A)*B)
When A is negative, log(A) is (log(-A) + sqrt(-1)*pi) so you get
exp(log(-A)*B + sqrt(-1)*pi*B) --->
(-A)^B * exp(1i*pi*B)
when B is an even integer, the exp(1i*pi*B) is +1 and the (-A)^B is the same as A^B giving a positive result.
When B is an odd integer, the exp(1i*pi*B) is -1 and the (-A)^B is -(A^B) but multiply by the -1 from the exp(1i*pi*B) to get an overall result of A^B giving a negative result (assuming negative A) .
Sor for integer B, A^B with A positive or negative gives the expected real result with no complex parts.
But when B is fractional like 1/3 then the exp(1i*pi*B) is the (1/B)'th root of -1which is complex and you get a complex result.
Why was .^ defined like that? Because it gives a nice continuous result that makes for consistent calculations? Instead of MATLAB having to figure out in A.^B that B is exactly 1/k for some integer and then take nthroot(A,k) . With floating point numbers, 1/k does not have an exact representation in binary floating point unless k is a power of 2, so the detection would be messy to get right for the 1/k case while still properly giving complex numbers for the cases where the floating point B differs from 1/k for integer k by at least one bit, as you can see that (-1)^(1/(3+delta)) should not be real-valued for delta non-integer ...

John D'Errico
John D'Errico 2022-2-3
编辑:John D'Errico 2022-2-3
Remember there are THREE cube roots of -1. -1 is not the only answer. In fact, it is not even the first solution to that problem you might find, depending on how you form the list. For example, we can use
syms x
rootsofminus1 = solve(x^3 == -1)
rootsofminus1 = 
So all three of those values will be cube roots of -1. Two of them are complex solutions.
vpa(rootsofminus1)
ans = 
We can also use roots.
format long g
roots([1 0 0 1])
ans =
-1 + 0i 0.5 + 0.866025403784438i 0.5 - 0.866025403784438i
What does MATLAB return by default when you just raise -1 to the indicated power?
(-1)^(1/3)
ans =
0.5 + 0.866025403784439i
It actually finds the third solution that solve returned in its list of roots (but the second in the set produced by roots.) So while you might think of -1 as the root you would expect, it is not the root others might expect as most natural. Not everyone will be happy with any choice that was made. The world is home to many different people, all of whom think differently.
And this is why nthroot is provided, since it will return -1 for those people who want that to be the solution they see.
nthroot(-1,3)
ans =
-1

标签

Community Treasure Hunt

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

Start Hunting!

Translated by