我正在使用这个代码来绘制单位阶跃函数
t1=-2:0.01:2;
if t1>=0
y=1;
else if t1<0
y=0;
end
end
subplot(3,1,1)
plot(t1,y)`但是我没有得到期望的output.It是在每个点上为y绘制0。
发布于 2013-10-01 03:48:40
您的代码一次测试所有t1值,并且与
if all(t1>=0)
y=1;
else if all(t1<0)
y=0;
end
end你想要的是:
t1=-2:0.01:2;
y = zeros(size(t1));
y(t1>=0) = 1;
subplot(3,1,1)
plot(t1,y)另一种(效率较低的)方法是:
t1=-2:0.01:2;
for index=1:length(t1)
if t1(index)>=0
y(index)=1;
else if t1(index)<0
y(index)=0;
end
end
end
subplot(3,1,1)
plot(t1,y)正如P0W所指出的,还有heaviside函数,它生成与您的步骤输出类似的阶跃输出(尽管值为0,但值为0.5 )。但是,仅当安装了symbolic工具箱时,heaviside函数才可用。
https://stackoverflow.com/questions/19102492
复制相似问题