我試圖計算和繪製的時間將溶液的振幅amp
運動的依賴t
微分方程(見rhs6
)作爲wd
的力系數f
的多個值的函數發現於f_array
。執行計算值數組 - 的Python
到目前爲止,我已經得到了代碼amp
針對wd
繪製的單個值f
。其結果是一個共振峯:
針對wd
用於f
一個值(其產生上述圖像)繪製amp
的代碼如下。
from pylab import *
from scipy.integrate import odeint
from numpy import *
import math
#Parameters.
k = 2.0
m = 1.0
w0 = (k/m)**(1/2)
alpha = 0.2
l = alpha/(2*m)
f = 1.0
wd = w0 + 0.025
beta = 0.2
t_fixed = 2.0
#Arrays.
t = linspace(0.0, 400.0, 400.0)
wd_array = linspace(w0-1.0, w0+1, 400.0)
f_array = linspace(10.0, 100.0, 3.0)
#Time step.
init_x = 0.0
init_v = 0.0
dx = 15.0
dv = 0.0
init_cond = [init_x,init_v]
init_cond2 = [init_x + dx,init_v + dv]
def rhs6(c,t,wd):
c0dot = c[1]
c1dot = -2*l*c[1] - w0*w0*c[0] + (f/m)*cos((wd)*t)
return [c0dot, c1dot]
amp_array=[]
for wd in wd_array:
res = odeint(rhs6, init_cond, t, args=(wd,))
amp = max(res[:,0])
amp_array.append(amp)
plot(wd_array, amp_array)
xlabel('Driving frequency, wd')
ylabel('Ampltiude, amp')
show()
現在我希望能夠找到amp
對wd
爲f
多個值。我試圖通過在f_array
上做出for
循環聲明來做到這一點。然而,我的方法不起作用,我得到的錯誤:
setting an element with a sequence
。
因爲這是一個很好的嘗試,下面是我的。
from pylab import *
from scipy.integrate import odeint
from numpy import *
import math
#Parameters.
k = 2.0
m = 1.0
w0 = (k/m)**(1/2)
alpha = 0.2
l = alpha/(2*m)
f = 1.0
wd = w0 + 0.025
beta = 0.2
t_fixed = 2.0
#Arrays.
t = linspace(0.0, 200.0, 200.0)
wd_array = linspace(w0-1.0, w0+1, 200.0)
f_array = linspace(10.0, 200.0, 3.0)
#Time step.
init_x = 0.0
init_v = 0.0
dx = 15.0
dv = 0.0
init_cond = [init_x,init_v]
init_cond2 = [init_x + dx,init_v + dv]
def rhs6(c,t,wd,f):
c0dot = c[1]
c1dot = -2*l*c[1] - w0*w0*c[0] + (f/m)*cos((wd)*t)
return [c0dot, c1dot]
full_array = zeros(len(f_array))
for index,force in enumerate(f_array):
amp_list = []
for wd in wd_array:
res = odeint(rhs6, init_cond, t, args=(wd,force))
amp = max(res[:,0])
amp_list.append(amp)
print(res)
amp_array = array(amp_list)
full_array[index] = amp_array
for f in full_array:
plot(wd, amp)
show()
任何想法?
你可以附加整個回溯,而不僅僅是錯誤本身? – Yoel 2014-09-24 09:29:38