试图解决由4个方程和4个未知数组成的系统。由于“函数调用结果不是正确的浮点数数组”而不断出错。我是python新手,所以我认为问题出在我的方程式定义中。
我尝试了fsolve,sympy.solve,并带有和不带有定义。
L0_fcc = 5200
L0_bct = 12000
L0_l = 4700
R = 8.3144
def equations(p):
t, XSnl, XSnfcc, XSnbct = p
GPb_fcc_bct = 489 + 3.52 * t
GPb_fcc_l = 4810 - 8.017 * t
GSn_bct_fcc = 5510 - 8.46 * t
GSn_bct_l = 7179 - 14.216 * t
GSn_fcc_l = 1661 - 5.756 * t
E1 = sp.Eq(GPb_fcc_l + R * t * sp.log((1-XSnl)/(1-XSnfcc)) + L0_l * (XSnl**2) - L0_fcc * (XSnfcc**2))
E2 = sp.Eq(GPb_fcc_bct + R * t * sp.log((1-XSnbct)/(1-XSnfcc)) + L0_bct * (XSnbct**2) - L0_fcc * (XSnfcc**2))
E3 = sp.Eq(GSn_fcc_l + R * t * sp.log(XSnl/XSnfcc) + L0_l * ((1-XSnl)**2) - L0_fcc * ((1-XSnfcc)**2))
E4 = sp.Eq(GSn_bct_l + R * t * sp.log(XSnl/XSnbct) + L0_l * ((1-XSnl)**2) - L0_bct * ((1-XSnbct)**2))
return (E1, E2, E3, E4)
x0 = [300, 0, 0, 0]
t, XSnl, XSnfcc, XSnbct = fsolve(equations, x0)
print(t, XSnl, XSnfcc, XSnbct)`
它应该带有4个值,其中3个值应介于0和1之间。我得到“来自函数调用的结果不是正确的浮点数组”
最佳答案
我不确定为什么您会期望sympy
对象与scipy
求解器进行交互,它们是完全不同的库。前者是符号对象,后者是数值分析。
解决方案是将以下几行简单地更改为:
E1 = (GPb_fcc_l + R * t * np.log((1-XSnl)/(1-XSnfcc)) + L0_l * (XSnl**2) - L0_fcc * (XSnfcc**2))
E2 = (GPb_fcc_bct + R * t * np.log((1-XSnbct)/(1-XSnfcc)) + L0_bct * (XSnbct**2) - L0_fcc * (XSnfcc**2))
E3 = (GSn_fcc_l + R * t * np.log(XSnl/XSnfcc) + L0_l * ((1-XSnl)**2) - L0_fcc * ((1-XSnfcc)**2))
E4 = (GSn_bct_l + R * t * np.log(XSnl/XSnbct) + L0_l * ((1-XSnl)**2) - L0_bct * ((1-XSnbct)**2))
return (E1, E2, E3, E4)
现在,这将导致
RuntimeWarning: invalid value encountered in long_scalars
和RuntimeWarning: divide by zero encountered in double_scalars
,最后是RuntimeWarning: The iteration is not making good progress, as measured by the improvement from the last ten iterations.
,但这是一个算法错误,您必须自己弄清楚。(几乎肯定是糟糕的开始条件,因为在第一次迭代中有一个
XSnl/XSnfcc == 0/0
项)寻找方程的数值解,如果通常不是艺术而是科学。
关于python - 同步方程求解器,4个方程式4个未知数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56781761/