gpt4 book ai didi

python - 确定两个值的差异何时达到最小值

转载 作者:太空宇宙 更新时间:2023-11-04 03:47:45 26 4
gpt4 key购买 nike

我有以下代码比较值 CdCdd :

# use Python 3 print function and division
from __future__ import print_function
from __future__ import division

# libraries
import numpy as np

#--- INPUTS

rhog = 1.17 # density (kg/m^3)
ug = 1.85e-5 # dynamic viscosity (kg/ms)
g = 9.81 # gravity (m/s^2)

dp = 0.000207 # diameter of particle (m)
rhos = 2500 # density of particle (kg/m^3)
sp = 0.8 # sphericity of the particle, perfect sphere = 1.0

#--- OUTPUTS

# acceptable difference between Cd and Cdd
dn = 0.02

# determine ut when abs(Cd-Cdd) is a minimum
for ut in np.arange(0, 20, 0.001):
Re = (dp*rhog*ut)/ug
Cd = (24/Re)*(1+(8.1716*np.exp(-4.0655*sp))*Re**(0.0964+0.5565*sp)) + (73.69*np.exp(-5.0748*sp)*Re)/(Re+5.378*np.exp(6.2122*sp))
Cdd = (4*g*dp*(rhos-rhog))/(3*(ut**2)*rhog)
delta = abs(Cd-Cdd)
if delta < dn:
break

print('ut =', ut)
print('Re =', Re)
print('Cd =', Cd)
print('Cdd =', Cdd)
print('delta =', delta)

一切正常,除非代码是针对 sp 的低值运行的例如 sp=0.2这会导致 delta < dn 的标准永远不会发生。

是否有更好的方法来比较 CdCdd条款?

最佳答案

矢量化方法是计算所有值,然后寻找最小值:

ut = np.arange(0, 20, 0.001)
Re = (dp*rhog*ut)/ug
Cd = ((24/Re)*(1+(8.1716*np.exp(-4.0655*sp))*Re**(0.0964+0.5565*sp)) +
73.69*np.exp(-5.0748*sp)*Re)/(Re+5.378*np.exp(6.2122*sp)))
Cdd = (4*g*dp*(rhos-rhog))/(3*(ut**2)*rhog)
delta = np.abs(Cd-Cdd)
idx = np.argmin(delta)

print('ut =', ut[idx])
print('Re =', Re[idx])
print('Cd =', Cd[idx])
print('Cdd =', Cdd[idx])
print('delta =', delta[idx])

因为没有 Python 循环,它可能比您当前的代码运行得更快,即使它可能做了很多不必要的工作。

关于python - 确定两个值的差异何时达到最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22848586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com