如何处理二阶 ODE? Python:“类型错误:无法解压不可迭代的浮点对象”

问题描述 投票:0回答:1

我目前正在尝试通过将二阶常微分方程 (ODE) 分解为两个一阶 ODE 来求解。

Problem Description Image

该方程涉及变量 x、v 和常数 c1、c2、c3、c4。我正在使用 scipy.integrate 库中的 odeint 函数来数值求解 ODE。但是,我遇到以下错误:

我已经将它分解为两个一阶 ODE。 但是当我尝试解决它时出现以下错误:

类型错误:无法解压不可迭代的浮点对象

c2 = 2 
c3 = 3 
c4 = 4

def dSdx(S, x):
    x, v = S
    return [v,
           k - c1*v-c2*x - c3*np.sin(m.radians(x)) + c4* np.cos(m.radians(x)) ]
x_0 = 0
v_0 = 0
S_0 = (x_0, v_0)

t = np.linspace(0, 0.3, 100)
sol = odeint(dSdx, y0=S_0, t=t, tfirst=True)

我得到了这个我无法理解的错误:

----> 2     x, v = S
      3     return [v,
      4            k - c1*v-c2*x - c3*np.sin(m.radians(x)) + c4* np.cos(m.radians(x)) ]

TypeError: cannot unpack non-iterable float object```
python ode odeint
1个回答
0
投票

问题在于你对 odeint of tfirst=True 的论证。

如果您包含此(非默认)值,那么您假设 dSdx 具有签名

dSdx( t, S )
- 即“t 优先” - 而它实际上具有签名
dSdx( S, t )
(或者应该具有,无论如何:您似乎有将第二个参数命名为 x)。

省略

tfirst=True
即可。

© www.soinside.com 2019 - 2024. All rights reserved.