[英]Solve highly non-linear equation for x in Python
我試圖解決dB的以下等式(為簡單起見,我在問題標題中將dB表示為x):
等式中的所有其他項都是已知的。 我嘗試使用SymPy來象征性地解決dB,但我一直在節省時間。 我也嘗試過使用fminbound
的scipy.optimize
但dB的答案是錯誤的(參見下面的使用fminbound
方法的Python代碼)。
有沒有人知道使用Python解決dB方程的方法?
import numpy as np
from scipy.optimize import fminbound
#------------------------------------------------------------------------------
# parameters
umf = 0.063 # minimum fluidization velocity, m/s
dbed = 0.055 # bed diameter, m
z0 = 0 # position bubbles are generated, m
z = 0.117 # bed vertical position, m
g = 9.81 # gravity, m/s^2
#------------------------------------------------------------------------------
# calculations
m = 3 # multiplier for Umf
u = m*umf # gas superficial velocity, m/s
abed = (np.pi*dbed**2)/4.0 # bed cross-sectional area, m^2
# calculate parameters used in equation
dbmax = 2.59*(g**-0.2)*(abed*(u-umf))**0.4
dbmin = 3.77*(u-umf)**2/g
c1 = 2.56*10**-2*((dbed / g)**0.5/umf)
c2 = (c1**2 + (4*dbmax)/dbed)**0.5
c3 = 0.25*dbed*(c1 + c2)**2
dbeq = 0.25*dbed*(-c1 + (c1**2 + 4*(dbmax/dbed))**0.5 )**2
# general form of equation ... (term1)^power1 * (term2)^power2 = term3
power1 = 1 - c1/c2
power2 = 1 + c1/c2
term3 = np.exp(-0.3*(z - z0)/dbed)
def dB(d):
term1 = (np.sqrt(d) - np.sqrt(dbeq)) / (np.sqrt(dbmin) - np.sqrt(dbeq))
term2 = (np.sqrt(d) + np.sqrt(c3)) / (np.sqrt(dbmin) + np.sqrt(c3))
return term1**power1 * term2**power2 - term3
# solve main equation for dB
dbub = fminbound(dB, 0.01, dbed)
print 'dbub = ', dbub
以下是四個單調的根方法:
from scipy.optimize import brentq, brenth, ridder, bisect
for rootMth in [brentq, brenth, ridder, bisect]:
dbub = rootMth(dB, 0.01, dbed)
print 'dbub = ', dbub, '; sanity check (is it a root?):', dB(dbub)
還有newton-raphson(割線/哈利)方法:
from scipy.optimize import newton
dbub = newton(dB, dbed)
print 'dbub = ', dbub, '; sanity check (is it a root?):', dB(dbub)
如果你有一個包圍間隔,scipy文檔建議使用brentq。
要解決標題中的內容很簡單:
In [9]:
import numpy as np
import scipy.optimize as so
In [10]:
def f(x):
return ((x-0.32)**0.8+(x+1.45)**1.1-np.exp(0.8))**2
In [11]:
so.fmin(f, x0=5)
Optimization terminated successfully.
Current function value: 0.000000
Iterations: 20
Function evaluations: 40
Out[11]:
array([ 0.45172119])
In [12]:
f(0.45172119)
Out[12]:
4.7663411535618792e-13
所有其他參數都是固定的?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.