5.5. Further Practice#

5.5.1. Diffusion Model#

Exercise 5.6

Adapt your diffusion model code so that it stores the result in a 3d array z. Each slice z[i,:,:] should be an n by n array representing the concentration of the region at timestep i.

N = 20 # number of time steps
n = 10 # size of region
z = np.zeros((N, n, n))

z[0,:,:] = # the inital concentrations

for i in range(N):
    # set z[i,:,:] to the value of the next moving average

Then create line graphs which show the concentration at a point
1. At the centre
2. Near the edge

Over time. What is the long-term average concentration at each point?

Exercise 5.7

Build a more realistic model of 2-dimensional diffusion. Assume that at each timestep a fraction \(d\) of the concentration in each cell leaks into each of the 8 surrounding cells. This happens in both directions, so that the value \(x_{i,j}\) in the next timestep is given by

\[ x_{i,j} - \mathrm{flow~out~of~cell~} i,j + \mathrm{flow~into~cell~} i, j \]
\[=x_{i,j} - 8d \times x_{i,j} + d \times (\mathrm{sum~of~neighbouring~8~cells}). \]

Build a diffusion model based on the above. Experiment with different values of \(d\).

5.5.2. Game of Life#

There are many many possible computational investigations you can undertake related to the Game of Life. A couple of this to try are given below, but you are encouraged to read about Game of Life and come up with your own ideas.

Exercise 5.8

Sometimes the game of life eventually settles down to a constant terminal state. Write a function terminal_state(x) which returns the final state corresponding to the initial state x.

NOTE: You’ll have to determine what to do in the case that no terminal state is ever reached!

Exercise 5.9 (Challenge)

Sometimes the game of life eventually settles down to a sequence of periodically repeating states. Write a function cycle_period(x) which returns the number of states in the cycle corresponding to starting state x.