In: Computer Science
Note: Present results using fprintf()and comment your code throughout.
The script should call the function 3 times and store the results of the three test cases in two row vectors called vol and sa.
Function specifications:
Input arguments: length, width, height - all scalars. (Order does not matter)
Output arguments: volume, area - both scalars. (Must be in this order)
OPTIONAL: If you prefer, you can write the function such that the input and output arguments are arrays; in that case, only one call to the function is necessary.
% Matlab function
function [volume, surface_area] =
prism_volume_surfacearea(length, width, height)
% Matlab function prism_volume_surfacearea to calculate the volume
and
% surface area of a rectangular prism for the given input length,
width and height
% The input arguments can be scalars
% In case the input arguments is scalar, output values of volume
and
% surface_area is also scalar
% calculate the volume of the rectangular prism
volume = length*width*height;
% calculate the surface area of the rectangular prism
surface_area = 2*(length * width + length * height+ width *
height);
end
%end of function
% Matlab main script to calculate the volume and surface area of
the
% rectangular prism for 3 different values of height, width and
depth
% create row vectors of size for volume and surface_area
volume = zeros(1,3);
surface_area = zeros(1,3);
% define the height, width and depth variables
height = 5;
width = 3;
depth = 2;
% calculate and insert the volume and surface area into the
corresponding vectors
[volume(1),surface_area(1)] = prism_volume_surfacearea(depth,
width,height);
height = 1;
width = 8;
depth = 4;
[volume(2),surface_area(2)] = prism_volume_surfacearea(depth,
width,height);
height = 2;
width = 2;
depth = 10;
[volume(3),surface_area(3)] = prism_volume_surfacearea(depth,
width,height);
% loop to display the volume and surface area from the
vectors
fprintf('%20s%20s\n','Volume','Surface Area');
for i=1:length(volume)
fprintf('%20.2f%20.2f\n',volume(i),surface_area(i));
end
%end of script
Output: