cube root of -1
3 次查看(过去 30 天)
显示 更早的评论
Why does the cube root of -1 not equal -1 in matlab. Instead you have to use nthroot, why is that?
0 个评论
回答(3 个)
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
%confirm result
z.^3,imag(z.^3)/eps
As you can see, the three values are all -1 (with a rounding error). The nthroot function just guarantees to return the real root.
0 个评论
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 ...
0 个评论
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)
So all three of those values will be cube roots of -1. Two of them are complex solutions.
vpa(rootsofminus1)
We can also use roots.
format long g
roots([1 0 0 1])
What does MATLAB return by default when you just raise -1 to the indicated power?
(-1)^(1/3)
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)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

