Please solve the problem by using Matlab Write a function that computes parking
ID: 1995688 • Letter: P
Question
Please solve the problem by using Matlab
Write a function that computes parking fees for a garage with the fee structure given below. Your function should be named parking fee, take a single input argument (hours parked in garage), and return a single output argument (total fee for parking). A lost ticket is signified using a negative value for hours. Partial hours are charged as full hours, e.g. 5 hours and 20 minutes is charged as 6 hours. Thus, the output should always be an integer. Examples (understand the PARKING FEES): parked for 2 hours, pay $4 parked for 3 hours, pay $7 parked for 4 hours, pay $7 parked for 5 hours, pay $8 parked for 20 hours, pay $23 parked for 22 hours, pay $24 parked for 123 hours, pay $127 parked for 123 hours but lost the ticket, pay $163 You have been provided with a script that will automatically test your function for partial correctness (test_parking_fee. m on eCampus). You should run the script right now and note the errors or failures. Pick an error or a failed test, track it down, fix it, and then run the test script again. This is what is known as test-driven development (TDD): Write the tests first, then write the code to pass the tests.Explanation / Answer
Matlab Code:
function fees = parking_fee(hours)
if hours<0;
neg=1;
hours = -1*hours;
else
neg=0;
end
hours=ceil(hours);
if hours>0 && hours<=2
fees=4;
elseif hours>2 && hours<=4
fees=7;
elseif hours>4 && hours<22
fees = 7+(hours-4);
elseif hours>=22 && hours<=24
fees = 24;
elseif hours>24
numofdays = floor(hours/24);
fees = numofdays*24;
remainhours = hours-numofdays*24;
fees = fees+parking_fee(remainhours);
end
if neg==1;
fees=fees+36;
end
end
output:
>> parking_fee(-123)
ans =
163