我试图使用sympy的solve命令解决方程,但我的输出是一个空列表[].我认为这可能发生的唯一原因是因为没有解决方案,但我怀疑这是原因.有谁知道为什么我没有得到答案?谢谢!
from sympy import *
class WaterModel:
def fp_requirement(self, Ws0, Wp0, Wg0):
greyW = 60.0
potW = 126.0
rainW = 17.05
self.Ws0 = Ws0
self.Wp0 = Wp0
self.Wg0 = Wg0
self.fp = var('fp')
filt_greyW = self.fp*greyW
dWg = self.Wg0 - greyW + (1 - self.fp)*greyW + rainW
dWp = self.Wp0 - potW + filt_greyW
F = self.Ws0 + dWp + dWg
self.fp = solve(F,self.fp)
return self.fp
a = WaterModel()
fp = a.fp_requirement(1500, 100, 100)
print(fp)
解决方法:
我尝试在你的函数中添加一些跟踪语句,就在求解之上.
print "dWg\t", dWg, type(dWg)
print "dWp\t", dWp, type(dWp)
print "F\t", F, type(F)
print "self.fp\t", self.fp
self.fp = solve(F,self.fp)
输出:
dWg 117.050000000000 - 60.0000000000000*fp <class 'sympy.core.add.Add'>
dWp -26.0000000000000 + 60.0000000000000*fp <class 'sympy.core.add.Add'>
F 1591.05000000000 <class 'sympy.core.numbers.Real'>
self.fp fp
[]
如果我正确地读这个,你的函数会评估表达式,而不是保持F的符号性质.因此,当你发出solve指令时,你试图为变量fp求解一个常量.这就是为什么你没有解决方案.
啊,哈!它就是!
1500 + 117 - 26 - 60*fp + 60*fp => 1591
随着fp的出现,没有解决方案.