The set of roots in the complex plane with R

I am trying to find a function that returns all complex solutions of an equation, such as:

16^(1/4) = 2+i0, -2+i0, 0+i2, 0-i2 

Be that as it may, if I type 16^(1/4) into the console, it will only return 2. I can write a function for this, but I was wondering if there is an easy way to do this in R.

+6
source share
1 answer

You need polyroot() :

 polyroot(z = c(-16,0,0,0,1)) # [1] 0+2i -2-0i 0-2i 2+0i 

Where z is the "vector of polynomial coefficients in ascending order."

Vector I, passed in z in the above example, is a compact representation of this equation:

 -16x^0 + 0x^1 + 0x^2 + 0x^3 + 1x^4 = 0 x^4 - 16 = 0 x^4 = 16 x = 16^(1/4) 

Edit:

If the polyroot syntax polyroot you, you can write a wrapper function that offers you a more convenient (if not universal) interface:

 nRoot <- function(x, root) { polyroot(c(-x, rep(0, root-1), 1)) } nRoot(16, 4) # [1] 0+2i -2-0i 0-2i 2+0i nRoot(16, 8) # [1] 1.000000+1.000000i -1.000000+1.000000i -1.000000-1.000000i # [4] 1.000000-1.000000i 0.000000+1.414214i -1.414214-0.000000i # [7] 0.000000-1.414214i 1.414214+0.000000i 
+10
source

All Articles