Code

Code

Wednesday, October 15, 2014

Logistic Map



Been playing around with some of the math functions in Python. I think it's really cool how such a simple formula can create something so complicated. If you're interested, I've documented the code I used to make the video (apologies for the lack of syntax highlighting at the moment):


import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

# Set up the figure, the axis, and the plot element
fig = plt.figure()
ax = plt.axes(xlim=(0, 60), ylim=(0, 1))
line, = ax.plot([], [], lw=1)

# initialization function: plot the background of each frame
def init():
    line.set_data([], [])
    return line,

# animation function. This is called sequentially
def animate(r):
    n = np.arange(0,60,1)
    x_n = .5 #initial value for x
    x_list = [] #initiate list

    for i in n:
        if i == 0:
            x_list.append(x_n) #initial value for x
        else:
            x_n1 = r*x_n*(1-x_n) #logisitic equation (the magic)
            x_list.append(x_n1) #add to x
            x_n = x_n1 #new x becomes old x
    x = np.array(x_list) #turn list into numpy array
    line.set_data(n, x)
    return line,

# specify array of parameter values
r = np.arange(2,4.5,.01)

# animate, save, show plot
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=r, interval=60, blit=True)
#anim.save('log_map2.mp4') #use default ffmpeg
plt.show()

2 comments:

  1. What exactly is this showing? Some of the patterns are interesting looking.
    //...

    ReplyDelete
    Replies
    1. The most common interpretation is that each frame of the movie shows you how large a population is over time. There is a variable "r" that changes each frame, which represents "Biotic potential", or how good the population is at reproducing. In this context, it's cool that if a population is either really bad OR really good at reproducing, the population dies off completely. You have to be in the sweet spot in the middle of the movie to survive.

      Delete