MATLAB: I am getting an error as “Operands to the || and && operators must be convertible to logical scalar values. Error in xd (line 8) if (t>=t1)&&(t

displacement

my code is
t1 = 0;
t2 = 10;
t3 = 20;
t4 = 30;
t = 0:50;
if (t>=t1)&&(t<t2)
r = 0.6;
if (t>=t2)&&(t<t3)
r = 0.3;
if (t>=t3)&&(t<t4)
r = 0.3;
if (t>=t4)
r = 0.6;
end
end
end
end
plot(t,r)

Best Answer

You cannot compare a vector t (which is 1x51 double) with a single value for an if statement, with the logical operators. Even if you could, the plot at the end won't give anything. I believe you want to do this.
t1 = 0;
t2 = 10;
t3 = 20;
t4 = 30;
t = 0:50;
r = zeros(length(t),1);
for i = 1:length(t)
if (t(i)>=t1)&&(t(i)<t2)
r(i) = 0.6;
elseif (t(i)>=t2)&&(t(i)<t3)
r(i) = 0.3;
elseif (t(i)>=t3)&&(t(i)<t4)
r(i) = 0.3;
else
r(i) = 0.6;
end
end
plot(t,r)
axis([0 60 0.2 0.7])