我需要从python 3.2中的fsolve获取所有解决方案(2个未知数)。
但是,虽然我在fsolve中设置了full_output = true,但它只返回一个事件。
def integrand1(x, b, c):
return (x/b)**(c-1) * exp(-x/b)
def intergralFunc2(b):
integral,err = quad(integrand2, 0, 100, args=(b))
return 100 - integral
def solveFunction():
sol= fsolve(intergralFunc1, 5, 5, full_output=True)
return sol
if __name__ == '__main__':
sol = solveFunction()
print("sol is ", sol)
我需要知道b和c的解决方案,但是,fsolve()只返回b的解决方案。
答案 0 :(得分:1)
使用fmin
(请参阅documentation)代替fsolve
并更改integralFunc
,以便包含b
和c
的序列并返回abs(100 - integral)
将获得两个参数。
from scipy.integrate import quad
from scipy.optimize import fmin
def integrand(x, b, c):
return (x/b)**(c-1) * exp(-x/b)
def intergralFunc(bc):
integral,err = quad(integrand, 0, 100, args=(bc[0], bc[1]))
return abs(100 - integral)
def solveFunction():
sol= fmin(intergralFunc, [5, 5])
return sol
b, c = solveFunction()
输出:
Optimization terminated successfully.
Current function value: 0.000000
Iterations: 49
Function evaluations: 89
检查b
和c
>>> integral, err = quad(integrand, 0, 100, args=(b, c))
>>> np.allclose(100, integral)
True
修改强>
如果你 使用fsolve
,这将有效,因为fsolve
期望目标函数的形状输出与输入相同,但你会b
和c
的结果与使用fmin
的结果略有不同。我得到b, c = (4.48517289097, 4.95091626134)
fsolve
和b, c = (5.14997777422, 4.85783682022)
fmin
def intergralFunc(bc):
integral,err = quad(integrand, 0, 100, args=(bc[0], bc[1]))
return np.array([100 - integral] * 2)
def solveFunction():
sol= fsolve(intergralFunc, [5, 5])
return sol