R语言 查找多项式的根或零 – polyroot() 函数
R语言中的 polyroot() 函数用于计算多项式方程的根。
一项多项式方程表示为:
p(x) = (z1) + (z2 * x) + (z3 * x2) +...+ (z[n] * xn-1)
语法: polyroot(z)
参数:
z: 递增的多项式系数矢量
例子 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