フィルターのクリア

Trying to Display a found answer with units

1 回表示 (過去 30 日間)
Miriah Dudley
Miriah Dudley 2021 年 4 月 20 日
コメント済み: David Fletcher 2021 年 4 月 20 日
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
  1 件のコメント
David Fletcher
David Fletcher 2021 年 4 月 20 日
If desired, you can add a modifer to the %f to limit the precision (such as %6.2f)

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by