. In the U.S. fuel efficiency of cars is specified in miles per gallon (mpg). In Europe it is often expressed in liters per 100 km. Write a MATLAB userdefined function that converts fuel efficiency from mpg to liters per 100 km. For the function name and arguments, use Lkm

Respuesta :

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