Help using sub functions
12 次查看(过去 30 天)
显示 更早的评论
I need to use the values calculated in the 3 subfunctions.
The 3 sub functions each calculate a surface area. The goal for the main function is to use the 3 surace areas and multiply them by a price and output the 3 prices. The 3 sub functions use the same volume from the main function. I am not sure how to complete this task. Help is appreciated!
I am required to use local/subfunctions or else I would have calculated all of the SAs to be indidividual variables.
function Options= NV_hw2_4(volume,cost) %goal here is to use the surface areas from the subfunctions and multiply them by the price which will be an input in the main function
SAS= [cubeSA,cylinderSA,sphereSA]
Options= price.*SAS
function cubeSA= getSA1(volume) %this function finds the surface area of a cube based on volume
g=nthroot(volume,3) %solving for the side length and assigning it to g
cubeSA=6*g^2 % calculating surface area based off the side length (g)
end
function cylinderSA= getSA2(volume) %this finds SA of the cylinder based on volume
c= nthroot((volume/pi),3) %finding the radius required
cylinderSA= (2*pi*c^2)*2 %calculating surface area with the required radius to match the desired volume
end
function sphereSA= getSA3(volume) %this funds the SA of the sphere
d= nthroot((volume/((4/3)*pi)),3) %solving for the required radius
sphereSA= (4*pi*d^2)%calculating the surface area with the required radius for the desired volume
end
end
0 个评论
采纳的回答
KSSV
2020-1-31
编辑:KSSV
2020-1-31
function Options=NV_hw2_4(volume,price) %goal here is to use the surface areas from the subfunctions and multiply them by the price which will be an input in the main function
cubeSA= getSA1(volume) ;
cylinderSA= getSA2(volume) ;
sphereSA= getSA3(volume) ;
SAS= [cubeSA,cylinderSA,sphereSA] ;
Options= price.*SAS
end
function cubeSA= getSA1(volume) %this function finds the surface area of a cube based on volume
g=nthroot(volume,3) %solving for the side length and assigning it to g
cubeSA=6*g^2 % calculating surface area based off the side length (g)
end
function cylinderSA= getSA2(volume) %this finds SA of the cylinder based on volume
c= nthroot((volume/pi),3) %finding the radius required
cylinderSA= (2*pi*c^2)*2 %calculating surface area with the required radius to match the desired volume
end
function sphereSA= getSA3(volume) %this funds the SA of the sphere
d= nthroot((volume/((4/3)*pi)),3) %solving for the required radius
sphereSA= (4*pi*d^2)%calculating the surface area with the required radius for the desired volume
end
Run like this:
volume = 10 ;
price = 5 ;
options = NV_hw2_4(volume,price) ;
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Volume Visualization 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!