MATLAB: Iterative method to find flowrate in pipe in series

darcy equationhydraulicsiterative method

Hello,
I've been trying to finish this code for a few days now. It's quite frustrating since I know exactly what to do but I can't write it in Matlab. I have to find lambda and the Reynolds numbers for the 2 pipes so that I can then calculate the flowrate Q. I need to use an iterative method (using the previous values Matlab calculated to use them in the next loop) but I can't seem to make mine work. My idea is to use a sequence and matlab to loop it until the difference between the previous value of lambdas and the value calculated in the loop is smaller than the tolerance number. I would appreciate it if someone could put me in the right path on how to properly use the iterative method in this case.
The code doesn't work yet because I can't get the lambda values to be close enough yet.
Thank you
clear;
clc;
% The physical properties
rho = 1000;
mu = 1.31e-3;
g = 9.81;
% The system properties
d1 = 0.1;%input ('Diameter of pipe 1=');
l1 = 2000;%input ('length of pipe 1=');
d2 = 0.05;%input ('Diameter of pipe 2=');
l2 = 100;%input ('length of pipe 2=');
Hc=5; %headloss (45-40m)
A1= (pi*(d1/2)^2/4);
A2= (pi*(d2/2)^2/4);
tol=10e-6;%tolerance
i=1;
% Initial guess of the Reynolds Number
Re1(1)= 2.8e5;
for i=1:100 %loop to repeat 100x
lam1(1)= 64/Re1(1); %lambda in pipe 1
V1= sqrt(2*g*d1*Hc/(lam1(1)*l1));% Calculating the velocity in pipe 1 from the Darcy Eq
V2= V1*(A1/A2); %using continuity eq
Re2(1) = rho*(V1*(A1/A2))*d2/mu; %Re in the 2nd pipe
lam2(1)= 64/ Re2(1); %lam2 in 2nd pipe
if lam1(1)-lam2(1)>tol %i'd like to use a while loop that would stop when the 2 lambda values are close enough (tolerance)
Re1(i+1)= rho*V1*d1/mu;
Re2(i+1)= rho*V2*d2/mu;
lam1= 64/Re1(i+1);
lam2= 64/Re2(i+1);
else
break
end
end
end
disp ('end')
Q1 = V1*pi*d1^2/4;
Q2 = V2*pi*d2^2/4;

Best Answer

Why would you assume the friction factor in the second pipe is the same as that in the first?
You ony apply the head loss to the first pipe. In this case you know everything needed to calculate the volumetric flowrate without iteration:
% g*Hc = lam*(L/D)*(1/2)*V^2 (1) Darcy L = length D = diameter
% Re = rho*V*D/mu; (2) Reynolds number
% lam = 64/Re; (3) laminar friction factor
% From (2) and (3): lam = 64*mu/(rho*V*D); (4)
% Use (4) in (1): g*Hc = 64*mu/(rho*V*D)*(L/D)*(1/2)*V^2; (5)
% Rearrange (5): V = g*Hc*rho*D^2/(32*mu*L) (6)
% Hence volumetric fowrate: Q = V*(pi*D^2/4);
Related Question