Skip to content

Instantly share code, notes, and snippets.

@kimitoboku
Last active May 4, 2019 01:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimitoboku/ac4ddd68417d7641135c to your computer and use it in GitHub Desktop.
Save kimitoboku/ac4ddd68417d7641135c to your computer and use it in GitHub Desktop.
python
import matplotlib.pyplot as plt
dt = 0.01
x = 4
r = 2
k = 2
def dx(t,x):
return r*x*(1-x/k)
time = []
xvar = []
for i in range(500):
t = i*dt
time.append(t)
xvar.append(x)
k1 = dx(t,x)
k2 = dx(t+dt,x + k1*dt)
x += (k1+k2)*dt / 2
x = 1
x2var = []
for i in range(500):
t = i*dt
x2var.append(x)
k1 = dx(t,x)
k2 = dx(t+dt,x + k1*dt)
x += (k1+k2)*dt / 2
x = 0.5
x3var = []
for i in range(500):
t = i*dt
x3var.append(x)
k1 = dx(t,x)
k2 = dx(t+dt,x + k1*dt)
x += (k1+k2)*dt / 2
plt.plot(time,xvar,"-b")
plt.plot(time,x2var,"-y")
plt.plot(time,x3var,"-r")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment