在MATLAB中绘制多个方程

时间:2018-02-02 19:05:31

标签: matlab plot graph

我想根据不同的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个函数。那么以这种方式我如何使用循环来实现这个代码?

Four Function Plotted

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

1 个答案:

答案 0 :(得分:3)

正如评论中所述,MATLAB并不建议使用foo。如果您正在使用MATLAB R2017b,那么您可以使用ezplot。如果您不这样做,则可以同时使用fimplicitfplot。但是,两者都需要一个明确的形式。后两者的代码是:

代表plot

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

我自己没有MATLAB R2017b,所以我无法测试代码,但是如果你想使用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 ,我认为它看起来像这样:

fimplicit