1.5k views
4 votes
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).

User Okema
by
7.5k points

1 Answer

4 votes

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.

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>>

User AshishB
by
6.5k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.