gpt4 book ai didi

python - 使用 odeint 求解一阶耦合 ODE 的代码中的维数错误

转载 作者:行者123 更新时间:2023-12-01 07:46:14 36 4
gpt4 key购买 nike

我已经为一阶 ODE 系统编写了这段代码,但显然当我将“y”表示为列表时,Python 将其理解为整数。有人能看到错误吗?我根本不知道它在哪里。

import matplotlib.pyplot as plt
from scipy.integrate import odeint
import numpy as np
from math import exp

def pfr_model_deltaP(P, X):
alpha = [0.019, 0.0075]
P0 = 1.013e6

dmdX = 5.40/(0.03591*exp((8.39 - 37.78/(4.5 + 5*X)))*((1-X)/(1+X))*(4.5/(4.5 + 5*X))*(P/P0))
dPdm = -(alpha[0]/2)*(X**2 + 1.90*X + 0.901)*(P0/P)

return [dmdX, dPdm]

y0 = 1.013e6
X = np.linspace(0, 0.95, 100)
y = odeint(pfr_model_deltaP, y0, X)

m = y[:, 0]
P = y[:, 1]

plt.plot(X, m)
plt.xlabel('Conversion')
plt.ylabel('Mass')
plt.show()

plt.plot(m, P)
plt.xlabel('Mass (kg)')
plt.ylabel('Pressure')
plt.show()

错误信息:

RuntimeError                              Traceback (most recent call last)
<ipython-input-6-3d4f22debf1d> in <module>
15 y0 = 1.013e6
16 X = np.linspace(0, 0.95, 100)
---> 17 y[m, P] = odeint(pfr_model_deltaP, y0, X)
18
19 plt.plot(X, m)

c:\users\idril\appdata\local\programs\python\python36\lib\site-packages\scipy\integrate\odepack.py in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg, tfirst)
242 full_output, rtol, atol, tcrit, h0, hmax, hmin,
243 ixpr, mxstep, mxhnil, mxordn, mxords,
--> 244 int(bool(tfirst)))
245 if output[-1] < 0:
246 warning_msg = _msgs[output[-1]] + " Run with full_output = 1 to get quantitative information."

RuntimeError: The array return by func must be one-dimensional, but got ndim=2.

最佳答案

试试这个:这只是您的函数应返回的示例

import matplotlib.pyplot as plt
from scipy.integrate import odeint
import numpy as np
from math import exp

def pfr_model_deltaP(P, X):
alpha = [0.019, 0.0075]
P0 = 1.013e6

dmdX = 5.40/(0.03591*exp((8.39 - 37.78/(4.5 + 5*X)))*((1-X)/(1+X))*(4.5/(4.5 + 5*X))*(P/P0))
dPdm = -(alpha[0]/2)*(X**2 + 1.90*X + 0.901)*(P0/P)

return dPdm

y0 = 1.013e6
X = np.linspace(0, 0.95, 100)
y = odeint(pfr_model_deltaP, y0, X)

m = y[:]
P = y[:]

plt.plot(X, m)
plt.xlabel('Conversion')
plt.ylabel('Mass')
plt.show()

plt.plot(m, P)
plt.xlabel('Mass (kg)')
plt.ylabel('Pressure')
plt.show()

此时,您返回 2 个值,即 2 个暗淡数组,但 odeint 需要一个仅返回单个值的函数,因此我仅返回您的 var 之一并且它有效,但我不确定这是否是您的确切输出正在找。 因此,只需尝试从函数返回单个值即可。

希望这有帮助...

关于python - 使用 odeint 求解一阶耦合 ODE 的代码中的维数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56448905/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com