import numpy as np
import math
from scipy.optimize import fsolve
def func(x):
return [x[0] * 0.00096 + x[1]*0.0259*np.log(0.00096/x[2]) - 1.2505,
x[0] * 0.00301 + x[1]*0.0259*np.log(0.00301/x[2]) - 1.2829,
x[0] * 0.000077 + x[1]*0.0259*np.log(0.000077/x[2]) - 1.1789,]
root = fsolve(func, [66, 1, 43])
所以问题是 fsolve 无法为上面的方程组找到解,因为:
<ipython-input-19-7ecad596ddb5>:2: RuntimeWarning: invalid value encountered in log
return [x[0] * 0.00096 + x[1]*0.0259*np.log(0.00096/x[2]) - 1.2505,
<ipython-input-19-7ecad596ddb5>:3: RuntimeWarning: invalid value encountered in log
x[0] * 0.00301 + x[1]*0.0259*np.log(0.00301/x[2]) - 1.2829,
<ipython-input-19-7ecad596ddb5>:4: RuntimeWarning: invalid value encountered in log
x[0] * 0.000077 + x[1]*0.0259*np.log(0.000077/x[2]) - 1.1789,]
我完全不知道该怎么办。可能是什么问题?
附注我忘了提到变量必须是非零且正数(x[1] 在 1 和 2 之间,x[2] 必须是 10 的负数次方)
你可以通过分析来解决它(抱歉:我还在尝试打出证明!)
不过,我不同意所有价值观都是积极的。
import math
a, b, c, d, e = 0.00096, 0.00301, 0.000077, 0.0259, 0.00096
f, g, h = 1.2505, 1.2829, 1.1789
det = d * ( ( a - c ) * math.log( b /c ) - ( b - c ) * math.log( a / c ) )
y = ( ( g - h ) * ( a - c ) - ( f - h ) * ( b - c ) ) / det
x = ( g - h - d * math.log( b / c ) * y ) / ( b - c )
logz = ( a * x + d * math.log( a ) * y - f ) / ( d * y )
z = math.exp( logz )
print( "x, y, z = ", x, y, z )
print( "Check:" )
print( 0.00096 * x + 0.0259 * y * math.log( 0.00096 / z ) - 1.2505 )
print( 0.00301 * x + 0.0259 * y * math.log( 0.00301 / z ) - 1.2829 )
print( 0.000077 * x + 0.0259 * y * math.log( 0.000077 / z ) - 1.1789 )
结果:
x, y, z = -0.017395040681110335 1.095890481308894 7.050533107244206e-23
Check:
0.0
0.0
0.0