This code is in matlab Write a matlab program called lab09 that gets width of a
ID: 3804186 • Letter: T
Question
This code is in matlab
Write a matlab program called lab09 that gets width of a cube, and radius of a sphere in meters from user, error checks for radius to be less than or equal to half of the width and converts both of them to ft^3. (Use this with a while loop) then calculates the volume of a cube and number of sphere can be placed within this cube. The volume of the cube is:
volume = width * width * width
Volume of sphere = 4/3 * pi * r^3
Conversion factor 1m = 3.28ft
The program must display the volume of the cube in one sentence. And then the number of spheres in another sentence. Volume to be displayed with three decimals, and number of spheres as an integer.
Use: sprintf or fprint for formatted output to the screen.
floor statement to round a number to next smallest integer.
A while loop for error checking.
Sample output
Enter the width of the cube in meters: 4
Enter the radius of sphere in meters: 3
Radius must be less than half the cube width
Enter the radius of sphere in meters: 1.2
Volume of the cube is 2258.403 ft3
There will be 8 sphere(s) in the cube
Explanation / Answer
clear;
clc;
close;
width = input('enter width of cube in meters:'); %reading width of cube and radius of sphere
radius = input('enter radius of sphere in meters:');
while(radius > (width/2) %comparing radius with half of width
fprintf("radius of sphere is invalid value");
radius = input('enter radius of sphere in meters:');
end
width = width *(3.28); %converting width and radius into feets
radius = radius *(3.28);
cubevolume = width*width*width; %finding vplume of cube
spherevolume = (4./3).*pi.*(radius.^3); %finding volume of sphere
count = fix(cubevolume/spherevolume); %finding no.of spheres can be placed into cube
fprintf('the no.of spheres can be placed inside the cube are: %d',count);