Help using sub functions
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Nikolas Vieira
am 31 Jan. 2020
Kommentiert: KSSV
am 31 Jan. 2020
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 Kommentare
Akzeptierte Antwort
KSSV
am 31 Jan. 2020
Bearbeitet: KSSV
am 31 Jan. 2020
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Surface and Mesh Plots finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!