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

16 ビュー (過去 30 日間)
Seth Meris
Seth Meris 2015 年 2 月 8 日
回答済み: Star Strider 2015 年 2 月 8 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by