Help using sub functions

조회 수: 12 (최근 30일)
Nikolas Vieira
Nikolas Vieira 2020년 1월 31일
댓글: KSSV 2020년 1월 31일
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

채택된 답변

KSSV
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) ;
  댓글 수: 2
Nikolas Vieira
Nikolas Vieira 2020년 1월 31일
Im not sure I know what you mean. Do I save all 3 subfunctions to one file? I see what the bottom 3 lines of code are doing. I am not sure what the Options=NV_hw2_4 is doing. Ideally that needs to be my main function.
KSSV
KSSV 2020년 1월 31일
Edited the answer.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Volume Visualization에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by