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

7 views (last 30 days)
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);
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
end
Vol(i) = V ;
Height(i) = H;
height(i) = h;
end
disp('height volume')
disp([height;Vol])

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:
h(i)
Best Regards,
Chris