*我知道这个问题很简单,但是我想知道在Python中设置这样的for循环的最佳方法.
我已经编写了一个程序来计算和绘制二阶微分方程的解(此代码在下面给出).
我想知道针对f参数数组(因此为f_array)重复此计算的最佳方法.即因此,该图显示了20个数据集,这些数据涉及作为t的函数的解,每个解具有不同的f值.
为任何想法加油.
from pylab import *
from scipy.integrate import odeint
#Arrays.
tmax = 100
t = linspace(0, tmax, 4000)
fmax = 100
f_array = linspace(0.0, fmax, 20)
#Parameters
l = 2.5
w0 = 0.75
f = 5.0
gamma = w0 + 0.05
m = 1.0
alpha = 0.15
beta = 2.5
def rhs(c,t):
c0dot = c[1]
c1dot = -2*l*c[1] - w0*w0*c[0] + (f/m)*cos((gamma)*t)-alpha*c[0] - beta*c[0]*c[0]*c[0]
return [c0dot, c1dot]
init_x = 15.0
init_v = 0.0
init_cond = [init_x,init_v]
ces = odeint(rhs, init_cond, t)
s_no = 1
subplot(s_no,1,1)
xlabel("Time, t")
ylabel("Position, x")
grid('on')
plot(t,ces[:,0],'-b')
title("Position x vs. time t for a Duffing oscillator.")
show()
这是一个曲线图,显示了针对t值数组的f的单个值的此方程的解.我想要一种快速的方法来对f值数组重复此图.