首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Python模拟两个耦合的动力系统

用Python模拟两个耦合的动力系统
EN

Stack Overflow用户
提问于 2019-09-03 22:02:38
回答 1查看 148关注 0票数 0

目标是绘制两个耦合的完全相同的动力系统。

我们有:

X= x0、x1、x2

U= u0、u1、u2

Xdot = f(X) + alpha*(U-X)

Udot = f(U) + alpha*(X-U)

因此,我希望将这个大系统的解绘制在一组轴上(例如,在xyz中),并最终改变耦合强度来研究其行为。

代码语言:javascript
复制
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from mpl_toolkits.mplot3d import Axes3D

def couple(s,t,a=0.2,beta=0.2,gamma=5.7,alpha=0.03):
    [x,u] = s
    [u0,u1,u2] = u
    [x0,x1,x2] = x
    xdot = np.zeros(3)
    xdot[0] = -x1-x2
    xdot[1] = x0+a*x1
    xdot[2] = beta + x2*(x0-gamma)
    udot = np.zeros(3)
    udot[0] = -u1-u2
    udot[1] = u0+a*u1
    udot[2] = beta + u2*(u0-gamma)
    sdot = np.zeros(2)
    sdot[0] = xdot + alpha*(u-x)
    sdot[1] = udot + alpha*(x-u)
    return sdot

s_init = [0.1,0.1]

t_init=0; t_final = 300; t_step = 0.01
tpoints = np.arange(t_init,t_final,t_step)

a=0.2; beta=0.2; gamma=5.7; alpha=0.03

y = odeint(couple, s_init, tpoints,args=(a,beta,gamma,alpha), hmax = 0.01)

我认为s_init有问题,因为它应该是两个初始条件向量,但当我尝试时,我得到了"odeint: y0应该是一维的“。另一方面,当我尝试将s_init作为一个6向量时,我得到了“太多的值要解包(应该是两个)”。使用当前的设置,我得到了错误

代码语言:javascript
复制
  File "C:/Users/Python Scripts/dynsys2019work.py", line 88, in couple
    [u0,u1,u2] = u

TypeError: cannot unpack non-iterable numpy.float64 object

干杯

*编辑:请注意,这基本上是我第一次尝试这种事情,我很高兴收到更多的文档和参考资料。

EN

回答 1

Stack Overflow用户

发布于 2019-09-04 01:52:19

ode定义接收并返回scipy odeint中的1D向量,我认为您可能会感到困惑,因为您实际上有1个系统的ode,其中有6个变量。你刚刚在心理上把它分成了两个独立的ODEs,它们是耦合的。

你可以这样做:

代码语言:javascript
复制
import matplotlib.pyplot as plt
from scipy.integrate import odeint
import numpy as np

def couple(s,t,a=0.2,beta=0.2,gamma=5.7,alpha=0.03):

    x0, x1, x2, u0, u1, u2 = s
    xdot = np.zeros(3)
    xdot[0] = -x1-x2
    xdot[1] = x0+a*x1
    xdot[2] = beta + x2*(x0-gamma)
    udot = np.zeros(3)
    udot[0] = -u1-u2
    udot[1] = u0+a*u1
    udot[2] = beta + u2*(u0-gamma)
    return np.ravel([xdot, udot])

s_init = [0.1,0.1, 0.1, 0.1, 0.1, 0.1]
t_init=0; t_final = 300; t_step = 0.01
tpoints = np.arange(t_init,t_final,t_step)
a=0.2; beta=0.2; gamma=5.7; alpha=0.03
y = odeint(couple, s_init, tpoints,args=(a,beta,gamma,alpha), hmax = 0.01)
plt.plot(tpoints,y[:,0])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57773349

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档