Trying to Display a found answer with units

2 views (last 30 days)
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 Comment
Miriah Dudley
Miriah Dudley on 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?

Sign in to comment.

Accepted Answer

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

Sign in to comment.

More Answers (0)

Categories

Find more on Install Products in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!