简体   繁体   English

当我求解方程组时发生TypeError

[英]TypeError occurs when I solve this system of equations

from math import exp, log
from sympy import Symbol, symbols, solve, Eq
Z = 70;p_abs = 101.325*((1-2.25577*(10**-5)*Z)**5.2559);t_std = 15-0.0065*Z;RH = 0.5;t_db = 20
K_db = t_db + 273.15
C8 = -5.8002206e+03C9 = 1.3914993e+00;C10 = -4.8640239e-02;C11 = 4.1764768e-05;C12 = -1.4452093e-08;C13 = 6.5459673e+00
p_ws_db = exp(C8/K_db+C9+C10*K_db+C11*K_db**2+C12*K_db**3+C13*log(K_db))/1000
p_w_db = p_ws_db*RH # partial pressure of water vapor_db
W_s_db = 0.621945*p_ws_db/(p_abs-p_ws_db)
W_db = 0.621945*p_w_db/(p_abs-p_w_db)
p_w_wb = (W_db*p_abs)/(0.621945+W_db)
t_wb, K_wb, W_s_wb, p_ws_wb = symbols('t_wb K_wb W_s_wb p_ws_wb')
e1 = Eq(K_wb, t_wb + 273.15)
e2 = Eq(p_ws_wb, exp(C8/K_wb+C9+C10*K_wb+C11*K_wb**2+C12*K_wb**3+C13*log(K_wb))/1000)
e3 = Eq(W_s_wb, (W_db*((2501+1.86*t_db)-4.186*t_wb)+1.006*(t_db-t_wb))/(2501-2.326*t_wb))
e4 = Eq(p_ws_wb, (W_s_wb*p_abs)/(W_s_wb+0.621945))
print(fsolve([e1,e2,e3,e4], t_wb, p_ws_wb, K_wb, W_s_wb))

This is the code what have 4 equation and 4 unknowns. 这是具有4个方程式和4个未知数的代码。 I am trying solving this equations by using sympy.solve. 我正在尝试使用sympy.solve解决这个方程式。 But it can't solve with an error; 但是它不能解决错误。

TypeError: can't convert expression to float.

I guess exp or log create a problem. 我猜exp或日志会产生问题。

print(fsolve([e1,e2,e3,e4], t_wb, p_ws_wb, K_wb, W_s_wb))

There is no fsolve in SymPy. 没有fsolve在SymPy。 There is fsolve in SciPy, a numeric solver. 数字求解器SciPy中有fsolve SciPy cannot work with SymPy objects, in particular it convert convert them to floats. SciPy无法使用SymPy对象,特别是它将对象转换为浮点型。 Neither can exp and log that you imported from math . math导入的explog都无法进行。

Understand the difference between numeric computation and symbolic computation. 了解数值计算和符号计算之间的区别。 If you want to compute exp(a) and a is a symbol, you need symbolic exp from SymPy, not a numeric exp from math or NumPy or somewhere else. 如果要计算exp(a)a是符号,则需要SymPy中的符号exp ,而不是math或NumPy或其他地方的数值exp

Correct import statements: 正确的进口声明:

from sympy import symbols, Eq, exp, log
from scipy.optimize import fsolve

Correct execution of numerical solution: 正确执行数值解:

eqns = lambdify([t_wb, p_ws_wb, K_wb, W_s_wb], [e.lhs - e.rhs for e in [e1, e2, e3, e4]])    
print(fsolve(lambda x: eqns(*x), [10, 10, 10, 10]))

Here, lambdify creates a function that SciPy can use, from the SymPy objects you have. 在这里, lambdify从您拥有的SymPy对象创建一个SciPy可以使用的函数。 It's a main tool for using SymPy objects in numeric modules. 它是在数字模块中使用SymPy对象的主要工具。 The created function takes four arguments, listed first, and returns four outputs, which are the difference between the left and right sides of each equation. 创建的函数采用四个参数(第一个列出),并返回四个输出,它们是每个等式的左侧和右侧之间的差。

The initial vector [10, 10, 10, 10] is just a guess for solution, fsolve needs it as a starting point. 初始向量[10, 10, 10, 10] fsolve [10, 10, 10, 10]只是对解的猜测, fsolve需要它作为起点。 The solution it finds is 找到的解决方案是

[1.51295417e+03 7.10681717e+01 1.78610417e+03 1.50246122e+00]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM