Reference no: EM132171580
Using Matlab, write a function called fare that computes the bus fare one must pay in a given city based on the distance travelled.
Here is how the fare is calculated: the first mile is $2.
Each additional mile up to a total trip distance of 10 miles is 25 cents. Each additional mile over 10 miles is 10 cents.
Miles are rounded to the nearest integer other than the first mile which must be paid in full once a journey begins.
Children 18 or younger and seniors 60 or older get a 20% discount. The inputs to the function are the distance of the journey and the age of the passenger in this order. Return the fare in dollars, e.g., 2.75 would be the result returned for a 4-mile trip with no discount.
A Transtutors contributor provided this code which works in all instances except where the miles are less than 1 but more than 0.
What needs to be adjuted to yield the correct value when miles are less than 1 (e.g. 0.22)?
function fareDollars = fare(miles, age)
if miles <= 0
fareDollars = 0;
else
remMiles = round(miles - 1);
fareDollars = 2;
if remMiles <= 10
fareDollars = fareDollars + (remMiles * 0.25);
else
fareDollars = fareDollars + (9 * 0.25) + ( (remMiles - 9) * 0.10 );
end
if age <= 18 || age >= 60
fareDollars = fareDollars - (fareDollars * 0.20);
end
end
end