useprxf
useprxf

Reputation: 269

Real root in MATLAB

MATLAB code:

x^(1/3)

If x = -1 then the result is 0.5000 + 0.8660i, but I only want the real root -1.

How do I modify the above expression?

PS: I don't want such a solution:

sign(x)*(abs(x))^(1/3)

Upvotes: 0

Views: 919

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112759

Using ^ produces only one root. From Mathworks,

The root returned by ^ is the one with the smallest absolute phase angle returned from the angle function. When two values are equal in absolute phase angle, then the ^ operator returns the positive one.

To find the real root use nthroot:

Y = nthroot(X,N) returns the real nth root of the elements of X. Both X and N must be real scalars or arrays of the same size. If an element in X is negative, then the corresponding element in N must be an odd integer.

Example:

>> nthroot(-1, 3)
ans = 
    -1

Upvotes: 4

EelkeSpaak
EelkeSpaak

Reputation: 2835

You will probably have to use nthroot:

>> nthroot(-1, 3)

ans =
    -1

Upvotes: 4

Related Questions