我正在尝试在MATLAB R2016a中绘制一个简单的分段定义函数。虽然t为负数,但此代码应绘制v = 0
,当t为正(或零)时,代码应绘制v = 10*exp(-5000*t)
。这是代码:
t = -0.0014:1e-5:0.0014;
v = zeros(1, length(t));
for i = 1:length(t)
if t(i) < 0
v(i) = 0;
elseif t(i) >= 0
v(i) = 10*exp(-5000*t);
end
end
plot(t, v)
这个m文件对我来说很合适,但我一直收到错误
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in PiecewiseFunction (line 10)
v(i) = 10*exp(-5000*t);
我怀疑它很简单,但我只是看不到它!
答案 0 :(得分:2)
对该问题的评论回答了这个问题,但是如果你对它进行矢量化,你可以使这个代码更简单:
v = 10*exp(-5000*t).*(t >= 0);