MATLAB Answers

Trying to calculate a volume by changing equation for volume for different variable parameter

7 views (last 30 days)
Alison Michell
Alison Michell on 25 Jul 2018
Hi, I am trying to calculate the volume of a tank given different values for radius and height of tank and liquid. My code keeps saying I have an undefined variable V. If someone could please help me figure out how to correct this I would greatly appreciate it. Thank you!
function Volume
h = 0:10
H = 10
r = 1
Vol = zeros([],1);
rad = zeros([],1);
Height = zeros([],1);
height = zeros([],1);
for i = 0:length(h)
if h <= r
V = pi.*h.^2.*(3.*r-h)./3
elseif (r <= h) & (h <= H-r)
V = 2.*pi.*r.^3./3+pi.*r.^2.*(h-r)
elseif ((H-r) <= h) & (h<=H)
V = 4.*pi.*r.^3./3+pi.*r.^2.*(H-2.*r)-pi.*(H-h).^2.*(3.*r-H+h)./3
Vol(i) = V ;
rad(i) = r ;
Height(i) = H;
height(i) = h;
disp('height volume')

Answers (1)

Christopher Wallace
Christopher Wallace on 25 Jul 2018
Hi Alison,
You're using the array 'h' in your if else statement which as written doesn't satisfy any of the conditions causing 'V' to never be defined.
If you're meaning to iterate over 'h' use:
Best Regards,

Community Treasure Hunt

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

Start Hunting!