📜  在 R 编程中查找多项式的根或零点 - polyroot()函数

📅  最后修改于: 2022-05-13 01:55:02.860000             🧑  作者: Mango

在 R 编程中查找多项式的根或零点 - polyroot()函数

R 语言中的polyroot()函数用于计算多项式方程的根。
多项式方程表示为,

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

示例 1:

# 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)

输出:

[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

示例 2:

# 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))

输出:

[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