2016-01-23 63 views
1

我正在写一个脚本,用小的直接强制绘制阻尼摆的分岔图。循环和分叉图

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在循环的每一步提高。

回答

3

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

另请注意,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() 
+0

好吧,我明白你所做的,现在它工作正常!非常感谢! – amcalvo

0

您定义的基于myModel功能,但它实际上没有任何地方被称为 - 它只是在函数内部引用。

+0

'myModel'函数对象被传递给'odeint',它调用它。 – xnx

+0

在你提供的解决方案中,这是真的,但在发布的问题中,odeint也不会被调用(因为它发生在myModel内部),除非我真的错过了这里的东西 –

+0

这是一个好点 - 我错过了。 – xnx