MATLAB: Error: Unexpected MATLAB expression.

#basicproblemsbus farehomework

This will work
function p=fare(d,a)
b=round(d);
if (b<=0)
f=2;
elseif (b==1)
f=2;
elseif (1<b&& b<=10)
f=2+(b-1)*0.25;
elseif (b>10)
f = 4.25+(b-10)*0.1;
end
if (a<=18||a>=60)
p=0.8*f;
else
p=f;
end

Best Answer

Hello, as a first remark, I would like to tell you that if it is possible for you to improve the preview of your code, since right now it looks illegible.
Regarding the problem, you have to consider this:
1.- the distance can be from 0 to positive infinite, so you have to think on distances going from 0 to 1 km. Which will give a fare of 2.
2.-From the distances going from 2 to 10 km, you will pay: 2 + ((distance-1)*0.25)
3.-For distances above 11 km, then you must pay: 2+(9*0.25)+((distance-1)*0.1) --> or 4.25+((distance-1)*0.1)
NOTE: Remember that you must consider to round the distance to its closest integer. So you CAN'T use the "fix" function. Try with the "round" one, since for a 1.6 km distance, it should be rounded to a 2 km one.
Once you have calculated the cost-distance rate, calculate the possible discount:
4.-If age >= 60 or age <= 18, apply discount
5.-Else: no discount applied.
6.-Calculate the final fare to pay.
Enjoy :)