MATLAB绘图

MATLAB绘图,matlab,plot,Matlab,Plot,我正在尝试使用以下代码绘制蛛网图: function cobweb(f,a,b,x0,x1,N) x(1)=0.2; % plot orbit starting at x0 for i=1:100 x(i+1)= 3*x(i)*(1-x(i)); plot([x(i),x(i)],[x(i),x(i+1)]); hold on plot([x(i),x(i+1)],[x(i+1),x(i+1)]); hold on end hold on r = 3;

我正在尝试使用以下代码绘制蛛网图:

function cobweb(f,a,b,x0,x1,N)
x(1)=0.2; % plot orbit starting at x0
for i=1:100
    x(i+1)= 3*x(i)*(1-x(i));
    plot([x(i),x(i)],[x(i),x(i+1)]);
    hold on
    plot([x(i),x(i+1)],[x(i+1),x(i+1)]);
    hold on
end

hold on
r = 3;
x = 0:0.01:1; %// set some x
f = (r.*x.*(1-x));
hold on
plot(x,f,'k')
hold on
plot([x(1), 0], [x(1), 3*x(1)*(1-x(1))])
我希望第一条线是从x轴开始画的,但是它当前从(x(1),x(1))开始,其中x(1)是初始点

我知道这是因为我的循环,所以我尝试为初始的一行一行地添加一个额外的绘图([x(1),0],[x(1),3*x(1)*(1-x(1))),但是我仍然得到相同的结果


我正在绘制的函数是f=3x(1-x)。

您可能希望移动该行:

plot([x(1), 0], [x(1), 3*x(1)*(1-x(1))])

for
循环之前,在循环之后添加一个
按住
,然后再进入循环。然后,您可以删除
for
循环中的另一个
按住
,并进一步删除代码,因为它们是多余的。

我仍然得到相同的结果!我不确定我是否很理解你的问题,但我已经更新了我的答案。希望这就是你想要的。如果不是,你可能需要澄清你的问题,现在还不是很清楚。