4TT 3 1. As you know the volume of a hollow sphere is determined by,-Co -r) 3 wh
ID: 3585414 • Letter: 4
Question
4TT 3 1. As you know the volume of a hollow sphere is determined by,-Co -r) 3 where ri is the inner radius and ro is the outer radius. When creating computer code it is important to calculate correct results and be user friendly! In this calculation the code will be required to accept both inner radius and outer radius in any order. The code will need to assign the larger dimension to the outer radius and the smaller dimension to the inner radius Invalid numerical input should inform the user of the error and request the user to input a new appropriate value Problem Requirements Write a function named hollow sphere that has two input arguments and one output argument (volume) 1. The function must assign smaller input radius to the inner dimension and the larger radius to the outer dimension 2. If the two inputs values are the same and non-negative, return zero and print "The input values, inner and outer radii cannot be equal! Please call this function again with two positive values. " 3. If any of the input values is equal to zero, and the other value is greater then zero, return the volume as usual but print "The inner radius is 0, and the sphere is solid." 4. If the two input values are zero, return zero. If any of the input values is less than zero, return not-a-number. In both of these cases, print "The inner and outer radii of the hollow sphere must be greater than zero, please call this function again with positive dimensions."Explanation / Answer
function hollvol = volume_hollow_sphere(inner, outer)
%check if both radii are equal to zero
if( inner ==0 && outer ==0 )
hollvol = 0;
disp('The inner and outer radii of the hollow sphere must be greater than zero, please call this function again with positive dimensions');
return;
end
%check if any radius is less than zero
if(inner <0 || outer <0)
hollvol = NaN;
disp('The inner and outer radii of the hollow sphere must be greater than zero, please call this function again with positive dimensions');
return;
end
%check if both radii are equal to each other
if(inner >= 0 && outer >= 0 && inner == outer)
hollvol =0;
disp('The input values, inner and outer radii cannot be equal! Please call this function again with two positive values');
return;
end
%if inner radius is greater than outer swap them
if( inner > outer )
temp = inner;
inner = outer;
outer = temp;
end
%if inner is zero then sphere is solid
if(inner==0)
hollvol = 4/3 * pi * (outer^3);
disp('The inner radius is 0 and sphere is solid');
return;
end
% Calculates the volume of a hollow sphere
hollvol = 4/3 * pi * (outer^3 - inner^3);
end
Script to test and run:
%finding and printing each test case value
vol1 = volume_hollow_sphere(15,10);
vol1
vol2 = volume_hollow_sphere(10,20);
vol2
vol3 = volume_hollow_sphere(10,10);
vol3
vol4 = volume_hollow_sphere(0,0);
vol4
vol5 = volume_hollow_sphere(10,0);
vol5
vol6 = volume_hollow_sphere(10,-10);
vol6
vol7 = volume_hollow_sphere(-10,-10);
vol7
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.