解决隐函数并传入三个参数

时间:2018-07-01 18:41:35

标签: python scipy

enter image description here

在上面的公式中,我想求解f并传递Re,D和epsilon。这是我的代码如下:

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)

我将使用fsolve()还是solve()?我在Python主站点上阅读了fsolve(),但是我不理解它想要的一些输入。先感谢您!

此外,我正在使用Spyder(Python 3.6)

1 个答案:

答案 0 :(得分:1)

wikipedia page on "Darcy friction factor formulae"Colebrook equation上有一个部分,展示了如何使用Lambert W function根据其他参数来表示f。

SciPy has an implementation of the Lambert W function,因此您可以使用它来计算f,而无需使用数值求解器:

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

例如,

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

为了进行比较,https://www.engineeringtoolbox.com/colebrook-equation-d_1031.html处的计算器给出了0.0197。