The specific gravity of gold is 19.3. Write a MATLAB function that will determine the length of one side of a cube of solid gold, in units of inches, provided the mass of the cube in kilograms. The output should display a sentence like the one shown below, with the length formatted to 2 decimal places. If the user types a negative number or zero for the mass of the cube, your program should display an error message and terminate.
Sample Input / Output (multiple scenarios):
Enter the mass of the cube [kilograms]: -3
Error: Mass must be greater than zero grams.
Enter the mass of the cube [kilograms]: 0.4
The length of one side of the cube is 1.08 inches.

Respuesta :

Answer:

The code is written line wise with number. The code is explained by comments, which are written after % sign in bold. Note: Comments are not the part of code.

Explanation:

1. mass = input('Enter the mass of cube [kilograms]:');   %First we take mass input and save in variable m

2. if mass <= 0    %Condition for value less than or equal to 0

3.    disp('Mass must be greater than zero grams');

4. end

5. if mass > 0

6.    density = 19.3*1000;   % Calculating Density of gold

7.    Volume = mass/density;  %Calculating Volume

8.    Length = Volume^(1/3);   %Calculating Length of edge

9.    Length = Length*(39.37);   %Conversion to inches

10.    fprintf('The length of one side of cube is ');

11.    fprintf('%.2f',Length);

12.    fprintf(' inches');

13. end

ACCESS MORE