Write a MATLAB script cup.m that will calculate the volume and surface area of a
ID: 3711152 • Letter: W
Question
Write a MATLAB script cup.m that will calculate the volume and surface area of a paper cup shaped
as a frustum of cone.
a) Request (by input) a vector that has two radii [R1 R2] of the cup from keyboard and name it r.
b) Request the height of the cup from keyboard and name it h.
c) Request for the calculation option either volume by typing “1” or surface area by
typing “2”. Any other number will end the program. Name the option as op.
Calculate volume of the cup and the surface area of the paper by:
v=(1/3)pi*h*(R1^2+R2^2+R1*R2) s=pi*(R1+R2)*sqrt((R2-R1)^2+h^2) + pi*R1^2
and print the formatted output (use fprintf) similar to the following example:
Bottom radius = x.x cm, top radius = x.x cm and height = xx.x cm
volume is xxx.xx cm^3 or surface area is xxx.xx cm^2
d) Do 3 test runs with r = [3.0 3.8 ] cm and h = 9.5 cm, and options op = 1, 2 and 5
Explanation / Answer
prompt = 'Enter the radius vector : ';
% get user input
r = input(prompt);
R1 = r(1);
R2 = r(2);
prompt = 'Enter height of the cup from keyboard : ';
% get user input
h = input(prompt);
fprintf(' Select one of the following options ... 1. Volume 2. Surface Area ');
ch = input('');
fprintf('Bottom radius = %0.1f cm, top radius = %0.1f cm and height = %0.1f ', R1, R2, h);
if ch == 1
v = ( 1 / 3 ) * pi * h * ( ( R1^2 ) + ( R2^2 ) + R1 * R2 );
fprintf('Volume = %0.2f cm^3 ', v);
elseif ch == 2
s = pi * ( R1 + R2 ) * sqrt( ( R2 - R1 )^2 + h^2 ) + pi * ( R1^2 );
fprintf('Surface Area = %0.2f cm^2 ', s);
else
fprintf(' Error! Invalid option. ');
end
Sample Output
Enter the radius vector : [ 3.0 3.8 ]
Enter height of the cup from keyboard : 9.5
Select one of the following options ...
1. Volume
2. Surface Area
2
Bottom radius = 3.0 cm, top radius = 3.8 cm and height = 9.5
Surface Area = 231.94 cm^2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.