gpt4 book ai didi

python - 欧拉函数没有给出结果

转载 作者:太空宇宙 更新时间:2023-11-04 10:24:04 26 4
gpt4 key购买 nike

我们编写了欧拉函数来研究人口变化。

他使用while 循环将我们的值保持在生物范围内(没有出生等)。当我们运行脚本时,绘图轴的范围从 -0.6 到 +0.6,实际上没有绘制任何图形。

有人知道代码有什么问题吗?

t=np.zeros((n+1))

for i in range (n):
t[i+1]=t[i]+dt

s = np.zeros((n+1))
z = np.zeros((n+1))
r = np.zeros((n+1))
s[0] = N-1
z[0] =1
i=0


while s[i]>0 and s[i] <N and z[i] < N and z[i] > 0 and r[i] >0 and r[i] <N and i<n:
s[i+1] = s[i]+dt*(-b*s[i]*z[i])
z[i+1] = z[i]+dt*(b*s[i]*z[i]-a*z[i])
r[i+1] = r[i]+dt*(a*z[i])
i=i+1

self.trace(t,s,r,z,i)

def trace(self,t, s, r, z, i):

plt.plot(t[:i],s[:i],'b')
plt.plot(t[:i],z[:i],'r')
plt.plot(t[:i],r[:i],'y')
plt.show()

最佳答案

我看到 r 初始化为:

r = np.zeros((n+1))

然后在 while 循环条件中你需要 r[i] >0

由于 i=0r[i]=0 且条件将为假,因此循环不会执行。

也许您缺少 r[0] = ... 的初始化

关于python - 欧拉函数没有给出结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30533636/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com