AlanSE

Solver of the Colebrook equation in VB

Dec 10th, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' Useful implementation of Colebrook-White equation solver
  2. Function Colebrook2(Rr As Double, Re As Double) As Double
  3.   Dim f_min As Double, f_max As Double, f As Double
  4.   Dim g_lower As Double, g_middle As Double
  5.   Dim n As Integer
  6.  
  7.   f_min = (2.51 / Re) ^ 2 * (1 - Rr / 3.7) ^ (-2)
  8.   f_max = ((2.51 / Re + Log(10) / 2) / (1 - Rr / 3.7)) ^ 2
  9.   n = 0
  10.  
  11.   Do
  12.     n = n + 1
  13.     f = (f_min + f_max) / 2
  14.     g_middle = -2 / Log(10) * Log(Rr / 3.7 + 2.51 / Re * f ^ (-1 / 2)) - f ^ (-1 / 2)
  15.     g_lower = -2 / Log(10) * Log(Rr / 3.7 + 2.51 / Re * f_min ^ (-1 / 2)) - f_min ^ (-1 / 2)
  16.     If (g_middle = 0 Or (f_max - f_min) / 2 < 0.000001) Then
  17.       Exit Do
  18.     Else
  19.       If (g_middle * g_lower > 0) Then
  20.         f_min = f
  21.       Else
  22.         f_max = f
  23.       End If
  24.     End If
  25.     Colebrook2 = f
  26.     If (n > 100) Then
  27.       Colebrook2 = CVErr(xlErrNA)
  28.       Exit Do
  29.     End If
  30.   Loop
  31. End Function
Advertisement
Add Comment
Please, Sign In to add comment