Please answer it either using excel or matlab. Provide the screenshot also.. Wri
ID: 3688545 • Letter: P
Question
Please answer it either using excel or matlab. Provide the screenshot also..
Write a code in the programming language of your choice (e.g., Python, Matlab, FORTRAN. C++, Excel, etc.) that computes the Darcy-Weisbach friction factor f given the following input values: velocity V, pipe diameter D, roughness height k_s, kinematic viscosity nu, and acceleration of gravity g. Make the code valid for laminar and turbulent flow, and for smooth, transitional, and rough pipes in both English and SI units by using f = 64/Re and the Colebrook Equation. Print out the code, or Excel worksheet, and an example of how it works for given input values. In essence, what does you code reproduce?Explanation / Answer
function f = moody(ed,Re,verbose)
%
% Synopsis: f = moody(ed,Re)
%
% Input: ed = relative roughness = epsilon/diameter
% Re = Reynolds number
%
% Output: f = friction factor
%
% Note: Laminar and turbulent flow are correctly accounted for
if Re<0
error(sprintf(’Reynolds number = %f cannot be negative’,Re));
elseif Re<2000
f = 64/Re; return % laminar flow
end
if ed>0.05
warning(sprintf(’epsilon/diameter ratio = %f is not on Moody chart’,ed));
end
if Re<4000, warning(’Re = %f in transition range’,Re); end
% --- Use fzero to find f from Colebrook equation.
% coleFun is an inline function object to evaluate F(f,e/d,Re)
% fzero returns the value of f such that F(f,e/d/Re) = 0 (approximately)
% fi = initial guess from Haaland equation, see White, equation 6.64a
% Iterations of fzero are terminated when f is known to whithin +/- dfTol coleFun = inline(’1.0/sqrt(f) + 2.0*log10( ed/3.7 + 2.51/( Re*sqrt(f)) )’,... ’f’,’ed’,’Re’);
fi = 1/(1.8*log10(6.9/Re + (ed/3.7)^1.11))^2; % initial guess at f dfTol = 5e-6;
f = fzero(coleFun,fi,optimset(’TolX’,dfTol,’Display’,’off’),ed,Re);
% --- sanity check:
if f<0, error(sprintf(’Friction factor = %f, but cannot be negative’,f)); end
function [out1,out2,out3] = pipeLoss(Q,L,A,Dh,e,nu,KL,Am)
% pipeLoss Viscous and minor head loss for a single pipe
%
% Synopsis: hL = pipeLoss(Q,L,A,Dh,e,nu)
% hL = pipeLoss(Q,L,A,Dh,e,nu,KL)
% hL = pipeLoss(Q,L,A,Dh,e,nu,KL,Am)
% [hL,f] = pipeLoss(...)
% [hv,f,hm] = pipeLoss(...)
%
% Input: Q = flow rate through the system (m^3/s)
% L = vector of pipe lengths
% A = vector of cross-sectional areas of ducts. A(1) is area of
% pipe with length L(1) and hydraulic diameter Dh(1)
% Dh = vector of pipe diameters
% e = vector of pipe roughnesses
% nu = kinematic viscosity of the fluid
% KL = minor loss coefficients. Default: KL = [], no minor losses
% Am = areas associated with minor loss coefficients.
% For a flow rate, Q, through minor loss element 1, the area,
% Am(1) gives the appropriate velocity from V = Q/Am(1). For
% example, the characteristic velocity of a sudden expansion
% is the upstream velocity, so Am for that element is the
% area of the upstream duct.
%
% Output: hL = (scalar) total head loss
% hv = (optional,vector) head losses in straight sections of pipe
% hm = (optional, vector) minor losses
% f = (optional, vector) friction factors for straight sections
if nargin<7, KL = []; Am = []; end
if nargin<8, Am = A(1)*ones(size(KL)); end
if size(Am) ~= size(KL)
error(’size(Am) = [%d,%d] not equal size(KL) = [%d,%d]’,size(Am),size(KL));
end
% --- Viscous losses in straight sections g = 9.81;
% acceleration of gravity, SI units if isempty(L) hv = 0; f = [];
% no straight pipe sections else V = Q./A;
% velocity in each straight section of pipe f = zeros(size(L));
% initialize friction factor vector for k=1:length(f) f(k) = moody(e(k)/Dh(k),V(k)*Dh(k)/nu);
% friction factors end hv = f.*(L./Dh).*(V.^2)/(2*g);
% viscous losses in straight sections end
% --- minor losses if isempty(KL) hm = 0;
% no minor lossess else hm = KL.*((Q./Am).^2)/(2*g);
% minor lossess end
% --- optional return variables if nargout==1 out1 = sum(hv) + sum(hm);
% return hL = total head loss elseif nargout==2 out1 = hv; out2 = f;
% return viscous losses and friction factors elseif nargout==3 out1 = hv; out2 = f;
% return viscous losses, friction factors out3 = hm;
% and minor losses else error(’Only 1, 2 or 3 return arguments are allowed’); end