Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Matlab wrong cubic root

Subject: Matlab wrong cubic root

From: Nasser M. Abbasi

Date: 15 Dec, 2012 19:54:03

Message: 1 of 2

On 12/15/2012 12:19 PM, sergio_r@mail.com wrote:
> Hello all,
>
> Is there a way of telling matlab to return first a real root
> instead of a complex one?
>
> For some strange reason matlab seems not to handle cubic roots
> properly. Apparently the default behaviour is to first
> report a complex root. Let's see two examples:
>
>>> (-1/2)^(1/3)
>
> ans =
>
> 0.3969 + 0.6874i
>
>>> (-1/2)^(2/3)
>
> ans =
>
> -0.3150 + 0.5456i
>
> Thanks in advance.
>

I think that is sort of convention. I am not sure now.
They pick one value, principal one I guess, and that is
the one picked.

But you could always use solve if you want all roots?

--------------------------
double(solve(x^3+1/2,x))

ans =

   -0.7937
    0.3969 + 0.6874i
    0.3969 - 0.6874i
-----------------------------

ans.^3

   -0.5000
   -0.5000 + 0.0000i
   -0.5000 - 0.0000i


--Nasser

Subject: Matlab wrong cubic root

From: Roger Stafford

Date: 16 Dec, 2012 01:57:11

Message: 2 of 2

> On 12/15/2012 12:19 PM, sergio_r@mail.com wrote:
> > Hello all,
> >
> > Is there a way of telling matlab to return first a real root
> > instead of a complex one?
> >
> > For some strange reason matlab seems not to handle cubic roots
> > properly. Apparently the default behaviour is to first
> > report a complex root. Let's see two examples:
> >
> >>> (-1/2)^(1/3)
> >
> > ans =
> >
> > 0.3969 + 0.6874i
> >
> >>> (-1/2)^(2/3)
> >
> > ans =
> >
> > -0.3150 + 0.5456i
> >
> > Thanks in advance.
- - - - - - - - - -
  Matlab's answer to cube roots is consistent with its handling of the n-th roots of complex numbers. If you have a complex number in polar form

 z = r*exp(theta*i), r >=0, -pi < theta <= pi,

the principal n-th root returned is this

 z^(1/n) = r^(1/n)*exp(theta/n*i)

just as you would expect. If you follow this rule where z is real but negative as with your z = -1/2, you get

 -1/2 = 1/2*exp(pi*i) <-- This is the standard polar form

 (-1/2)^(1/3) = (1/2)^(1/3)*exp(pi/3*i) = 0.396850263 + 0.687364819*i

It does make sense.

  If you want the real cube root of any real number x, positive or negative, you can do it with

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

which is admittedly awkward but it works.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us