2014-11-03 130 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