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()