Question

In: Computer Science

trying to make a code that simulates a predator prey relationship in python, however for some...

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

Solutions

Expert Solution

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


Related Solutions

The predator-prey relationship between gray wolves and moose on Isle Royal in Lake Michigan has been...
The predator-prey relationship between gray wolves and moose on Isle Royal in Lake Michigan has been studied for decades. Although both populations fluctuate, the moose have never been driven to extinction. In this case, as well as many others, wildlife ecologists and managers often refer to the victims of predation as a “doomed surplus.” Use the concepts of reproductive value and survivorship to discuss what this means.
Working with Python. I am trying to make my code go through each subject in my...
Working with Python. I am trying to make my code go through each subject in my sample size and request something about it. For example, I have my code request from the user to input a sample size N. If I said sample size was 5 for example, I want the code to ask the user the following: "Enter age of subject 1" "Enter age of subject 2" "Enter age of subject 3" "Enter age of subject 4" "Enter age...
In python please write the following code the problem. Write a function called play_round that simulates...
In python please write the following code the problem. Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. The function has two parameters: the name of player 1 and the name of player 2. It returns a string with format '<winning player name> wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'.
I am trying to make a robot move (theoretically), some of my code does not work...
I am trying to make a robot move (theoretically), some of my code does not work in python. I am getting some errors. How can I fix it? This is my code: #!/usr/bin/env python.3 import time import sys import random #Provide a Menu for the User def main(): print("To control LED press 1") print("To drive press 2") print("To change motor speed press 3") print("To control the Servo press 4") while True: option = int(input("Enter the numbers 1, 2, 3, or...
In python make a simple code. You are writing a code for a program that converts...
In python make a simple code. You are writing a code for a program that converts Celsius and Fahrenheit degrees together. The program should first ask the user in which unit they are entering the temperature degree (c or C for Celcius, and f or F for Fahrenheit). Then it should ask for the temperature and call the proper function to do the conversion and display the result in another unit. It should display the result with a proper message....
This is my code for python. I am trying to do the fourth command in the...
This is my code for python. I am trying to do the fourth command in the menu which is to add an employee to directory with a new phone number. but I keep getting error saying , "TypeError: unsupported operand type(s) for +: 'dict' and 'dict". Below is my code. What am I doing wrong? from Lab_6.Employee import * def file_to_directory(File): myDirectory={}       with open(File,'r') as f: data=f.read().split('\n')    x=(len(data)) myDirectory = {} for line in range(0,199):      ...
Trying to score a hand of blackjack in this python code but my loop is consistently...
Trying to score a hand of blackjack in this python code but my loop is consistently outputting (13,1) which makes me think that something is wrong with my loop. Could someone help me with this code?: import random cards = [random.randint(1,13) for i in range(0,2)] #initialize hand with two random cards def get_card(): #this function will randomly return a card with the value between 1 and 13 return random.randint(1,13) def score(cards): stand_on_value = 0 soft_ace = 0 for i in...
this is my code in python I am trying to open a file and then print...
this is my code in python I am trying to open a file and then print the contents on my console but when i run it nothing prints in the console def file_to_dictionary(rosterFile): myDictionary={}    with open(rosterFile,'r') as f: for line in f: myDictionary.append(line.strip()) print(myDictionary)             return myDictionary    file_to_dictionary((f"../data/Roster.txt"))      
Invalid entry code in python my code is pasted below. The last elif statement, I'm trying...
Invalid entry code in python my code is pasted below. The last elif statement, I'm trying to get the program to print "invalid entry" if the entry for user_input is invalid. The first user prompt should only allow for numbers 1-10 and "exit" and "quit" import math user_prompt = """Enter number of operation that you want to execute <type exit or quit to end program>: 1 sin(x) 2 cos(x) 3 tan(x) 4 asin(x) 5 acos(x) 6 atan(x) 7 ln(x) 8...
I am trying to make a new code that uses functions to make it. My functions...
I am trying to make a new code that uses functions to make it. My functions are below the code. <?php */ $input; $TenBills = 1000; $FiveBills = 500; $OneBills = 100; $Quarters = 25; $Dimes = 10; $Nickels = 5; $Pennies = 1; $YourChange = 0; $input = readline("Hello, please enter your amount of cents:\n"); if(ctype_digit($input)) { $dollars =(int)($input/100); $cents = $input%100;    $input >= $TenBills; $YourChange = (int)($input/$TenBills); $input -= $TenBills * $YourChange; print "Change for $dollars dollars...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT