Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Lab10_assignment_LavanyaBahri_100799787

April 5, 2021

0.1 Lab 10 - Assignment


Due date: 1 week from your lab session.
Hello Everyone!
In this lab, we will write an eco-system simulation for two species: predator and prey. The
prey is the primary food source for the predator. During each round of the simulation, the number
of prey will increase or decrease (due to predation, other deaths, and births), and the number of
predators will increase or decrease (due to births and deaths).

0.2 Task 1
[ ]: #Created on Monday 5th April 2021
#@author: Lavanya Bahri(100799787)
#Lab 09 - Jupyter Notebook

0.3 Lab09 - Assignment


Write a function called simulate that simulates the counts of two species: one predator and one
prey. The arguments of this function are:

Argument Description Sample value


initialPred The initial number of 50
predators
initialPrey The initial number of prey 1000
preyGrowth The rate at which births of 0.25
prey exceeds natural deaths
predationRate The rate of predation 0.01
predShrink The rate at which deaths of 0.05
predators exceeds births
without food
predFedBirthRate The rate of increase in 0.00002
predators in the presence of
food

Your function will collect the counts of each species in two lists: predatorCounts and preyCounts.
The function will have a loop, representing rounds of the simulation, which repeats until one of

1
the values (predator or prey) is zero. At each iteration, you will update the count, and append
these counts to the predatorCounts and preyCounts lists, of predators and prey according to the
following formulae:
preyi+1 = preyi × (1 + preyGrowth − predationRate × predatorsi )
predatorsi+1 = predatorsi × (1 − predShrink + predF edBrithRate × preyi )

0.4 Notes
Note 1: preyi+1 is merely a Mathematical notation to show the relationship with how the values
change from one round to the next. You cannot use this notation in Python.
Note 2: preyi+1 is the number of prey on the current timestep, and preyi is the number of prey
on the previous timestep. You should have a variable called prey for this purpose. Similarly,
predatorsi+1 is the number of predators on the current timestep, and predatorsi is the number of
predators on the previous timestep. You should have a variable called predators for this purpose.
Once the simulation ends, draw a plot comparing the number of predator and prey over time.

0.5 Sample Output


simulate(50, 1000, 0.25, 0.01, 0.05, 0.00002)
[6]: ##your code here
import matplotlib.pyplot as plt

def␣
,→Simulate(initialPred,initialPrey,preyGrowth,predationRate,predShrink,predFedBirthRate):

,→

preyCounts=[initialPrey]
predatorCounts=[initialPred]
i=0
while True:
Prey = preyCounts[i]*(1+preyGrowth-(predationRate*predatorCounts[i]))

,→Predators=predatorCounts[i]*(1-predShrink+(predFedBirthRate*preyCounts[i]))

if Prey>0 and Predators>0:


preyCounts.append(Prey)
predatorCounts.append(Predators)
i+=1
else:
break

plot1=plt.plot(range(len(preyCounts)),preyCounts, "c",label='Prey')
plot2=plt.
,→plot(range(len(predatorCounts)),predatorCounts,"r",label='Predator')

plt.xlabel('Time in years')
plt.ylabel('Number of Prey/Predators')
plt.axis([0,50,0,2000])

2
legend=plt.legend()
plt.title('Predator and Prey Trends')
plt.show()

Simulate(50, 1000, 0.25, 0.01, 0.05, 0.00002)

0.6 Submission
1. Remember to add the code below to the beginning of your assignemnet with today’s date and
your name and student ID
2. Follow the steps from the labs/how-to-submit.pdf file to submit your work.
Note. Instructions are updated to include both .ipynb and .pdf notebook versions.

You might also like