[英]Trouble solving a system of 6 nonlinear equations in Python
I'm trying to solve a system of 6 non linear equations with Python.我正在尝试用 Python 解决一个由 6 个非线性方程组成的系统。 So far I've got:
到目前为止,我有:
from scipy.optimize import fsolve
import math
from numpy import log
ka1 = 1.045
ka2 = 0.1759
ka3 = 0.3159
def equations(p):
yh2o, yco2, yh2, ych4, yco = p
return (yh2o + yco2 + yh2 + ych4 + yco - 1,
ka1 - (yh2o ** 2)/(yco * (yh2 ** 2),
ka2 - (yco ** 2) / yco2,
ka3 - ych4 / (yh2 ** 2),
0.5 - (2.0 * yco2 + yco + yh2o) / (2 * yh2o + 2 * yh2 + 4 * ych4)))
yh2o, yco2, yh2, ych4, yco = fsolve(equations, [0.2, 0.2, 0.2, 0.2, 0.2])
print(f"yh2o = {yh2o}, yco2 = {yco2}, yh2 = {yh2}, ych4 = {ych4}, yco = {yco}")
When I try to run this I get当我尝试运行它时,我得到
TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument 'equations'.Shape should be (5,) but it is (2,).
I've tried changing the initial guess but the error is still the same.我已尝试更改初始猜测,但错误仍然相同。
I've also tried changing the equations into polynomials like so:我还尝试将方程更改为多项式,如下所示:
return (yh2o + yco2 + yh2 + ych4 + yco - 1.0,
ka1 * (yco * (yh2 ** 2.0)) - (yh2o * yh2o),
ka2 * (yco2) - (yco ** 2.0),
ka3 * (yh2 ** 2.0) - ych4,
0.5 * (2.0 * yh2o + 2.0 * yh2 + 4 * ych4) - (2.0 * yco2 + yco + yh2o))
But I get an error saying但我收到一个错误说
TypeError: can't multiply sequence by non-int of type 'float'
I think you just have problem with parentheses (ie placing of (
and )
) in your equations (returned tuple).我认为您只是在方程式(返回的元组)中遇到括号(即放置
(
和)
)的问题。
Below is full corrected working code:以下是完整更正的工作代码:
from scipy.optimize import fsolve
import math
from numpy import log
ka1 = 1.045
ka2 = 0.1759
ka3 = 0.3159
def equations(p):
yh2o, yco2, yh2, ych4, yco = p
return (
(yh2o + yco2 + yh2 + ych4 + yco - 1),
ka1 - (yh2o ** 2) / (yco * (yh2 ** 2)),
ka2 - (yco ** 2) / yco2,
ka3 - ych4 / (yh2 ** 2),
0.5 - (2.0 * yco2 + yco + yh2o) / (2 * yh2o + 2 * yh2 + 4 * ych4)
)
yh2o, yco2, yh2, ych4, yco = fsolve(equations, [0.2, 0.2, 0.2, 0.2, 0.2])
print(f"yh2o = {yh2o}, yco2 = {yco2}, yh2 = {yh2}, ych4 = {ych4}, yco = {yco}")
Output:输出:
yh2o = 0.17829101808889491, yco2 = 0.17513942402710805, yh2 = 0.4163023675952086, ych4 = 0.05474789024926531, yco = 0.1755193000395232
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.