# Fixed point iteration applied to Coolebrook-White

My first blog post is about the *fixed point iteration* method. A simple method to solve (implicit) equations iteratively. It is applied to the Colebrook-White equation and solved in a Python script and Excel.

### Explicit and implicit functions

An explicit function of the form

(1)

can usually be solved directly. An implicit function of the form

(2)

may not be solved directly. This is because there is an implicitely defined relationship between the left and right side of the function. Therefore, these functions can be solved iteratively.

### Friction factor using Coolebrook-White

The dimensionless friction factor is used to calculate the pressure loss due to friction. The Colebrook-White equation [1, p. 78] can be applied for turbulent flow ( 2300 in pipes) and at Reynolds number larger than 4000. The Darcy friction factor using the Colebrook-White equation is calculated as follows:

(3)

**Necessary Parameters:**

- Reynolds number
- Pipe diameter
- Pipe surface roughness

**The friction factor is on both sides of the equation (implicit dependency).**

### Solving the equation using Python

Basically, a solution of a previous calculation step is used to recalculate the equation. This is repeated, until it converges. This practically means, that the deviation between the current and the previous solution is lower than an accepted tolerance. There are two termination condition for the loop: the relative deviation to the previous result and a maximum amount of iterations.

Within Python you can solve this equation very easily using the following code.

- To access basic mathematical operations e.g.
*log*the dependency*math*is imported. - The input parameters are defined.
- Afterwards, the fixed point iteration function is defined.
- The Colebrook white equation is defined (Solved for on the left side).
- A initial guess value needs to be provided to start the iteration process.
- The function is called.

#License: MIT License (http://opensource.org/licenses/MIT)

import math

# Input parameter

k = 5e-5 #[m]

d = 0.05 #[m]

Re = 50000 #[-]

# Function for fixed point iteration

def fixedPoint(f, x0, rtol=1e-7, maxiter=50):

e = 1

it = 0

while(e > rtol and it < maxiter):

x = f(x0)

e = math.fabs((x-x0)/math.fabs(x))

x0 = x

it = it + 1

return x

# Colebrook-White equation for Darcy friction factor

def fColebrookWhite(x):

return 1/(-2*math.log10((2.51/(Re*math.sqrt(x))) + (k/(3.7*d))))**2

#Initial guess value

fGuess = 0.5

# Call function

fFriction = fixedPoint(fColebrookWhite, fGuess)

print(fFriction)

### Stop criteria

I used the relative deviation between the solutions as a stop criteria for the iterations. Hence, the relative deviation is calculated from:

(4)

Therefore, I changed the function *fixedPoint* to *fixedPointRes*.

#License: MIT License (http://opensource.org/licenses/MIT)

import numpy as np

import matplotlib.pyplot as plt

def fixedPointRes(f, x0, rtol=1e-7, maxiter=50):

it = 0

e =np.array([1])

iteration = np.array([0])

while(it < maxiter and e[it] > rtol):

x = f(x0)

e= np.append(e,math.fabs((x-x0)/math.fabs(x)))

iteration=np.append(iteration, it)

x0 = x

it = it + 1

fig = plt.figure(1)

fig = fig.add_subplot(111)

plt.plot(iteration, e,'--', color='red', linewidth=2.0)

fig.set(xlabel='Iteration', ylabel='Relative deviation')

plt.show()

plt.savefig('rel_error_deviation.png')

return x

The change of the relative deviation is plotted in the following figure.

### Solving the equation using Excel

In Excel the same input data needs to be provided. Again, the solution of the previous calculation step is used to recalculate it. This is repeated until a desired deviation from the previous step is achieved.

### Conclusion

There is a vast amount of methods to solve (implict) equations iteratively. In this post, a very basic one is presented to make the subject more accessible. The choice of the initial guess values has a strong impact on the number of iterations needed. For some equations it is not easy to find a suitable initial guess value.

### Hints regarding Colebrook-White

- You can find explicit equations which deliver similar results. So no iterations are needed. As a result, you can save computational costs.
- Finally, don’t confuse Darcy and Fanning friction factor ().

### Sources

[1] Rennels, D. C., Hudson H. M.: PIPE FLOW A Practical and Comprehensive Guide. John Wiley & Sons, 2012.### What did you think?

I’d like to hear what you think about this post.

Let me know by leaving a comment below.

Good job. I like the explanation especially the python code. What is about the convergence of the fixpoint iteration? Not every equation converge to a solution.

Best Greeting,

Fabian

Thanks for your feedback!

Good question! Convergence problems can occur if the function shows no Lipschitz continuity for all the values in the investigated area. Furthermore, the initial value needs to be close enough to a root.

Best regards,

Johannes