【问题标题】:Plotting multiple equations in MATLAB在 MATLAB 中绘制多个方程
【发布时间】:2018-02-02 19:05:30
【问题描述】:

我想根据不同的 alpha 值绘制多个函数。我的功能是:x^3+x+y+(alpha)*y=0

ezplot('x.^3 + x + y + y', [-10 10 -10 10]);
hold on
ezplot('x.^3 + x + y + 2*y', [-10 10 -10 10]);
hold on
ezplot('x.^3 + x + y + 3*y', [-10 10 -10 10]);
hold on
ezplot('x.^3 + x + y + 4*y', [-10 10 -10 10]);
...

当我使用 for 循环编写代码时,它的工作与上面显示的代码相同,绘图不显示任何内容。我不想只是根据 100 个不同的 alpha 值复制和粘贴来获得 100 个函数。那么以这种方式,我怎样才能使用循环来实现这段代码呢?

图中显示了上面显示的四个等式。

【问题讨论】:

  • 使用 fplot insted of ezplot
  • 所以你没有显示你遇到问题的代码,然后你说你毕竟没有问题,没有问题可以看到。你为什么不试着慢慢解释一下:你想要什么,你尝试了什么,你得到了什么,你得到的有什么问题。
  • 你需要多少个 alpha 值?您可能应该避免使用ezplot,而只使用plot。当然,您必须生成x,y 数据。我还建议重述等式,例如y = f(x),例如:y = @(x,alpha)-(x.^3+x)./(1+alpha);
  • ezplot() 自动调整绘图限制,使早期绘图的结果可能仅部分可见(或完全超出范围)。

标签: matlab plot graph


【解决方案1】:

正如 cmets 中所述,MATLAB 不建议使用 ezplot。如果您使用的是 MATLAB R2017b,则可以使用 fimplicit。如果您不这样做,那么您可以同时使用fplotplot 作为替代方案。然而,两者都需要明确的形式。那么后两者的代码是:

对于fplot

for alpha=1:100
    y = @(x) -(x.^3+x)/(1+alpha);
    fplot(y,[-10 10])
    ylim([-10 10])
    hold on
end
hold off

对于plot

x = -10:.1:10;

for alpha=1:100
    y = -(x.^3+x)/(1+alpha);
    plot(x,y)
    ylim([-10 10])
    hold on
end
hold off

我自己没有MATLAB R2017b,所以无法测试代码,但是如果你想用fimplicit,我觉得是这样的:

for alpha=1:100
    fimplicit(@(x,y) x.^3 +x + (1+alpha)*y, [-10 10 -10 10])
    hold on
end
hold off

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多