我正在编写一个脚本,该脚本以较小的直接力绘制了阻尼摆的分叉图。

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中插入任何内容,我也不知道为什么,因为在循环的每个步骤中变量i都会增加。

最佳答案

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

还要注意,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()

关于python - 回路和 fork 图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34969490/

10-10 10:29