0
我對編程相當陌生。我在課堂上選擇了一些R和Matlab,但我試圖提高我對Python的熟悉程度。我試圖編寫一個模型,該數學模型將給出多個初始條件下的微分方程組的數值近似。我目前擁有的代碼如下:Python多個初始條件
import matplotlib.pyplot as plt
# parameters
sigma=10
gamma=1
alpha=50
delta=2.1
# initial conditions
B=[100,200]
P=[1,10]
t=0
# counter
dt=0.00005
stop=1
# vector
Bstor=[]
Pstor=[]
tstor=[]
# let's go
for i in B:
for j in P:
while t<=stop:
Bstor.append(i)
Pstor.append(j)
tstor.append(t)
i=i+(sigma-gamma*i-alpha*i*j)*dt
j=j+(alpha*i*j-delta*j)*dt
t=t+dt
plt.figure(1)
plt.plot(tstor,Bstor,'-b')
plt.plot(tstor,Pstor,'-r')
plt.xlabel('Time')
plt.ylabel('Number')
plt.show()
這將做數學題相當不錯,但只有P和B的第一個值我如何爲每個可能的組合(P[0],B[0]; P[1],B[0]; P[1],B[0]; P[1],B[1]
)運行方程?
謝謝!
你是什麼意思的第一個值?嵌套的'for'將進行所需的排列。 –