2

I am trying to code an approximation of the Colebrook equation to obtain the friction factor. I have achieved this in Matlab but when it come to python my code does not work.

import math
Re = 2300
eD=0.0009 
1/math.sqrt(friction)=-2*math.log10((((eD/3.7)+(2.51/Re))*(1.14-2*math.log10(((eD/2)+(21.25/Re**0.9))))))
print(friction)

I also tried this

import math
def friction(Re, eD):
    eD= 0.0009
    Re= 2300
    f = -2*math.log10((((eD/3.7)+(2.51/Re))*(1.14-2*math.log10(((eD/2)+(21.25/Re**0.9))))))
    return 1/math.sqrt(f)

but this also does not work.

Cœur
  • 37,241
  • 25
  • 195
  • 267
yamifm0f
  • 65
  • 4

4 Answers4

0

I'm not aware of the Colebrook equation, but assuming the formula in this line:

1/math.sqrt(friction)=-2*math.log10((((eD/3.7)+(2.51/Re))*(1.14-2*math.log10(((eD/2)+(21.25/Re**0.9))))))

is correct, we can just rearrange to

import math
Re = 2300
eD = 0.0009 
friction = (1/(-2*math.log10((((eD/3.7)+(2.51/Re))*(1.14-2*math.log10(((eD/2)+(21.25/Re**0.9))))))))**2
print(friction)

Giving us

friction = 0.05073525684035217
CDJB
  • 14,043
  • 5
  • 29
  • 55
0

If you simply want the value (and not trying to demonstrate that you can solve it yourself) a python module already exists, see: colebrook

0

There are multiple approximations of the Colebrook equation. See here Which one are you referring to? It helps to know what you are trying to do more specifically.

Aside from that, there is an easy issue here.

1/math.sqrt(friction)=-2*math.log10((((eD/3.7)+(2.51/Re))*(1.14-2*math.log10(((eD/2)+(21.25/Re**0.9))))))

In Python (and most languages I've ever used), you can't write equations this way. The computer will not solve algebra for you. If you solve to get f=SomeEquation(), only then can the computer actually do the math for you.

In your second example there are also issues.

import math
def friction(Re, eD):
    eD= 0.0009
    Re= 2300
    f = -2*math.log10((((eD/3.7)+(2.51/Re))*(1.14-2*math.log10(((eD/2)+(21.25/Re**0.9))))))
    return 1/math.sqrt(f)

You have defined a function which takes Re and eD, but then you immediately redefine them in thee function. There is a way to set a default value for these variables if you choose not to pass them in, but I don't think that's what you are trying to do here. Furthermore, (and you may very well have just left this part out for the sake of brevity) you never actually called your function, which means your script never actually does anything.

You need something like (and I'm not checking your equation here because I don't know which one you were going for):

import math

# Function definition
def friction(Re, eD):
    f = -2*math.log10((((eD/3.7)+(2.51/Re))*(1.14-2*math.log10(((eD/2)+(21.25/Re**0.9))))))
    return 1/math.sqrt(f)

# Call your function with your desired values (arguments) and store the result in f
f = friction(2300, 0.0009)

Hope that helped.

LLSv2.0
  • 501
  • 6
  • 20
0
import math
Re = 2300
eD = 0.0009 
friction = (1/(-2*math.log10((((eD/3.7)+(2.51/Re))*(1.14-2*math.log10(((eD/2)+(21.25/Re**0.9))))))))**2
print(friction)

Output: 0.05073525684035217

import colebrook
Re = 2300
eD = 0.0009 
factor = colebrook.sjFriction( Re, eD)
print(factor)

Output: 0.0495

For further help you could visit https://pypi.org/project/colebrook/