Re = [1 1e2 1e3 2e3 4e3 1e4 1e5 1e6 1e7 1e8];
f = zeros(size(Re));
for i=1:length(Re)
f(i) = moody(Re(i),0);
end
loglog(Re,f,’.-‘,’MarkerSize’,15)
function f = moody(Re,eD)
if Re<=2000 f = 64/Re; elseif Re>2000 && Re<4000 f = NaN; else g = @(x)(1./sqrt(x)+2.*log10(eD./3.7+2.51./(Re.*sqrt(x)))); fguess = (-1.8.*log10(6.9./Re+(eD./3.7).^1.11))^(-2); f = fzero(g,fguess); %Replace fzero here with any root finding method, although Newton-Raphson is recommended. end end