Using for loop and if statement, write a MATLAB code to plot a graph for x(t) as a function of time t in the range 0 to 12 in increment of 0.01
?(?) =
1: 0 ≤ ? ≤ 1
2? − 1 1 ≤ ? ≤ 2
3 2 ≤ ? ≤ 3
−2.5? + 10.5 3 ≤ ? ≤ 5
−2 5 ≤ ? ≤ 6
4/3 ? − 10 6 ≤ ? ≤ 9
2 9 ≤ ? ≤ 12
A graph for the given function is plotted in MATLAB in the range 0 to 12 with an increment of 0.01. The graph as well as the MATLAB code are provided below:
MATLAB code:
t=0:0.01:12;
x=0*t;
for i=1:numel(t)
if t(i)>=0 && t(i)<=1
x(i)=1;
elseif t(i)>=1 && t(i)<=2
x(i)=2*t(i)-1;
elseif t(i)>=2 && t(i)<=3
x(i)=3;
elseif t(i)>=3 && t(i)<=5
x(i)=-2.5*t(i)+10.5;
elseif t(i)>=5 && t(i)<=6
x(i)=-2;
elseif t(i)>=6 && t(i)<=9
x(i)=4*t(i)/3-10;
elseif t(i)>=9 && t(i)<=12
x(i)=2;
end
end
plot(t,x);
xlabel('t')
ylabel('x(t)')
title('t vs x(t)')
axis([min(t) max(t) min(x)-.5 max(x)+.5]);
Get Answers For Free
Most questions answered within 1 hours.