How to tell Matlab to give real cube roots instead of complex ones?
65 views (last 30 days)
I am using symbolic math toolbox (Matlab2011b) to do some geometric computations to derive an expression for a geometric length. At some point of my calculations some cube roots are introduced and unfortunately Matlab seems to automatically simplify terms like (-1)^(1/3) to yield 0.5000 + 0.8660i.
I realize this is a correct solution, however i want my result to be real - is there a way to tell Matlab to always return the real cube root?
These imaginary ones are messing up my expressions...
(Also i wonder why matlab chooses to consider one of two complex roots instead of giving all three solutions or the real one...)
Eric on 7 Sep 2017
Edited: Eric on 7 Sep 2017
This is almost certainly occurring because MATLAB is doing the math by first converting to log space so that it can just multiply the exponent by the log of the argument and then convert it back. This is similar to how computers and calculators commonly do complicated arithmetic like multiplication (which becomes addition in log space) by first converting to log space, because adding logs is typically much easier, faster, and usually the only option available to the computer in the first place. The following is likely how MATLAB/the computer goes through the calculation:
(-1)^(1/3) = exp(exp( log(log(-1)) - log(3) ))
log(-1) = pi*i
log(pi*i) = 1.1447 + 1.5708i
log(3) = 1.0986
log(log(-1)) - log(3) = (1.1447 + 1.5708i) - (1.0986) = 0.0461 + 1.5708i
exp(0.0461 + 1.5708i) = exp(0.0461)*exp(1.5708i) = 1.0472i
exp(1.0472i) = 0.5 + 0.866i % A complex cube root of -1, along with its conjugate
Since the log of a negative number (namely -1 in this case) is going to be complex, this is what (almost certainly) makes the output of the operation also complex and why there won't be a setting to change it to default to real. Having said that, it is fairly easy to get the real answer of a cube root by doing something like
assuming your inputs are real. This takes advantage of what we already know to be true, namely that
(-x)^(1/3) = -(x^(1/3)) % For x>=0
and should, in theory, work for Ruye's issue, though I am no expert on symbolic forms and am almost certainly using a later MATLAB release (R2016a).
At this point, it should be worth mentioning that MATLAB already has a function called
which works for any root and will return a real result or bust. If you look at the code for nthroot(), you will see that it is essentially doing the above sign*abs method, but with fancy checking to see that the root will be real first.
(I know this is an old question, but I wanted to compile previous answers and give a plausible explanation for why it might give a complex number)
More Answers (4)
Ruye Wang on 16 Apr 2014
I have the same question. I need to evaluate a function in symbolic form, essentially like the following:
The output is one of the three roots: 0.5000 + 0.8660i
But what I need is the real cube root of x=-1 (which is -1).
I know I could get the real cube root if I explicitly hard code it like this:
However, how do I do this in the form of symbolic function, which is needed in the program?