我有一个隐式函数要解决:

python - 解决隐式函数-LMLPHP

所以我尝试了从scipy.optimize查找根函数:

- fsolve : RuntimeWarning: The iteration is not making good progress, as
  measured by the improvement from the last ten iterations.
- excitingmixing : NoConvergence
-brent:  RuntimeWarning: invalid value encountered in double_scalars (but
  without a chance to set constraints)


我不得不承认我不确定正确的求解器。有人可以帮忙吗?

最小的工作示例:

from scipy.optimize import fsolve, newton, brent, excitingmixing
import numpy as np

def func_zeta(zeta):
    k= 1.2
    Re = 5000.
    d = 0.03
    return (2.51 /Re/np.sqrt(zeta)+k/d/3.71) + 10**(0.5/ np.sqrt(zeta))
zeta = fsolve(func_zeta, 64/Re)

最佳答案

您翻译公式时出现问题。不应该是下面的return语句吗?

return 2.51 / (Re * np.sqrt(zeta)) + k / (d * 3.71) - 10 ** (-0.5 / np.sqrt(zeta))


但是即使如此,我们仍然得到RuntimeWarning。让我们再试一次以替换zeta

from scipy.optimize import fsolve
import numpy as np

def zeta_in_disguise(x):
    global k, d, Re
    return x + 2 * np.log10(2.51 * x / Re + k / (d * 3.71))

k = 1.2
Re = 5000
d = 0.03
#x = 1 / np.sqrt(zeta)
x = fsolve(zeta_in_disguise, 0)
print(x)
#let's test, if x is really the solution to the equation
print(-2 * np.log10(2.51 * x / Re + k / d / 3.71))


嗯,现在收敛了,并产生以下输出

-2.06528864


这就是问题所在-仅考虑浮点数时,没有解决您给定参数的方法,至少没有解决方案。

关于python - 解决隐式函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48842582/

10-12 16:53
查看更多