How do I plot this piecewise function WITHOUT using any LOGIC operators?

15 次查看(过去 30 天)
Hello, I am having trouble plotting this piecewise function:
I do not know what I'm doing wrong. If someone can help me out, I would really appreciate it. Here is my code.
t=[-5:75/99:70];
for i=1:length(t)
if t(i)>=0
V(i)=10*((t(i))^2)-0.5*t(i);
elseif t(i)<8
V(i)=10*((t(i))^2)-0.5*t(i);
elseif t(i)>=8
V(i)=676-5*t(i);
elseif t(i)<16
V(i)=676-5*t(i);
elseif t(i)>=16
V(i)=20+36*t(i)+12*((t(i)-16)^2);
elseif t(i)<26
V(i)=20+36*t(i)+12*((t(i)-16)^2);
elseif t(i)>=26
V(i)=2156*exp(-.1*(t(i)-26));
else
V(i)=0;
end
end
plot(t,V)
Here is what my output is:
And this is what it is supposed to look like:

回答(1 个)

Star Strider
Star Strider 2015-2-8
I can’t imagine doing it without logic statements, but with that caveat, this is how I would do it:
v = @(t) [(10*t.^2-0.5*t).*((0<=t) & (t<8)) + (676-5*t).*((8<=t) & (t<16)) + (20+36*t+12*(t-16).^2).*((16<=t) & (t<26)) + (2156*exp(-0.1*(t-26))).*(t>=26)];
t=[-5:75/99:70];
vt = v(t);
figure(1)
plot(t, vt)
grid

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by