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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

10.2. INTERACTIVE SIMULATION WITH PYCX 177for i in xrange(n):xlist[i] += rd.gauss(0, sd)ylist[i] += rd.gauss(0, sd)Here, a small Gaussian noise with mean 0 <strong>and</strong> st<strong>and</strong>ard deviation sd is added <strong>to</strong> <strong>the</strong> x-<strong>and</strong> y-coordinates <strong>of</strong> each particle in every time step.Combining <strong>the</strong>se components, <strong>the</strong> completed code looks like this:Code 10.6: r<strong>and</strong>om-walk-2D.pyimport matplotlibmatplotlib.use(’TkAgg’)from pylab import *import r<strong>and</strong>om as rdn = 1000 # number <strong>of</strong> particlessd = 0.1 # st<strong>and</strong>ard deviation <strong>of</strong> Gaussian noisedef initialize():global xlist, ylistxlist = []ylist = []for i in xrange(n):xlist.append(rd.gauss(0, 1))ylist.append(rd.gauss(0, 1))def observe():global xlist, ylistcla()plot(xlist, ylist, ’.’)def update():global xlist, ylistfor i in xrange(n):xlist[i] += rd.gauss(0, sd)ylist[i] += rd.gauss(0, sd)import pycxsimula<strong>to</strong>rpycxsimula<strong>to</strong>r.GUI().start(func=[initialize, observe, update])

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

Saved successfully!

Ooh no, something went wrong!