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.

4.4. SIMULATING DISCRETE-TIME MODELS WITH MULTIPLE VARIABLES 47global x, y, xresult, yresultxresult.append(x)yresult.append(y)def update():global x, y, xresult, yresultx = 0.5 * x + yy = -0.5 * x + yinitialize()for t in xrange(30):update()observe()plot(xresult, ’b-’)plot(yresult, ’g--’)show()What I did in this code is essentially <strong>to</strong> repeat things for both x <strong>and</strong> y. Executing two plotcomm<strong>and</strong>s at <strong>the</strong> end produces two curves in a single chart. I added <strong>the</strong> ’b-’ <strong>and</strong> ’g--’options <strong>to</strong> <strong>the</strong> plot functions <strong>to</strong> draw xresult in a blue solid curve <strong>and</strong> yresult in a greendashed curve. If you run this code, it produces a decent result (Fig. 4.2), so things mightlook okay. However, <strong>the</strong>re is one critical mistake I made in <strong>the</strong> code above. Can you spotit? This is a ra<strong>the</strong>r fundamental issue in complex systems simulation in general, so we’dbetter notice <strong>and</strong> fix it earlier than later. Read <strong>the</strong> code carefully again, <strong>and</strong> try <strong>to</strong> findwhere <strong>and</strong> how I did it wrong.Did you find it? The answer is that I did not do a good job in updating x <strong>and</strong> y simultaneously.Look at <strong>the</strong> following part <strong>of</strong> <strong>the</strong> code:Code 4.12:x = 0.5 * x + yy = -0.5 * x + yNote that, as soon as <strong>the</strong> first line is executed, <strong>the</strong> value <strong>of</strong> x is overwritten by its newvalue. When <strong>the</strong> second line is executed, <strong>the</strong> value <strong>of</strong> x on its right h<strong>and</strong> side is alreadyupdated, but it should still be <strong>the</strong> original value. In order <strong>to</strong> correctly simulate simultaneousupdating <strong>of</strong> x <strong>and</strong> y, you will need <strong>to</strong> do something like this:

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

Saved successfully!

Ooh no, something went wrong!