Find roots or zeros of a Polynomial in R Programming – polyroot() Function

polyroot() function in R Language is used to calculate roots of a polynomial equation.
A polynomial equation is represented as,

p(x) = (z1) + (z2 * x) + (z3 * x2) +...+ (z[n] * xn-1)

Syntax: polyroot(z)

Parameters:
z: Vector of polynomial coefficients in Increasing order

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to find zeros of a polynomial
  
# Creating vectors of coefficients
x1 <- c(1, 2, 3)
x2 <- c(-8, 4, -2)
x3 <- c(12, -2, 3)
  
# Calling polyroot() function
polyroot(x1)
polyroot(x2)
polyroot(x3)

chevron_right


Output:

[1] -0.3333333+0.4714045i -0.3333333-0.4714045i
[1] 1+1.732051i 1-1.732051i
[1] 0.333333+1.972027i 0.333333-1.972027i

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to find zeros of a polynomial
  
# Calling polyroot() function
  
# For equation 2x - 3 = 0
polyroot(c(-3, 2))
  
# For equation 3x ^ 2 - 4x + 5 = 0
polyroot(c(5, -4, 3))
  
# For equation 2x ^ 4 - 3x -12 = 0
polyroot(c(-12, -3, 0, 2))

chevron_right


Output:

[1] 1.5+0i
[1] 0.666667+1.105542i 0.666667-1.105542i
[1]  2.090489+0.000000i -1.045244+1.333269i -1.045244-1.333269i
My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.