Write a function that will take in the dimensions of the box, length, width, and height, and the radius of the hole, and determine the volume of material remaining. Assume the hole has been drilled along the height direction and that the hole has radius less than min(length/2, width/2).

Respuesta :

Answer:

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

Step-by-step explanation:

L = input('Enter the Length of Box [meters]:');   %First we take length input and save in variable L

W = input('Enter the Width of Box [meters]:');   %We take length input and save in variable W

H = input('Enter the Height of Box [meters]:');   %We take length input and save in variable H

R = input('Enter the Radius of Hole [meters]:');   %We take length input and save in variable R

if R >= min(L/2,W/2) %Condition for the radius greater than half of shorter dimension    

   disp('Error! Radius must be less than the half of the shorter dimension among length and width');

else

   Vb = L*H*W;   % Calculating Total Volume of Box and save in Vb

   Vh = pi*(R^2)*H;  %Calculating Volume of Hole and save in Vh

   Vr = Vb - Vh;   %Calculating remaining volume and save in Vr

   fprintf('The remaining volume of box is ');  

   fprintf('%.f2',Vr);  %Display Answer

   fprintf(' cubic meter');

end

SAMPLE RUN:

Enter the Length of Box [meters]:3

Enter the Width of Box [meters]:3

Enter the Height of Box [meters]:3

Enter the Radius of Hole [meters]:1

The remaining volume of box is 182 cubic meter>>

Q&A Education