以下代码的目标是将函数“ddW”插入 odeint 以找到给定 X 的 W(稍后)。我正在使用打印功能来确保功能运行)。
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import odeint
import pint
u = pint.UnitRegistry()
R = 8.31446261815324 * u.J/(u.mol*u.K)
Rgas = R.to (u.atm*u.L/(u.mol*u.K))
P0 = 10 * u.atm
T = 400 * u.K
v0 = 2 * u.L/u.min
α = 0.02 * u.kg**-1
k = 1.4 * u.L**2/(u.mol*u.kg*u.min)
FA0 = 0.5*P0*v0/(Rgas*T) #Assuming I.G.
def ddW(param,w): #param: [X,P]
X = param[0]
P = u.Quantity(param[1]).magnitude * u.atm
W = u.Quantity(w).magnitude * u.kg
d = np.zeros(2)
d[0] = k*FA0/(v0**2*P0) * P*W*(1-X) #dX/dW
d[1] = -α/2 *P0**2 * (1-X)/P #dP/dW
return d
param0 = [0,P0]
Wrange = np.linspace(0,100) *u.kg
#PBR = odeint(ddW,param0,Wrange)
#plt.plot(Wrange,PBR)
print(ddW(param0,0))
Traceback (most recent call last):
File "<ipython-input-1-83022bc3b5da>", line 1, in <module>
runfile(REDACTED, wdir=REDACTED)
File "C:\Users\Spencer\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\Spencer\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "REDACTED", line 43, in <module>
print(ddW(param0,0))
File "REDACTED", line 33, in ddW
d[1] = -α/2 *P0**2 * (1-X)/P #dP/dW
ValueError: setting an array element with a sequence.
在第 29 行定义 W 之前,我之前对现在的第 32 行(定义 d[0])有相同的错误消息。困扰我的是 d[1] 没有任何输入(据我所知) 的尺寸 > 1,所以它应该适合。
波斯汪
料青山看我应如是
相关分类