I'm trying to plot a simple piecewise defined function in MATLAB R2016a. While t is negative, this code should plot v = 0
and when t is positive (or zero), the code should plot v = 10*exp(-5000*t)
. Here's the code:
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)
This m-file looks right to me, but I keep getting the error
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);
I suspect it's something simple, but I just don't see it!
问题上的注释可以回答问题,但是如果将其向量化,则可以使此代码更简单:
v = 10*exp(-5000*t).*(t >= 0);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.