How to program a little code that shows me the evolution of the system when starting with initial values?

45 Views Asked by At

I have rather no programming skills, neither with Matlab nor with other languages. I need a little "program" that shows me the evolution of a dynamical system when giving it some initial values. The system is at follows:

Consider $X=\left\{0,1,2\right\}^{\mathbb{Z}}$ and $T\colon X\to X$ is working as follows:

  • 1 becomes 2,
  • 2 becomes 0,
  • 0 becomes 1 if at least one of its two neighbors is 1, else it remains 0.

Now I would like to start with some values - let's say 50 - i.e. with a configuration of the form $$ 01201020012000021020001201020001200102 $$ or something like this and then in the lines below I would like to have the evolution step by step, maybe 100 steps.

Is it possible to program something like this and then to get the result in a picture I could add to a LaTeX-Document?

Maybe Matlab or R are appropriate programs for that. But as I already said I have so less skills.

Could you please help me? I do not really have to understand how it works as long as it works. :-)

1

There are 1 best solutions below

8
On BEST ANSWER

This is some quick code in python 2.7 (https://www.python.org/) which might help as a starter. Try putting it in a file my_script.py and run it with python. Hopefully if you look at it you can guess what some of it does. Roughly, for each line it goes through each number in turn and checks which case it belongs to and adds that the result.

start = "01201020012000021020001201020001200102"

def next_step(input_string):
  res = ""

  #Loop through each character in the number string
  for i, ch in enumerate(input_string):
    #Check each case and append to the result
    if ch == "0":
      #Check if neighbours are 1, careful with boundaries
      if i > 0 and input_string[i-1] == "1":
        res += "1"
      elif i + 1 < len(input_string) and input_string[i+1] == "1":
        res += "1" 
      else:
        res += "0" 

    elif ch == "1":
      res += "2" 

    elif ch == "2":
      res += "0" 

  #return the result
  return res 

#Do the work
v = start
for i in range(10):
  print v
  v = next_step(v)

I got the following result printed out as an example which you could copy into your document:

01201020012000021020001201020001200102
12012100120000002100012012100012001210
20120211200000000210120120210120012021
01200022000000000021201200021200120002
12000000000000000002012000002001200000
20000000000000000000120000000012000000
00000000000000000001200000000120000000
00000000000000000012000000001200000000
00000000000000000120000000012000000000
00000000000000001200000000120000000000

Edit: Some quick code to do Evgeny's suggestion without changing too much, it could be made neater. You may need to install some additional packages. Add the following to the bottom of the .py file It has red green and blue corresponding to one colour each and produces a .png you could use.

import numpy as np
from scipy.misc import imsave

num_rows = 50
v = start
x = np.zeros((num_rows, len(start), 3)) 
for i in range(num_rows):
  for j, ch in enumerate(v):
    if ch == "0":
      x[i, j, 0] = 255 
    if ch == "1":
      x[i, j, 1] = 255 
    if ch == "2":
      x[i, j, 2] = 255 
  v = next_step(v)

imsave('my_system.png', x)