为参数数组求解ODE(Python)

为参数数组求解ODE(Python),python,scipy,Python,Scipy,*我知道这个问题很简单,但我想知道在Python中设置这样一个for循环的最佳方法 我已经编写了一个程序来计算和绘制一个二阶微分方程的解(下面给出了这个代码) 我想知道对f参数数组重复此计算的最佳方法(因此f_数组)。也就是说,图中显示了作为t函数的解决方案的20组数据,每个数据的f值不同 为任何想法干杯 from pylab import * from scipy.integrate import odeint #Arrays. tmax = 100 t = linspace(0, tmax

*我知道这个问题很简单,但我想知道在Python中设置这样一个for循环的最佳方法

我已经编写了一个程序来计算和绘制一个二阶微分方程的解(下面给出了这个代码)

我想知道对f参数数组重复此计算的最佳方法(因此
f_数组
)。也就是说,图中显示了作为
t
函数的解决方案的
20组数据,每个数据的
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
值数组重复这个图

这里有一种方法:

修改
rhs
以接受第三个参数,即参数
f
。应开始定义
rhs

def rhs(c, t, f):
    ...
使用
for
循环在
f_数组
上迭代。在循环中,使用
args
参数调用
odeint
,以便
odeint
f
的值作为
rhs
的第三个参数。将每次调用
odeint
的结果保存在列表中。基本上,替换

ces = odeint(rhs, init_cond, t)    

对于
f_数组
中的
f
的每个值,现在在
解决方案
列表中有一个解决方案

要绘制这些,可以将
plot
调用放入另一个
for
循环:

for ces in solutions:
    plot(t, ces[:, 0], 'b-')

好建议,谢谢。我已经掌握了如何将每个解决方案从用于打印的阵列列表中分离出来?您可以将对
plot
的调用放在
solutions
的循环中。请参阅更新的答案。
for ces in solutions:
    plot(t, ces[:, 0], 'b-')