MATLAB: I am getting an error in the code stating that,”Operands in the operator || and && must be convertible to scalar”. I am very new to programming so can’t really find out where the mistake is

errorlogical errorMATLAB

function K = Force(t)
if (0 <= t)&&(t < 0.015)
K = (58600180.67)*t + 66012.93;
elseif (0.015 <= t)&&(t <= 0.0682)
K = 945015.64;
else
K = (-55633369.71)*t + 4739211.454;
end
plot(t,K)

Best Answer

The error message occurs, if the input t is not a scalar, because the && operator requires scalars. Use & for arrays.
But then the if commands will not help, because they require scalar conditions also. Use logical indexing instead for arrays:
function K = Force(t)
K = -55633369.71 * t + 4739211.454;
index = (0 <= t) & (t < 0.015);
K(index) = 58600180.67 * t(index) + 66012.93;
index = (0.015 <= t) & (t <= 0.0682)
K(index) = 945015.64;
plot(t,K)
end