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:

댓글 수: 0
답변 (1개)
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!