Answer:
MATLAB Code is written below with comments in bold, starting with % sign.
MATLAB Code:
function L = Lkm(mpg)
L = mpg*1.60934/3.78541; %Conversion from miles per gallon to km per liter
L = L^(-1); %Conversion to liter per km
L = L*100; %Conversion to liter per 100 km
end
Explanation:
A function named Lkm is defined with an output variable "L" and input argument "mpg". So, in argument section, we give function the value in miles per gallon, which is stored in mpg. Then it converts it into km per liter by following formula:
L = (mpg)(1.60934 km/1 mi)(1 gallon/3.78541 liter)
Then this value is inverted to convert it into liter per km, in the next line. Then to find out liter per 100 km, the value is multiplied by 100 and stored in variable "L"
Test Run:
>> Lkm(100)
ans =
2.3522