Q2) Write a Matlab function that calculates the surface area and volume of a cub
ID: 1717079 • Letter: Q
Question
Q2)
Write a Matlab function that calculates the surface area and volume of a cube. The equations to calculate the area and volume are: area= 6 x2, volume = x3 , where x is the length of the cube. Name the function areavolcube.
Write a Matlab function that calculates the surface area and volume of a cylinder. The equation to calculate the surface area is: ( 2 r h + 2 r2 ), where h is the length of the cylinder and r is its radius. The volume = r2 h. Name the function areavolcylinder.
Write a Matlab function that calculates the surface area and volume of a sphere. The equations to calculate the area and volume are: area= 4r2, volume = (4/3) r3 , where r is the radius of the sphere. Name the function areavolsphere.
Explanation / Answer
1.Define a function in a file named areavolcube.m that returns the area and volume of a cube.
function [a,v] = areavolcube (x)
a = 6*(x^2);
v = x^3;
end
Call the function from the command line.
side = 6; %example I have taken length of side as 6 cms
[are,vol] = areavolcube (side)
2. areavolcylinder.m
function [a,v] = areavolcylinder (x,y)
a = (2*pi*x*y) + (2*pi*(x^2));
v = (pi*(x^2)*y);
end
Call the function from the command line.
radius = 6; %example I have taken radius as 6 cms
height = 6; %example I have taken height as 6 cms
[are,vol] = areavolcylinder (radius,height)
3. areavolsphere.m
function [a,v] = areavolsphere (x)
a = 4*pi*(x^2);
v = (4/3)*pi*(x^3);
end
Call the function from the command line.
radius = 6; %example I have taken length of side as 6 cms
[are,vol] = areavolsphere (radius)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.