Filter löschen
Filter löschen

Trying to Display a found answer with units

1 Ansicht (letzte 30 Tage)
Miriah Dudley
Miriah Dudley am 20 Apr. 2021
Kommentiert: David Fletcher am 20 Apr. 2021
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 Kommentar
Miriah Dudley
Miriah Dudley am 20 Apr. 2021
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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

David Fletcher
David Fletcher am 20 Apr. 2021
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 Kommentar
David Fletcher
David Fletcher am 20 Apr. 2021
If desired, you can add a modifer to the %f to limit the precision (such as %6.2f)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by