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:
# R program to find zeros of a polynomial # Creating vectors of coefficientsx1 <- c(1, 2, 3)x2 <- c(-8, 4, -2)x3 <- c(12, -2, 3) # Calling polyroot() functionpolyroot(x1)polyroot(x2)polyroot(x3) |
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:
# R program to find zeros of a polynomial # Calling polyroot() function # For equation 2x - 3 = 0polyroot(c(-3, 2)) # For equation 3x ^ 2 - 4x + 5 = 0polyroot(c(5, -4, 3)) # For equation 2x ^ 4 - 3x -12 = 0polyroot(c(-12, -3, 0, 2)) |
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


