Trying to Display a found answer with units

5 visualizaciones (últimos 30 días)
Miriah Dudley
Miriah Dudley el 20 de Abr. de 2021
Comentada: David Fletcher el 20 de Abr. de 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 comentario
Miriah Dudley
Miriah Dudley el 20 de Abr. de 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?

Iniciar sesión para comentar.

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by