我在写一个脚本,画出一个有阻尼的单摆在一个小的直接力作用下的分岔图。在
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
中插入任何内容,我不知道为什么,因为变量{
遵循代码有点困难,但这可能更接近于您想要的。您只需要定义一次模型,即使
f
是一个变量参数:您可以将这样的参数传递给args
元组中的odeint
,然后将它们交给model函数。在还要注意NumPy数组没有
insert
方法。在您定义了myModel函数,但实际上它并没有在任何地方被调用——它只是从函数本身内部引用的。在
相关问题 更多 >
编程相关推荐