MATLAB: Why the output of this calculation over 300 is wrong

calculation

disp('Total cost in $')
a=0.5;
b=0.3;
c=0.2;
x=input('Enter the travel distance (km): ');
if x<=100
cost=x*a
elseif 100<x<=300
cost=50+((x-100)*b)
else
cost=110+((x-300)*c)
end
If I input 500 the output will be 170 instead of 150 I don't know why but matlab always counts with the previous formula (elseif 100<x<=300)

Best Answer

100<x<=300
Is not what you think, replace it with
100<x && x<=300