【问题标题】:Octave / Matlab: How to plot the roots of a polynomialOctave / Matlab:如何绘制多项式的根
【发布时间】:2010-03-14 04:42:34
【问题描述】:

我试图绘制多项式的根,但我无法得到它。

首先我创建多项式

p5 = [1 0 0 0 0 -1] %x^5 - 1
r5 = roots(p5)
stem (p5)

我正在使用stem 函数,但我想去掉茎,然后只在根周围画一个圆圈。

这可能吗,stem 是正确的命令吗?

提前致谢,

PS:这不是作业,但很接近,如果需要,会标记它。

【问题讨论】:

    标签: math matlab octave


    【解决方案1】:

    如果您想绘制复数根,实部在 x 轴上,虚部在 y 轴上,您可以使用 PLOT 函数:

    plot(r5,'o');
    

    如果您想将函数 根绘制在一起,则必须忽略复杂的根(正如 yuk 在下面的评论中提到的那样):

    p5 = [1 0 0 0 0 -1];
    r5 = roots(p5);
    realRoots = r5(isreal(r5));  %# Gets just the real roots
    x = -2:0.01:2;               %# x values for the plot
    plot(x,polyval(p5,x));       %# Evaluate the polynomial and plot it
    hold on;                     %# Add to the existing plot
    plot(realRoots,zeros(size(realRoots)),'o');  %# Plot circles for the roots
    

    【讨论】:

    • 请注意,r5 包含复数,并且绘图将忽略虚部。要绘制真正的根,你可以这样做: plot(r5(imag(r5)==0),zeros(sum(imag(r5)==0)),'o');
    • @yuk:很好。我使用 ISREAL 函数更新了答案。 ;)
    • @yuk, gnovice:谢谢你们的帮助,但是我将如何绘制所有根(img 和 real)?
    • @Tom:我的新答案的第一部分展示了如何将所有根的实部和虚部绘制在一起。
    • @gnovice,谢谢,最终使用 plot (real(r5),imag(r5),'o')
    猜你喜欢
    • 2022-01-25
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多