Sie sind auf Seite 1von 3

12/6/2016 Untitled

In[1]: def lk(prey,predator):

File "<ipython-input-1-95c9755a1f1e>", line 5

import numpy as np
IndentationError: expected an indented block

In[17]: import numpy as np

from matplotlib import pyplot as plt

def eulers(coef, prey, pred, step):

#This function takes in a tuple containing 4 values representing
#the coefficients, then the current populations and step size.
a, b, c, d = coef[0], coef[1], coef[2], coef[3]
next_x = prey + step * (a*prey - b*prey*pred)
next_y = pred + step * (c*pred*prey - d*pred)
#The output is a tuple, with the new prey population
#followed by the new predator population.
return next_x, next_y

In[124]: ### Here, I originate the inputs

prey_pop = int(input('What is the initial prey population?'))
pred_pop = int(input('What is the initial predator population?'))
a, b, c, d = 0.1, 0.1, 0.1, 0.1
coefficients = a, b, c, d
time_elapse = input('What is the amount of time to be modeled?')
time_step = input('What is the desired step size?')
#Here, I calculate the number of steps we will do,
#and build a list containing the values for
#"current time" that I will move through.
num_steps = int(time_elapse/time_step)
time_values = []
for i in range(num_steps + 1):
#This list will be used to create the graph.

What is the initial prey population?10

What is the initial predator population?10
What is the amount of time to be modeled?10
What is the desired step size?2

http://localhost:8888/nbconvert/html/Untitled.ipynb?download=false 1/3
12/6/2016 Untitled

In[125]: #Next, I will originate, then iteratively add to

#the lists containing the values for
#predator and prey populations over time.

prey_values = [prey_pop]
pred_values = [pred_pop]
for i in range (num_steps):
#Here, I will do Euler's method for the current time.
step = eulers(coefficients, prey_pop, pred_pop, time_step)
#Then, I will take the output, and add the relevant
#populations to each population's list.
prey_pop = step[0]
pred_pop = step[1]
#Next, I will adjust any negative outcomes to 0,
#since the population must be a positive number.
if pred_pop < 0 :
pred_pop = 0
if prey_pop < 0 :
prey_pop = 0
#Then, I will add this current value to the list containing
#values from every step. These will be used to create the graph.
print 'The final prey population is', str(int(round(prey_values[num_step
print num_steps, pred_values, prey_values
print 'The final predator population is', str(int(round(pred_values[num_

#Then, these will be put into a line graph.

plt.plot(time_values, prey_values, time_values, pred_values)
plt.xlabel('Time Elapsed')
plt.ylabel('Population Size')
plt.legend(labels = ['Prey', 'Predators'], bbox_to_anchor=(1.38, 0.6))

http://localhost:8888/nbconvert/html/Untitled.ipynb?download=false 2/3
12/6/2016 Untitled

The final prey population is 0

5 [10, 28.0, 22.4, 17.919999999999998, 14.335999999999999, 11.468799999
999998] [10, 0, 0.0, 0.0, 0.0, 0.0]
The final predator population is 11

http://localhost:8888/nbconvert/html/Untitled.ipynb?download=false 3/3