Solving implicit function and passing in three arguments

有些话、适合烂在心里 提交于 2021-02-11 13:43:19

问题


enter image description here

In the equation above I want to solve for f and pass in Re, D, and epsilon. Here is my code below:

import math
from scipy.optimize import fsolve

# Colebrook Turbulent Friction Factor Correlation
def colebrook(Re, D, eps):
    return fsolve(-(1 / math.sqrt(f)) - 2 * math.log10(((eps / D) / 3.7) + (2.51 / Re * math.sqrt(f))), f)

Would I use fsolve() or solve()? I read up on fsolve() on Python's main site, however I don't understand some of the inputs it wants. Thank you in advance!

Also, I am using Spyder (Python 3.6)


回答1:


The wikipedia page on "Darcy friction factor formulae" has a section on the Colebrook equation, and shows how f can be expressed in terms of the other parameters using the Lambert W function.

SciPy has an implementation of the Lambert W function, so you can use that to compute f without using a numerical solver:

import math
from scipy.special import lambertw


def colebrook(Re, D, eps):
    """
    Solve the Colebrook equation for f, given Re, D and eps.

    See
        https://en.wikipedia.org/wiki/Darcy_friction_factor_formulae#Colebrook%E2%80%93White_equation
    for more information.
    """
    a = 2.51 / Re
    b = eps / (3.7*D)
    p = 1/math.sqrt(10)
    lnp = math.log(p)
    x = -lambertw(-lnp/a * math.pow(p, -b/a))/lnp - b/a
    if x.imag != 0:
        raise ValueError('x is complex')
    f = 1/x.real**2
    return f

For example,

In [84]: colebrook(125000, 0.315, 0.00015)
Out[84]: 0.019664137795383934

For comparison, the calculator at https://www.engineeringtoolbox.com/colebrook-equation-d_1031.html gives 0.0197.



来源:https://stackoverflow.com/questions/51126008/solving-implicit-function-and-passing-in-three-arguments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!