全部版块 我的主页
论坛 经济学论坛 三区 博弈论
5899 4
2020-12-29
import random
import matplotlib.pyplot as plt
# define system in terms of separated differential equations
def f(x,y):
    return x*(1-x)*(5*y-2)
def g(x,y):
    return y*(1-y)*(4*x-2)
# initialize lists containing values

def sys(iv1, iv2, dt, time):
    x = []
    y = []
    # initial values:
    x.append(iv1)
    y.append(iv2)
    #z.append(iv3)
    # compute and fill lists
    for i in range(time):
        x.append(x + (f(x,y)) * dt)
        y.append(y + (g(x,y)) * dt)
        #z.append(z + (h(x,y,z)) * dt)
    return (x, y)


print("\n")
D=[]
for i in range(500):
    a=random.random()
    b=random.random()
    d=sys(a,b,0.01,1000)
    D.append(d)


"""画图"""
#plot
fig = plt.figure(figsize=(17,15))
fig.subplots_adjust(wspace = 0.5, hspace = 0.3)
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)

for n,m in D:
    ax1.plot(n, 'r-')
    ax1.plot(m, 'b-')
    ax2.plot(n, m, color="blue")
#ax1.plot(z, 'g-', label='prey')
ax1.set_title("Dynamic in time")
ax1.set_xlabel("time")
ax1.grid()
ax1.legend(loc='best')


ax2.set_xlabel("x")
ax2.set_ylabel("y")  
ax2.set_title("Phase space")
ax2.grid()

plt.show()


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2021-3-22 17:11:00
请问自己需要修改哪里呢?
老是出现Traceback (most recent call last):
  File "C:\Users\28102\Desktop\Python\xiangweitu.py", line 30, in <module>
    d=sys(a,b,0.01,1000)
  File "C:\Users\28102\Desktop\Python\xiangweitu.py", line 19, in sys
    x.append(x+(f(x,y))*dt)
  File "C:\Users\28102\Desktop\Python\xiangweitu.py", line 5, in f
    return x*(1-x)*(5*y-2)
TypeError: unsupported operand type(s) for -: 'int' and 'list'
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2021-3-26 10:04:20
我也想知道怎么解决这个问题???
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2021-4-9 10:20:59
把append里的x改成xi,y改成yi试试?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2023-12-7 18:48:15
     # compute and fill lists
    for i in range(time):
        x.append(x[-1] + f(x[-1],y[-1]) * dt)
        y.append(y[-1] + g(x[-1],y[-1]) * dt)

这两行要改,我的改动如上。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群