在Matlab中绘制循环结果 [英] Plotting a result from a loop in matlab
本文介绍了在Matlab中绘制循环结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个关于在Matlab中绘制循环结果的问题.这是代码.
I have a question about plotting a result from a loop in Matlab. Here is the code.
for t=0:1:10;
VS=3*exp(-t/3)*sin(pi*t);
if VS>0
VL=VS
else VL=0
end
end
plot(t,VL)
xlabel('Time(s)')
ylabel('Across Voltage(V)')
title('Across Voltage Vs Time')
我想根据上述表达式中的电压与从0到10的时间之间的关系绘制一个图.但是,在运行代码后,该图仍然没有显示任何内容.谁能帮我找出原因?
I wanna plot a figure based on the voltage from the above expression versus the time through 0 to 10. However, the figure keeps showing nothing after running the code. Can anyone help me figure out why?
推荐答案
这是因为您正在绘制for
循环之外的图形.因此,在那个时候,t不是您期望的向量,而是标量值t=10
.另外,VL
取决于执行情况.因此,您应该将t
形成为向量,并执行以下操作:
This is because you are plotting outside the for
loop. So at that time t is not a vector as you expect, its a scalar value, t=10
. Also, VL
depends on the execution. So you should form t
as a vector and do the following:
k=0;
for t=0:1:10;
k=k+1;
VS=3*exp(-t/3)*sin(pi*t);
if VS>0
VL(k,1)=VS
else VL(k,1)=0
end
end
plot(0:10,VL)
xlabel('Time(s)')
ylabel('Across Voltage(V)')
title('Across Voltage Vs Time')
这篇关于在Matlab中绘制循环结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文