MATLAB: I am supposed to plot the given figure on matlab

plotramp functionunit step

This is what I wrote:
t= -2:0.002:2; f=@(t) (-(t>-2)+t.*(t>-2)-t.*(t>-1)+(t>-1)+(t>0)-(t>1)-t.*(t>1)); plot(t,f(t));
I'm unable to get the correct output.

Best Answer

Try this
t= -2:0.002:2;
f=@(t) (t>-2 & t<=-1).*(t+1) + ...
(t>-1 & t<=0).*1 + ...
(t>0 & t<=1).*2 + ...
(t>1 & t<=2).*(-t+2);
plot(t,f(t));
Related Question