scipy 使用solve_ivp“至少需要一个数组来连接”

oyt4ldly  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(173)
xa_max = (0.7 * k1 + (0.3-gamma) * k2) / (k1 + k2) #(0.5343)
npt = 50
xau = np.linspace(0,xa_max,npt)
V = np.linspace(0.,0.,npt)
Xa = np.zeros([npt,npt])

def batch(xa , t):
    z = 1 / (k1 * (1 - xa) - k2 * (gamma + xa))
    return z

def volumemin(V,xau):
    res = Pb_min - (c0[0] * xau ) / (t_reaz/ V + tc + ts)
    return res

for j in range(npt):
    Xa[j,:] = np.linspace(0, xau[j], npt)
    t0 = np.array([0.])
    xaeval = Xa[j,:] 
    SOL = solve_ivp(batch, (0, Xa[j,npt-1]), t0 , t_eval=xaeval)
    t = SOL.y
    t_reaz = max(t) 
    V0 = 3. #m^3
    V[j] = fsolve(volumemin, V0, args = (xau [j])) #m^3

Vmin = min(V)

当我运行它时,出现以下错误:

File "C:\Users\Lenovo\Desktop\ex2_esame.py", line 36, in <module>
    SOL = solve_ivp(batch, (0, Xa[j,npt-1]), t0 , t_eval=xaeval)

  File "C:\Users\Lenovo\anaconda3\lib\site-packages\scipy\integrate\_ivp\ivp.py", line 650, in solve_ivp
    ts = np.hstack(ts)

  File "<__array_function__ internals>", line 5, in hstack

  File "C:\Users\Lenovo\anaconda3\lib\site-packages\numpy\core\shape_base.py", line 346, in hstack
    return _nx.concatenate(arrs, 1)

  File "<__array_function__ internals>", line 5, in concatenate

ValueError: need at least one array to concatenate

我不明白这是什么类型的错误;我认为它与solve_ivp中的y0有关,但我无法修复它。

rdlzhqv9

rdlzhqv91#

solve_ivp[source](文档中的链接),hstack(ts)位于

ts = np.hstack(ts)
    ys = np.hstack(ys)

您错误消息可能由以下原因产生:

In [1]: np.hstack([])
Traceback (most recent call last):
  Input In [1] in <cell line: 1>
    np.hstack([])
  File <__array_function__ internals>:180 in hstack
  File /usr/local/lib/python3.8/dist-packages/numpy/core/shape_base.py:345 in hstack
    return _nx.concatenate(arrs, 1)
  File <__array_function__ internals>:180 in concatenate
ValueError: need at least one array to concatenate

这里ts是求解应该返回的时间点。
我不会试图进一步挖掘,但它看起来像ts不知何故派生自t_eval(您提供的)和t_span-它们来自您的Xa
您需要仔细地将您的参数与solve_ivp文档进行比较,并从solve_ivp代码中获得补充帮助。

相关问题