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 175import matplotlibmatplotlib.use(’TkAgg’)from pylab import *# import necessary modules# define model parametersdef initialize():global # list global variables# initialize system statesdef observe():global # list global variablescla() # <strong>to</strong> clear <strong>the</strong> visualization space# visualize system statesdef update():global # list global variables# update system states for one discrete time stepimport pycxsimula<strong>to</strong>rpycxsimula<strong>to</strong>r.GUI().start(func=[initialize, observe, update])The first three lines <strong>and</strong> <strong>the</strong> last two lines should always be in place; no modification isneeded.Let’s work on some simple example <strong>to</strong> learn how <strong>to</strong> use pycxsimula<strong>to</strong>r.py. Herewe build a model <strong>of</strong> a bunch <strong>of</strong> particles that are moving around r<strong>and</strong>omly in a twodimensionalspace. We will go through <strong>the</strong> process <strong>of</strong> implementing this simulation modelstep by step.First, we need <strong>to</strong> import <strong>the</strong> necessary modules <strong>and</strong> define <strong>the</strong> parameters. In thisparticular example, a Gaussian r<strong>and</strong>om number genera<strong>to</strong>r is useful <strong>to</strong> simulate <strong>the</strong> r<strong>and</strong>ommotion <strong>of</strong> <strong>the</strong> particles. It is available in <strong>the</strong> r<strong>and</strong>om module <strong>of</strong> Python. There arevarious parameters that are conceivable for this example. As an example, let’s consider<strong>the</strong> number <strong>of</strong> particles <strong>and</strong> <strong>the</strong> st<strong>and</strong>ard deviation <strong>of</strong> Gaussian noise used for r<strong>and</strong>ommotion <strong>of</strong> particles, as model parameters. We write <strong>the</strong>se in <strong>the</strong> beginning <strong>of</strong> <strong>the</strong> code asfollows:Code 10.2:

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

Saved successfully!

Ooh no, something went wrong!