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

3 次查看(过去 30 天)
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
end
Vol(i) = V ;
rad(i) = r ;
Height(i) = H;
height(i) = h;
end
disp('height volume')
disp([height;Vol])

回答(1 个)

Christopher Wallace
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

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by