回路和分岔图

2024-09-28 03:19:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我在写一个脚本,画出一个有阻尼的单摆在一个小的直接力作用下的分岔图。在

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
epsi = 0.01
# Declare the model
f_dir = np.arange(0,1.3,0.01)
A_s = np.zeros(len(f_dir))

i = 0
for f in f_dir:
 def myModel(y, t):

    dy0 = y[1]
    dy1 = -epsi*y[1]-np.sin(y[0]) - f*np.cos((1.01)*t)*np.cos(y[0])
    return [dy0, dy1]
    time = np.arange(0.0, 2000,0.01)
    yinit = np.array([np.pi/2, 0])
    y = odeint(myModel, yinit, time)

    A_s.insert(i,np.abs(np.max(y[-600:-1,0])- np.min(y[-600:-1,0])))

 i += 1


plt.plot(f_dir,A_s,'*')
plt.xlabel(r'$f_s$')
plt.ylabel(r'$A_s$')
plt.hold
plt.show()

问题是我没有在A_s中插入任何内容,我不知道为什么,因为变量{}在循环的每一步都会增加。在


Tags: import脚本timeasdirnppltcos
2条回答

遵循代码有点困难,但这可能更接近于您想要的。您只需要定义一次模型,即使f是一个变量参数:您可以将这样的参数传递给args元组中的odeint,然后将它们交给model函数。在

还要注意NumPy数组没有insert方法。在

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
epsi = 0.01
# Declare the model
f_dir = np.arange(0,1.3,0.01)
A_s = np.zeros(len(f_dir))

def myModel(y, t, f):
    dy0 = y[1]
    dy1 = -epsi*y[1]-np.sin(y[0]) - f*np.cos((1.01)*t)*np.cos(y[0])
    return [dy0, dy1]

i = 0
for f in f_dir:
    time = np.arange(0.0, 2000,0.01)
    yinit = np.array([np.pi/2, 0])
    y = odeint(myModel, yinit, time, args=(f,))
    A_s[i] = np.abs(np.max(y[-600:-1,0])- np.min(y[-600:-1,0]))
    i += 1


plt.plot(f_dir,A_s,'*')
plt.xlabel(r'$f_s$')
plt.ylabel(r'$A_s$')
plt.hold
plt.show()

您定义了myModel函数,但实际上它并没有在任何地方被调用——它只是从函数本身内部引用的。在

相关问题 更多 >

    热门问题