急!matlab,如何判断一个函数在是不是大于0的?
来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/07/07 07:49:26
急!matlab,如何判断一个函数在是不是大于0的?
程序如下,不知道哪里错了,不能显示第二个图形
for t=0:20;
if sin(t)>0
y=1;
else
y=-1;
end
end
subplot(2,1,1)
t=0:0.1:20;
plot(t,sin(t))
axis([0 20 -1.5 1.5])
subplot(2,1,2)
t=0:0.1:20;
plot(t,y)
axis([0 20 -1.5 1.5])
程序如下,不知道哪里错了,不能显示第二个图形
for t=0:20;
if sin(t)>0
y=1;
else
y=-1;
end
end
subplot(2,1,1)
t=0:0.1:20;
plot(t,sin(t))
axis([0 20 -1.5 1.5])
subplot(2,1,2)
t=0:0.1:20;
plot(t,y)
axis([0 20 -1.5 1.5])
错在y是一个数而不是一个向量,得不到对应时刻t所得的y值,程序修改为:i=1;for t=0:0.1:20;%此处与画图t对应 if sin(t)>0 y(i)=1; i=i+1; else y(i)=-1; i=i+1; endendsubplot(2,1,1)t=0:0.1:20;plot(t,sin(t))axis([0 20 -1.5 1.5])subplot(2,1,2)t=0:0.1:20;plot(t,y)axis([0 20 -1.5 1.5])结果如图: