In: Computer Science
trying to make a code that simulates a predator prey relationship in python, however for some reason my code will not show the plot, i cant find any issues with syntax or anything else, please help.
import matplotlib.pyplot as plt
predatorcount = []
preycount = []
def simulate(initialpred, initialprey, preygrowth, predationrate, predshrink, predbirthrate):
predatorcount.append(initialpred)
preycount.appened(initialprey)
i=0;
prey = 1
predator = 1
while True:
prey =
preycount[i]*(1+(preygrowth-predationrate*predatorcount[i]))
predator =
predatorcount[i]*(1-(predshrink+predbirthrate*preycount[i]))
if prey <= 0 or predator <=0:
break;
else:
preycount.append(prey)
predatorcount.append(predator)
i = i+1
simulate(50, 1000, 0.25, 0.01, 0.05, 0.00002)
print(preycount[35])
plt.plot(range(35), predatorcount[35])
plt.plot(range(35), preycount[35])
plt.show()
UPDATED CODE
import matplotlib.pyplot as plt
import numpy as np
predatorcount = []
preycount = []
def simulate(initialpred, initialprey, preygrowth,
predationrate, predshrink, predbirthrate):
predatorcount.append(initialpred)
preycount.append(initialprey)
i=0
prey = 1
predator = 1
while True:
prey =
preycount[i]*(1+(preygrowth-predationrate*predatorcount[i]))
predator =
predatorcount[i]*(1-(predshrink+predbirthrate*preycount[i]))
if prey <= 0 or predator <=0:
break
else:
preycount.append(prey)
predatorcount.append(predator)
i = i+1
simulate(50, 1000, 0.25, 0.01, 0.05, 0.00002)
print(preycount[35])
print(predatorcount[35])
plt.plot(35,predatorcount[35],'g^')
plt.plot(35,preycount[35],'ro')
plt.show()
OUTPUT
Note: In plot() you should give list values not range