Trying to Display a found answer with units

2 次查看(过去 30 天)
Here I have a script I am trying to write a script but I need my answers (within the if-statement) to output"Water Volume = amount found m^3(unit needed to display"
so i.e Water Volume = 4.0235 m^3
This is what I have so far and I have another script to "call" this function where it lists:
tankvolume( -5)
tankvolume(15)
tankvolume(50)
tankvolume(60)
function Vtotal = tankvolume(h)
rc = 10;
rf = 20;
hc = 30;
hf = 25;
if h < 0
Vtotal = -1;
disp('Error h Cannot be Negative')
elseif h < hc && h > 0
Vtotal = pi*rc*rc*h;
fprintf ('Water Total = % m^3, Vtotal') % Volume of Water in Tank in m^3
elseif hc < h && h < hc + hf
rh = rc+((h-hc)*(rf-rc))/hf;
Vtotal = pi*rc*rc*hc+(pi*(h-hc)*(rc*rc+rc*rh+rh*rh))/3; % Volume of Water in Tank in m^3
fprintf ('Water Total = % m^3, Vtotal')
else
Vtotal = -1;
disp('Error Overflow')
end
end
any advice?
  1 个评论
Miriah Dudley
Miriah Dudley 2021-4-20
UPDATE: ('Water Total = %6.2f m^3\n', Vtotal)
Found a way to print Water total =
HOWEVER, I still get ans = as an output and I don't want that. Anyway to avoid it from being an output?

请先登录,再进行评论。

采纳的回答

David Fletcher
David Fletcher 2021-4-20
function Vtotal = tankvolume(h)
rc = 10;
rf = 20;
hc = 30;
hf = 25;
if h < 0
Vtotal = -1;
disp('Error h Cannot be Negative')
elseif h < hc && h > 0
Vtotal = pi*rc*rc*h;
fprintf ('Water Total = %f m^3\n', Vtotal) % Volume of Water in Tank in m^3
elseif hc < h && h < hc + hf
rh = rc+((h-hc)*(rf-rc))/hf;
Vtotal = pi*rc*rc*hc+(pi*(h-hc)*(rc*rc+rc*rh+rh*rh))/3; % Volume of Water in Tank in m^3
fprintf ('Water Total = %f m^3\n', Vtotal)
else
Vtotal = -1;
disp('Error Overflow')
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Install Products 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by