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:
How to get all roots of a square root?

Subject: How to get all roots of a square root?

From: Yuheng

Date: 5 Mar, 2012 02:50:13

Message: 1 of 8

For example.

Matlab returns 0+1.0000i for

>> sqrt(-1)

what should I do if I want to get both 0+1.0000i and 0-1.0000i?

Thanks a lot!

Subject: How to get all roots of a square root?

From: Nasser M. Abbasi

Date: 5 Mar, 2012 03:00:44

Message: 2 of 8

On 3/4/2012 8:50 PM, Yuheng wrote:
> For example.
>
> Matlab returns 0+1.0000i for
>
>>> sqrt(-1)
>
> what should I do if I want to get both 0+1.0000i and 0-1.0000i?
>
> Thanks a lot!

by convention, most programs return the positive extraction of the root.
(this is called the principal root).

sqrt(4) gives 2, not 2 AND -2 even though both are valid square roots
for the number 4.

I do not know right now how to tell Matlab to give the non-principal root
and that is possible. May be someone else does.

hth,

--Nasser

Subject: How to get all roots of a square root?

From: Roger Stafford

Date: 5 Mar, 2012 03:43:11

Message: 3 of 8

"Yuheng" wrote in message <jj19l5$q4p$1@newscl01ah.mathworks.com>...
> For example.
> Matlab returns 0+1.0000i for
> >> sqrt(-1)
> what should I do if I want to get both 0+1.0000i and 0-1.0000i?
> Thanks a lot!
- - - - - - - -
  The square root function has just two branches and the two possible roots for a square root are always sqrt(z) and -sqrt(z). This is true for complex numbers as well as reals. The real part of matlab's sqrt(z) is always non-negative and for -sqrt(z) always non-positive. The branch cut is along the negative real axis.

Roger Stafford

Subject: How to get all roots of a square root?

From: Yuheng

Date: 5 Mar, 2012 06:39:10

Message: 4 of 8

"Roger Stafford" wrote in message <jj1cof$5j4$1@newscl01ah.mathworks.com>...
> "Yuheng" wrote in message <jj19l5$q4p$1@newscl01ah.mathworks.com>...
> > For example.
> > Matlab returns 0+1.0000i for
> > >> sqrt(-1)
> > what should I do if I want to get both 0+1.0000i and 0-1.0000i?
> > Thanks a lot!
> - - - - - - - -
> The square root function has just two branches and the two possible roots for a square root are always sqrt(z) and -sqrt(z). This is true for complex numbers as well as reals. The real part of matlab's sqrt(z) is always non-negative and for -sqrt(z) always non-positive. The branch cut is along the negative real axis.
>
> Roger Stafford

I know it is not hard to calculate the roots manually, espicially for square root. I just wonder if there is any existing function in matlab to do it.

Subject: How to get all roots of a square root?

From: Nasser M. Abbasi

Date: 5 Mar, 2012 07:02:21

Message: 5 of 8

On 3/5/2012 12:39 AM, Yuheng wrote:

>
> I know it is not hard to calculate the roots manually, espicially for
>square root. I just wonder if there is any existing function in matlab to do it.

Yes, there is :


EDU>> allRoots=@(x) [sqrt(x) -sqrt(x)]
EDU>> allRoots(sqrt(-1))

    0.7071 + 0.7071i -0.7071 - 0.7071i

EDU>> allRoots(4)
      2 -2

--Nasser

Subject: How to get all roots of a square root?

From: Roger Stafford

Date: 5 Mar, 2012 07:58:11

Message: 6 of 8

"Yuheng" wrote in message <jj1n2e$4iv$1@newscl01ah.mathworks.com>...
> I know it is not hard to calculate the roots manually, espicially for square root. I just wonder if there is any existing function in matlab to do it.
- - - - - - - - -
  This will give you all n of the n-th roots of z:

 r = roots([1,zeros(1,n-1),-z]);

Roger Stafford

Subject: How to get all roots of a square root?

From: Yuheng

Date: 7 Mar, 2012 01:01:12

Message: 7 of 8

"Nasser M. Abbasi" <nma@12000.org> wrote in message <jj1oe2$3um$1@speranza.aioe.org>...
> On 3/5/2012 12:39 AM, Yuheng wrote:
>
> >
> > I know it is not hard to calculate the roots manually, espicially for
> >square root. I just wonder if there is any existing function in matlab to do it.
>
> Yes, there is :
>
>
> EDU>> allRoots=@(x) [sqrt(x) -sqrt(x)]
> EDU>> allRoots(sqrt(-1))
>
> 0.7071 + 0.7071i -0.7071 - 0.7071i
>
> EDU>> allRoots(4)
> 2 -2
>
> --Nasser

Thank you!

Subject: How to get all roots of a square root?

From: Yuheng

Date: 7 Mar, 2012 01:03:13

Message: 8 of 8

"Roger Stafford" wrote in message <jj1rmj$hbg$1@newscl01ah.mathworks.com>...
> "Yuheng" wrote in message <jj1n2e$4iv$1@newscl01ah.mathworks.com>...
> > I know it is not hard to calculate the roots manually, espicially for square root. I just wonder if there is any existing function in matlab to do it.
> - - - - - - - - -
> This will give you all n of the n-th roots of z:
>
> r = roots([1,zeros(1,n-1),-z]);
>
> Roger Stafford

Excellent, that's really helpful!

Tags for 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