Starting from:

$29

ECE-203 Laboratory Experiment Week 4

ECE-203 Programming for Engineers
Laboratory Experiment Week 4
Name:
In Lab Assignments (Due end of this lab session)
(10 Points) In a predator-prey simulation, you compute the populations of predators and prey
using the following equations:
preyt+1 = preyt × (1 + A − B × predt)
predt+1 = predt × (1 − C + D × preyt)
Here, A is the rate at which prey birth exceeds natural death, B is the rate of predation, C is the
rate at which predator deaths exceed births without food, and D represents predator increase in the
presence of food. The units of time t are expressed in years.
Write a program that prompts the user for these rates, the initial population sizes, and the number of
years. Then print the populations for the given number of years after your simulation has completed.
If the user does not enter a value and just presses the enter key at a prompt, use the following default
values: A = 0.1, B = 0.01, C = 0.01, D = 0.00009, initial prey = 1000, initial predators = 20, and
years to simulate = 30.
(Hint: This can be accomplished in about 15 lines of code.)
How many predators and prey are there after 30 years have passed?
How many years must pass before all the predators die?
Show your working code to the TA.
TA Initials
1
(10 Points) The Buffon Needle Experiment was devised by Comte Georges-Louis Leclerc de Buffon
(1707 – 1788), a French naturalist. In this experiment a 1 inch long needle is dropped onto paper
that is ruled with lines 2 inches apart. If the needle drops onto a line, we count it as a hit. Buffon
discovered the quotient of attempts/hits to be a very significant quantity. In this exercise, you will
simulate this experiment to make the same discovery.
To simulate the Buffon needle experiment, you will need to generate two random numbers: one
random number to describe where the tail of the needle landed on the y-axis and another to describe
the angle of the needle with respect to the x-axis. A random floating point number between 0 and
1.0 can be generated using the function random(), which you need to import from the Python
module also named random. So, at the top of your python program you will need the following code:
from random import random
Which says to import the function random() from the random module. You could then, for example,
generate a random number between 0 and 100 with the following code:
from random import random
some_float_between_0_and_100 = 100.0*random()
print some_float_between_0_and_100
For this experiment, you may generate the coordinate of where the tail of the needle landed ytail
as a number between 0 and 2.0. Likewise, the angle θ the needle makes with respect to the x-axis
will randomly be something between 0 and π radians. The head of the needle yhead may then be
computed as:
yhead = ytail + sin(θ)
If the head of the needle yhead is greater than 2.0, you count that simulated attempt as a hit.
Likewise, if yhead is less than 2.0, do not count that attempt as a hit.
θ
0
y
tail
y
head
2
The trigonometric function sin() is available from the math module, as is the numerical constant
π. You can import these for use in your program by adding the following statement to the top of
your Python program:
from math import pi, sin
# Some simple demonstration
print pi
print sin(pi/2.0)
2
Your program should prompt the user for the number of attempts to perform (i.e. how many times
we will simulate dropping the needle) and print the ratio attempts/hits as a floating point number
after all the attempts have completed.
(Hint: This can be achieved in about 20 lines of code.)
What is the ratio for 10 attempts?
What is the ratio for 100 attempts?
What is the ratio for 1000 attempts?
What is the ratio for 10,000 attempts?
What is the ratio for 100,000 attempts?
What is the ratio for 1,000,000 attempts?
What is the ratio for 10,000,000 attempts?
Show your working code to the TA.
TA Initials
Now validate your simulation results by performing the physical experiment.
...just kidding. Computers are awesome, right? Give some respect for Buffon.
3

More products