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

17 Ansichten (letzte 30 Tage)
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:

Antworten (1)

Star Strider
Star Strider am 8 Feb. 2015
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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by