MTH 337: Week 3

"%pylab inline" generates graphs directly in the notebook

This does two things for us:

  1. Imports the plotting functions
  2. Displays the graph when a code cell is executed
In [1]:
%pylab inline
Populating the interactive namespace from numpy and matplotlib

Logical operators revisited

The order of precedence is:

  1. not
  2. and
  3. or
In [2]:
# Quiz 1: question 6 returns True
6 != 5 or 5 > 4 and 4 == 3
Out[2]:
True
In [3]:
# Because "and" is higher than "or", this is evaluated as True or (True and False)
True or True and False
Out[3]:
True

Labeling graphs

In [4]:
x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 9, 16]
plot(x, y)
# "xlabel" adds a label to the x-axis
xlabel('x')
# "ylabel" adds a label to the y-axis
ylabel('y')
# "title" adds a title at the top of the graph
title('y = x*x')
Out[4]:
<matplotlib.text.Text at 0x7f2f90cd97d0>

"xrange" allows iteration without creating a list

This is more efficient for long iterations

In [5]:
# "xrange" works just like range in a "for" loop
for i in xrange(5):
    print i
0
1
2
3
4
In [6]:
# "xrange" takes the same parameters as "range"
# So, we can specify a start, step and step
for i in xrange(2, 10, 2):
    print i
2
4
6
8

"break" and "continue"

In [7]:
# "break" terminates a loop completely and jumps to the following code
# Note that strings are also containers - they contain a sequence of characters
# So, we can loop over all the characters in a string
for char in 'abcde':
    if char == 'c':
        print "Letter c found"
        break
Letter c found
In [8]:
# "continue" just terminates the current iteration, and continues the loop at the next item
# The even numbers are not printed, because the "print" statement is never reached
for x in xrange(10):
    if x % 2 == 0:
        continue
    print x
1
3
5
7
9
In [9]:
# We can use "continue" to ensure that the loop still runs to the end 
i = 0
total = 0
for char in 'abcdcjbssajhchjcvjavjce':
    if char == 'c':
        print "Letter c found at position", i 
        total += 1
        i += 1
        continue
    i += 1
print total, "c's found"
Letter c found at position 2
Letter c found at position 4
Letter c found at position 12
Letter c found at position 15
Letter c found at position 21
5 c's found

The "Mayfly Model"

$x_{t + 1} = b(1 - x_t)x_t$

  • $x_t$ is the mayfly population in year t (as a proportion of the maximum possible population)
  • $b$ is the maximum growth rate (without no limits to population growth)
In [10]:
# x is the initial population
# b is the maximum growth rate
# t is the number of years to run
def mayfly(x, b, t):
    for i in range(t):
        print x
        x = b*(1 - x)* x
In [11]:
# With b = 1.5, the population converges to a single stable state
mayfly(.5, 1.5, 20)
0.5
0.375
0.3515625
0.341949462891
0.337530041579
0.335405268916
0.334362861749
0.333846507648
0.333589525469
0.333461330949
0.333397307566
0.333365314311
0.333349322288
0.333341327427
0.333337330284
0.333335331785
0.333334332553
0.333333832942
0.333333583137
0.333333458235
In [12]:
# With b = 2.5, the population still converges to a stable state, but at a higher population
mayfly(.5, 2.5, 20)
0.5
0.625
0.5859375
0.606536865234
0.596624740865
0.601659148632
0.599163543749
0.600416478978
0.599791326874
0.600104227702
0.599947858991
0.600026063708
0.599986966448
0.600006516351
0.599996741718
0.600001629114
0.599999185436
0.60000040728
0.599999796359
0.60000010182

Plotting the mayfly population over time

In [13]:
def plot_mayfly(x, b, t):
    for i in range(t):
        plot(i, x, 'ro')
        x = b*(1 - x)* x
In [14]:
plot_mayfly(.5, 1.5, 20)
In [15]:
plot_mayfly(.5, 2.5, 20)