Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用odeint求解一阶耦合常微分方程代码的维数误差_Python_Python 3.x_Odeint - Fatal编程技术网

Python 用odeint求解一阶耦合常微分方程代码的维数误差

Python 用odeint求解一阶耦合常微分方程代码的维数误差,python,python-3.x,odeint,Python,Python 3.x,Odeint,我为一阶ODE系统编写了这段代码,但显然python理解“y”是一个整数,而我的意思是它是一个列表。有人能看出错误吗?我根本不知道它在哪里 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

我为一阶ODE系统编写了这段代码,但显然python理解“y”是一个整数,而我的意思是它是一个列表。有人能看出错误吗?我根本不知道它在哪里

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.
运行时错误回溯(最近一次调用)
在里面
15 y0=1.013e6
16 X=np.linspace(0,0.95100)
--->17 y[m,P]=odeint(pfr_模型_deltaP,y0,X)
18
19 plt.绘图(X,m)
c:\users\idril\appdata\local\programs\python36\lib\site packages\scipy\integrate\odepack.py在odeint中(func、y0、t、args、Dfun、col_deriv、full_输出、ml、mu、rtol、atol、tcrit、h0、hmax、hmin、ixpr、mxstep、mxhnil、mxordn、mxwords、printmessg、tfirst)
242满输出,rtol,atol,tcrit,h0,hmax,hmin,
243 ixpr、mxstep、mxhnil、mxordn、mxords、,
-->244 int(bool(tfirst)))
245如果输出[-1]<0:
246警告\u msg=\u msgs[output[-1]]+“使用完整的\u output=1运行以获取定量信息。”
RuntimeError:func返回的数组必须是一维的,但得到的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 dim数组,但odeint接受一个函数,该函数只返回一个值,因此我只返回了一个变量,它工作了,但我不确定这是否是您要查找的确切输出因此,只需尝试从函数返回单个值即可。


希望这有帮助……

我在这里考虑的另一个选项是,初始值y0应该是长度为2的列表,它在函数内部解包。取决于要解决的原始ode是什么。