Iteration and the Colebrook-White Formula

This is a discussion on Iteration and the Colebrook-White Formula within the C++ Programming forums, part of the General Programming Boards category; Howdy all, I just started programming some 1 days ago, I have the program complete except for the following problem-- ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    7

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    7
    Quote Originally Posted by tabstop View Post
    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. #4
    Registered User
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    7
    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. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Code:
    } while (tpfc = tpfc2);
    = is for assignment, == is for comparison

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    7
    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. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #10
    Registered User
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    7
    Quote Originally Posted by tabstop View Post
    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'?

    Name:  console.jpg
Views: 935
Size:  45.8 KB

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Sorry, should be while > 1e-6. (You want to continue while the error is too large.)

  12. #12
    Registered User
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    7
    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. #13
    Registered User
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    7
    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. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Iteration?
    By Aliaks in forum C++ Programming
    Replies: 2
    Last Post: 06-06-2009, 11:17 PM
  2. Iteration
    By juststartedC in forum C Programming
    Replies: 3
    Last Post: 11-18-2007, 07:00 AM
  3. Nested Iteration??
    By Mewr in forum C++ Programming
    Replies: 7
    Last Post: 08-04-2003, 08:03 AM
  4. Iteration and recursion?
    By Munkey01 in forum C++ Programming
    Replies: 15
    Last Post: 01-18-2003, 07:16 PM
  5. Iteration help please
    By incognito in forum C++ Programming
    Replies: 3
    Last Post: 12-09-2001, 06:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21