[英]Plot Multiple Roots in R
I have an equation which I would like to solve for values of m
我有一个方程式,我想为
m
值求解
m^2+x*m+1=0
I know I can find these roots using polyroot(c(1,x,1))
. 我知道我可以使用
polyroot(c(1,x,1))
找到这些根。
I'd like to plot these roots in R for varying x
such that all the real roots of the polynomial are plotted for each value of x
. 我想将这些根在R中绘制为变化的
x
,以便为x
每个值绘制多项式的所有实根。
Note, the polynomial may become more complex than this, ruling out application of the quadratic or cubic formulas. 注意,多项式可能变得比这更复杂,排除了二次或三次公式的应用。
Any thoughts as to how I could accomplish this? 有关如何实现这一目标的任何想法?
Thanks! 谢谢!
I think this will do the trick. 我认为这样做会有所帮助。 Just set
X
to whatever values you want to evaluate. 只需将
X
设置为您要评估的任何值即可。
X <- seq(0, 10, length=21)
roots <- sapply(X, function(x) polyroot(c(1,x,1)))
roots[abs(Im(roots)) > 1e-10] <- NA
matplot(X, t(roots), pch=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.