2014-11-03 104 views
0

我有一個方程'a * x + logx-b = 0,(a和b是常數)',我想解決x。問題是我有許多常量a(因此有許多b)。我如何通過使用python解決這個公式?Python解決非線性(收費)方程

+1

這是很難的問題 - 看http://math.stackexchange.com/questions/433717/how-to-solve-equations-with-logarithms-like-this-ax-b-logx-c- 0 – 2014-11-03 19:02:43

+0

請參閱http://en.wikipedia.org/wiki/Transcendental_equation尋找解決收縮方程的一般方法 – 2014-11-03 19:06:47

+0

最後在python中求解超縮進方程的數值方法在這裏http://stackoverflow.com/questions/15649134/using-python -to-solve-a-nonlinear-equation – 2014-11-03 19:09:01

回答

0

酷 - 今天我瞭解了Python的數值解算器。

from math import log 
from scipy.optimize import brentq 

def f(x, a, b): 

    return a * x + log(x) - b 


for a in range(1,5): 
    for b in range(1,5): 
     result = brentq(lambda x:f(x, a, b), 1e-10, 20) 
     print a, b, result 

brentq提供了函數穿過x軸的位置。你需要給它兩點,一點肯定是消極的,一點肯定是積極的。對於負點選擇小於exp(-B)的數字,其中B是最大值b。對於正數選擇大於B的數字。

如果無法預測b值的範圍,則可以使用求解器代替。這可能會產生一個解決方案 - 但這並不能保證。

from scipy.optimize import fsolve 


for a in range(1,5): 
    for b in range(1,5): 
     result = fsolve(f, 1, (a,b)) 
     print a, b, result