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

2024-09-30 16:40:06 发布

您现在位置:Python中文网/ 问答频道 /正文

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(Python3.6)


Tags: 代码fromimportremathscipysqrteps
1条回答
网友
1楼 · 发布于 2024-09-30 16:40:06

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。你知道吗

相关问题 更多 >