Python 求解隐函数并传递三个参数

Python 求解隐函数并传递三个参数,python,scipy,Python,Scipy,在上面的方程中,我想解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 *

在上面的方程中,我想解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,其中有一节介绍了,并展示了如何使用其他参数来表示f

,因此您可以使用它来计算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

为便于比较,计算器的读数为0.0197。

好的,谢谢您提供有关SciPy的信息。这非常有帮助。你能给我解释一下这行代码在做什么吗?x=-lambertw lnp/a*math.powp,-b/a/lnp-b/a