15.08.2015 Views

Introduction to the Modeling and Analysis of Complex Systems

introduction-to-the-modeling-and-analysis-of-complex-systems-sayama-pdf

introduction-to-the-modeling-and-analysis-of-complex-systems-sayama-pdf

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

196 CHAPTER 11. CELLULAR AUTOMATA I: MODELINGnextconfig[x, y] = 1 if count >= 4 else 0config, nextconfig = nextconfig, configNote <strong>the</strong> swapping <strong>of</strong> config <strong>and</strong> nextconfig at <strong>the</strong> end. This is precisely <strong>the</strong> sametechnique that we did for <strong>the</strong> simulation <strong>of</strong> multi-variable dynamical systems in <strong>the</strong> earlierchapters.By putting all <strong>the</strong> above codes <strong>to</strong>ge<strong>the</strong>r, <strong>the</strong> completed simula<strong>to</strong>r code in its entiretylooks like this:Code 11.5: panic-ca.pyimport matplotlibmatplotlib.use(’TkAgg’)from pylab import *n = 100 # size <strong>of</strong> space: n x np = 0.1 # probability <strong>of</strong> initially panicky individualsdef initialize():global config, nextconfigconfig = zeros([n, n])for x in xrange(n):for y in xrange(n):config[x, y] = 1 if r<strong>and</strong>om() < p else 0nextconfig = zeros([n, n])def observe():global config, nextconfigcla()imshow(config, vmin = 0, vmax = 1, cmap = cm.binary)def update():global config, nextconfigfor x in xrange(n):for y in xrange(n):count = 0for dx in [-1, 0, 1]:for dy in [-1, 0, 1]:count += config[(x + dx) % n, (y + dy) % n]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!