Function With While Loop Help

Hello--
So I need to calculate time, flow rate, height, and velocity of water exiting a cylindrical container until the container is empty. I must use a while loop within a function to do so. I am supposed to break the loop once h (height) is slightly larger than 0 so h>0. I need to define a counting variable and start at t = 0.
This is my code for the function so far:
function [Q,h,t,V] = hw4_func_BDP(Initial_Height,Outer_D,Inner_D)
p = 1000; %(kg/m^3) pressure of water
g = 9.81; %(m/s^2) acceleration due to gravity
k = ((d^2)/(D^2))*sqrt(2*g); %constant with diameters of large hole vs exit hole
%Outputs (all vectors)
t = 0 %Time
h = sqrt(h0-.5*k.*t).^2 %Fluid Height
V = sqrt(2*g.*h) %Velocity
Q = pi*(Inner_D/2)^2 .*V %Flow Rate
j = 1
while h > 0
%I am uncertain what to do here
j = j+1
end
The inputs in my script are:
h0 = 2; %initial height
D = 1; %outer diameter
d = .1; %inner diameter
Also, I am supposed to calculate these as vectors, should I define them in my script as blank vectors before hand? Like t = [] h = [] V = [] Q = []
Please help!!

 Accepted Answer

Walter Roberson
Walter Roberson on 17 Feb 2019
At each step, you can determine h (height) by knowing the current volume remaining and knowing the cross-section area of the vessel. At each step, the volume reduces by the flow rate multiplied by the time step.

4 Comments

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];
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
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
So I was able to perform the while loop, but I am struggling on making a vector dh to watch the change in height after each time step. I do not want it to be directly related to the height, but more of a sanity check to make sure the loop is changing the height correctly. so not
dh = h(dt) - h(dt-1)
I know flow rate is given as:
Q = AV
My instructions say:
Use the flow rate to calculate how much water drained form the tank in the currenttime step and then use the area of the tank (not the exit hole) to calculate how much the height of the water change. You can call this vector dh.....
Having an open drain does not cause the the pressure at top and bottom to be equal. If there were no pressure at the drain then the water would not flow as such -- though it could simply fall due to being unsupported.
Pressure at the drain needs to be calculated according to the weight of water above, which in turn depends upon the depth, which depends upon the original volume of water and the cross-section area.
So what is velocity equal to in this case?
Oh and the question says we can assume pressure on the top and the bottom is 0Pa

Sign in to comment.

More Answers (1)

Brian Peoples
Brian Peoples on 18 Feb 2019
Screen Shot 2019-02-17 at 9.48.24 PM.png

3 Comments

So I am asking about e. on the third page. I can't figure this out.....

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!