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

조회 수: 1 (최근 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
Christopher Wallace 2018년 7월 25일
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에서 3-D Volumetric Image Processing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by