# While Loop Running forever (Draining Cylinder Question)

8 views (last 30 days)
Brian Peoples on 18 Feb 2019
Hey, I'm stuck figuring out how to run the loop until h is just barely above 0. so I want to run the loop while h>0, but it is a squared equation so it goes down to near 0, then goes back up like a parabola.
Also I need to solve for dh using the area of the tank (not the exit hole) to calculate how much the height of the water changed. dh would be a vector.
function [Q,h,t,V] = hw4_func_BDP(Initial_Height,Outer_D,Inner_D)
%I got V by starting off with the Bernoulli's equation, crossing off
%pressure for the top and the bottom of the cylindrical container because the open drain causes it to equal
%zero, crossed out out the velocity on the top due to it being ostensibly
%zero (Because D>>d Vtop << Vbottom), crossed out density because it is the
%same all throughout the equation, then we are left with gh1 = 1/2 V2^2,
%therefore V2 is equal to sqrt(2gh).
%Constants
p = 1000; %(kg/m^3) pressure of water
g = 9.81; %(m/s^2) acceleration due to gravity
k = ((Inner_D^2)/(Outer_D^2))*sqrt(2*g); %constant with diameters of large hole vs exit hole
%Outputs for time, height, velocity,
%and flow rate in while loop
%Output Variables
t = [0];
V = [0];
Q = [0];
dh = [0];
if Initial_Height == 5
h = 5;
else
h = 2;
end
dt = 1; %time step
while h > 0
t(1+dt) = t(dt) + 1 %time goes up by 1 each time
h(1+dt) = (sqrt(Initial_Height)-.5*k.*t(1+dt)).^2 %Fluid Height
V(1+dt) = sqrt(2*g.*h(1+dt)); %Velocity
Q(1+dt) = pi*(Inner_D/2)^2 .*V(1+dt); %Flow Rate
%dh for sanity check
%dh(1+dt) = -h(1+dt)*sqrt(2*g.*h(1+dt))
dt = dt+1;
end
%Last peramitors errasing when h is less than 0 and making the last time
%step equal to zero
h(dt) = 0
V(dt) = 0
Q(dt) = 0
end
Patel Mounika on 21 Feb 2019
The value of h is never going else than 0 therefore the while loop is running for forever,try changing the threshold which meets the requirement.

Yasasvi Harish Kumar on 21 Feb 2019
Hi,
There are a few problems in your code.
Firstly, using a . before an operation indicates element wise operation. Secondly, h is an array and you are using it in some places like its not.
Lastly, .5*k*t(1+dt) in h(1+dt) = (sqrt(Initial_Height)-.5*k.*t(1+dt)).^2 is greater than sqrt(Initial_Height), since you are raising the whole thing to the power 2, your h value never reduces.
Regards