解决隐式函数并传入三个参数 [英] Solving implicit function and passing in three arguments
问题描述
在上面的等式中,我想求解 f 并传入 Re、D 和 epsilon.这是我的代码如下:
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)
我会使用 fsolve() 还是 solve()?我在 Python 的主站点上阅读了 fsolve() ,但是我不明白它想要的一些输入.先感谢您!
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!
另外,我使用的是 Spyder (Python 3.6)
Also, I am using Spyder (Python 3.6)
推荐答案
维基百科页面上的达西摩擦"因子公式" 有一个关于Colebrook 方程的部分a>,并展示了如何使用 Lambert W 函数以其他参数表示 f>.
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 实现了 Lambert W 函数,因此您可以使用它来计算 f 而不使用数值求解器:
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
例如
In [84]: colebrook(125000, 0.315, 0.00015)
Out[84]: 0.019664137795383934
为了进行比较,https://www.engineeringtoolbox.com/colebrook 上的计算器-equation-d_1031.html 给出 0.0197.
For comparison, the calculator at https://www.engineeringtoolbox.com/colebrook-equation-d_1031.html gives 0.0197.
这篇关于解决隐式函数并传入三个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!