Thread: Iteration and the Colebrook-White Formula

1. Iteration and the Colebrook-White Formula

Howdy all,

I just started programming some 1 days ago, I have the program complete except for the following problem-- iteration. I consider myself pretty engineering savy when it comes to Excel and have created a spreadsheet to calculate this already, but I would like to create a simple program to do it as well.

Just as a reference my Excel formula (modified for my C++ names instead of cells)is:

Code:
```=IF(re>4000,(1/(-2*LOG(rrough/3.7 + 2.51/(rnum*SQRT(SAMECELL+1E-300))))^2),"0")

This code uses iteration and circular references within Excel to get a value to 1-E6 accuracy or 1000 iterations.```
For those unfamiliar, the equation deals with friction loss through a pipe and outputs a friction factor to then be used in a pressure drop calculation. All of the required values are already either inputted or calculated. The problem is the same factor on both sides of the equation (f), see below.

The original equation is:
Code:
`1/sqrt(f) = -2 * log10((e/3.7D) + ((2.51/Re*sqrt(f))))`
Since my experience in C++ is lacking I was looking for someone to "show me the way," I am not used to doing math the way C++ handles it. I currently have most of the pieces defined, but I know I do not have this setup correctly.

Code:
```//TURBULENT
if (rnum > 4000) {
t1 = 1
t2 = -2
t3 = log
t4 = rrough
t5 = 3.7
t6 = 2.51
t7 = rnum
t8 =
t9 =
tpfc =
cout<< "TURBULENT (RE > 4000)\n";
cout<<"\n";
cout<< "PIPE FRICTION COEFFICIENT (F=): \n";
cout<< tpfc << endl;
}```
I think I would limit the program to 500 iterations and/or 1-E6 accuracy. If I can provide anymore detail I will.

Regards,
McJ

2. All your t variables aren't needed. When you put your Excel formula into C++ you need to remember that x^2 is represented by pow(x,2) (and that if the power is in the denominator, it's really a negative power) and that common log is represented by log10 rather than log, so you would have
Code:
`x = pow(-2*log10(rrough/3.7 + 2.51/(rnum*sqrt(x+1e-300))), -2);`
inside a big loop.

3. Originally Posted by tabstop
All your t variables aren't needed. When you put your Excel formula into C++ you need to remember that x^2 is represented by pow(x,2) (and that if the power is in the denominator, it's really a negative power) and that common log is represented by log10 rather than log, so you would have
Code:
`x = pow(-2*log10(rrough/3.7 + 2.51/(rnum*sqrt(x+1e-300))), -2);`
inside a big loop.
Interesting, it looks like I have done a bunch of defining variables when that was un-needed. Just another thing to put down for the ol'learning curve.

Now when you mention a big-loop, I am assuming I will be defining iteration and tolerances and have the loop run until one of those two conditions are met. I will do some searching on loops to learn about them.

Regards,
McJ

4. So I would need to use a "Do" statement?

From MSDN:
Code:
```do
{
tpfc = pow ( -2 * log10 (rrough / 3.7 + 2.51 / ( rnum * sqrt (tpfc2 + 1e - 300 ))), -2);;
} while (tpfc = tpfc2);```

5. If you want to, I suppose. What you have written will never end, so you maybe want to come up with a way to make it stop.

6. Code:
`} while (tpfc = tpfc2);`
= is for assignment, == is for comparison

7. What would be the point in looping while they are equal anyway? Don't you want to loop until two things are equal, i.e. while the two things are not equal?

Or more usefully, loop until the two things are close enough to equal, within a certain tolerance.

8. Indeed you are all correct.

iMalc
What would be the point in looping while they are equal anyway? Don't you want to loop until two things are equal, i.e. while the two things are not equal?

Or more usefully, loop until the two things are close enough to equal, within a certain tolerance
I would prefer a tolerance of 1-E6. But am still unsure as to how to make that happen. The goal of the formula is to use iteration to solve for the factor (f) until they both are more or less equal.

If you want to, I suppose. What you have written will never end, so you maybe want to come up with a way to make it stop.
The program "solves" and will end just fine currently (whether or not it is supposed to I am not sure). However, the end result is incorrect. No matter the other input values it outputs -1.

9. You probably want to go while fabs(old - new) < 1e-6 then. Currently your loop will only stop when you value is zero. Probably what is happening is you assign tpfc2 a value of zero, compute with it to get a value of -1, then re-assign zero back to the old value while stopping the loop.

10. Originally Posted by tabstop
You probably want to go while fabs(old - new) < 1e-6 then. Currently your loop will only stop when you value is zero. Probably what is happening is you assign tpfc2 a value of zero, compute with it to get a value of -1, then re-assign zero back to the old value while stopping the loop.
I have tried your method and came up with:

Code:
```do
{
tpfc = pow ( -2 * log10 (rrough / 3.7 + 2.51 / ( rnum * sqrt (tpfc2 + 1e - 300 ))), -2);
}
while (fabs (tpfc - tpfc2) < 1e-6);```
Still returns the same issue: -1 for the friction coefficient (See attached image). In theory, since "f" = "f" should 'tpfc2' be 'tpfc'?

11. Sorry, should be while > 1e-6. (You want to continue while the error is too large.)

12. Changed the '<' sign as suggested and the result is the same: -1 for the friction coefficient.

Code:
```//TURBULENT
if (rnum > 4000) {

do
{
tpfc = pow ( -2 * log10 (rrough / 3.7 + 2.51 / ( rnum * sqrt (tpfc2 + 1e - 300 ))), -2);
}
while (fabs (tpfc - tpfc2) > 1E-6);
cout<< "TURBULENT (RE > 4000)\n";
cout<<"\n";
cout<< "PIPE FRICTION COEFFICIENT (F=): \n";
cout<< tpfc << endl;
}```
Is scientific notation usable in this environment? Seems like I am missing something pretty basic.

13. All, I have now been trying to use the MatLab code in C++, I get a result, a subsequent iterations change the values, I believe the result is still in correct-- perhaps not, I will check.

Code:
```//TURBULENT
if (rnum > 4000) {

X1 = rrough * rnum * 0.123968186335418;
X2 = log10(rnum) - 0.779397488455682;
F = X2 - 0.2;

// First Iteration
E = (log10(X1 + F) + F - X2) / (1 + X1 + F);
F = F - (1 + X1 + F + 0.5 * E) * E * (X1 + F) / (1 + X1 + F + E * (1 + E / 3));

// Second Iteration
E = (log10(X1 + F) + F - X2) / (1 + X1 + F);
F = F - (1 + X1 + F + 0.5 * E) * E * (X1 + F) / (1 + X1 + F + E * (1 + E / 3));

//Solution
F = 1.15129254649702 / F;
Colebrook = F * F;

cout<< "TURBULENT (RE > 4000)\n";
cout<<"\n";
cout<< "PIPE FRICTION COEFFICIENT (F=): \n";
cout<< Colebrook << endl;
}```
Will this "style" of notation work in C?

Regards,
McJ

14. Scientific notation is usable, assuming you know what scientific notation looks like
Code:
`1e-300`
is scientific notation.
Code:
`1e - 300`
is one minus 300, or -299. (I don't know whether the spaces got added in just in pasting, but.)