Saturday, July 14, 2012

Solving Colebrook equation in Scilab

Colebrook equation is an implicit equation used to calculate the Darcy-Weisbach friction factor which is used to calculate the pressure drop in pipes.

So, one of the methods to solve this equation is Newton-Raphson method. Colebrook equation can be re-written in residual form like the following

The following Scilab function is used to calculate Darcy-Weisbach friction factor for a fully-developed turbulent flow.

function [f, #of_iterations]=Colebrook_f(epslon, d, Re, NMax) 

R='1/sqrt(f)+2*log10((epslon/d)/3.7+2.51/(Re*sqrt(f)))'; // Residual expression 

D='-0.5*f**(-1.5)*(1+(5.02/(log(10)*Re))/((epslon/d)/3.7+2.51/(Re*sqrt(f))))'; // derivative expression: partial ... derivative of residual with respect to friction factor
f=0.02; // Initial guess of Darcy-Weisbach friction factor 

for i=1:NMax, // loop stops after reaching stop criteria 

RV=eval(R); // Residual value evaluation 
DV=eval(D); // Derivative value evaluation 
f_new=f-RV/DV; // computing the new value of f using Newton-Raphson method

if RV <=0.0001 then // If residual is less than the accepted error 
else f=f_new; 




The following capture shows the result obtained by the function used in Scilab

and the following capture is the result obtained by the online calculator in this link