You are on page 1of 6

Assignment 1 - Snake Pit

Numerical Methods (6E5X0) — 2022/2023

1 Introduction
In this assignment, we will use the concepts of a random walk to study a mouse running, and a
self-avoiding walk to simulate a snake. The animals will be simulated in a 2D plane. The mouse
can move via a truly random walk, and the snakes are bound to a lattice.

Figure 1: Schematic overview of a random walk (red) and a self-avoiding walk (blue).

2 Goals
After successfully completing this assignment, you will be able to:

• Assign and use variables, arrays and functions in Matlab

• Use a variety of internal Matlab functions and their reference pages

• Create an algorithm to solve a multi-step problem

• Use various plotting tools

• Perform statistical analysis of the results

1
3 Questions
1. (0.5p) Study the behavior of the rand function, and create a command using this function
that gives a random number R in the range R ∈ (0, 2π). Experiment with the rand
function to see how to generate a single number or multiple numbers.
2. (0.5p) We will now generate an arbitrary (user defined) amount of random numbers. To
do this, we should create a function. Use the command in a new function that takes input
N (denoting the number of random numbers to be generated). The function should return
a vector containing these random numbers.
3. (0.5p) Use the function to generate a vector with a large amount of random numbers. Use
the histogram command to plot a histogram (look through the documentation to see how
it works). Verify that the distribution of the numbers generated is fair. It may be useful
to create a main script (run script) to collect often-used commands.
4. (1p) Create a function that takes a vector of angles (radians) as input. Use the input to
generate (x, y) coordinates on a unit-circle. Store them in a matrix of size (N, 2), and
return the matrix.
5. (1p) The vector (let’s call it s) we created contains the subsequent steps of size unity that
the mouse is taking in the (x, y)-plane. The mouse starts at pos = (0, 0). Create a time
step loop from t = 1 until tend = N , and store the mouse positions at each time step in
pos, using the movements stored in s. Create an animation by plotting the x vs y positions
at each step (use the drawnow command to update the figure while a loop is running). For
the report, show the time loop and the commands that occur inside, and show a typical
result figure.
6. (0.5p) From here on we’re especially interested in the final position that the mouse ends
up to at t = tend , as it resembles the mean travelled distance. It seems that, on average,
the mouse does not always travel very far from its origin. Compute the distance between
the starting point and the end point (end-to-end distance) DE .
7. (0.5p) Mice often come in great numbers∗. A single random walk does not mean much,
rather we are interested in the statistics of many random walks. Therefore, simulate a
large amount of random walks, and collect the⟨ resulting
⟩ DE and DE2 in two separate
vectors. Confirm that the theoretical relation DE2 (N ) = ℓ2 N is true, where ℓ is the
step length and ⟨·⟩ denotes the mean. Create a histogram of both DE and DE2 .
8. (1p) It turns out that the distribution of DE can be described with the following proba-
bility density function (pdf):
[ ]
2DE DE2
P (DE ) = exp −
L L

∗Did you know a couple can create an offspring of 6—8 mice every 6 weeks

2
with L the total path length. Create a function that computes P (DE ) (which input pa-
rameters do you need?) and compare the function values with the histogram of DE . Hint:
use the documentation for histogram to find out how to change the histogram normal-
ization.

9. (1p) The pdf is a Rayleigh distribution function, generalized as:


[ ]
x x2
P (x, σ) = 2 exp − 2
σ 2σ

For a generic Rayleigh distribution, the variance is computed by:


4−π 2
var = σ
2
Use the dfittool to fit a Rayleigh distribution, and compare the mean and variance of the
fitted distribution with the theoretical ones. Create a plot that contains (i) the histogram,
(ii) the plot P (DE ) and (iii) the fitted distribution.

The mouse was allowed to move anywhere throughout the 2D space. We will now study a
snake that is confined to a 2D lattice (similar to the Snake game, don’t waste too much time
there, there’s still plenty to do below.) Hence, the snake is only able to move left, or right, or
up, or down. Additionally, it is not allowed to cut through itself, so each projected move must
be checked before it can be accepted.

10. (0.5p) Create a function with input variables Nx and Ny that generates and returns a matrix
M (2D matrix, size Nx ×Ny ). Matrix M represents a 2D space from (1, 1) to (Nx , Ny ). We
will use this matrix to track the occupancy of each position. The matrix should be filled
with zeros (indicating free spaces), while the outer edges (left and right columns, top and
bottom rows) should be set to -1. These are the walls of our domain. Verify for yourself
that the function works correctly for different values of Nx and Ny . Call the function and
store the matrix, then initialize the snake as a single point s in the (approximate) center
of the domain, registering a 1 in matrix M at that position.

11. (1p) Now let’s create an algorithm that takes care of doing the self-avoiding walk of the
snake:

• Create a time loop


• Generate a projected random movement vector from the 4 possible options (up,
down, left or right).
• Test the move with matrix M ; if the new position is occupied: Try another move. If
the new position is free: accept the move, append the new position to s (the snake
grows), and update matrix M . How to deal with a situation where no moves are
available?

3
• Wrap up after the time loop by computing some useful properties of the snake (it’s
final length, the mean travelled distance (end-to-end distance)) for later processing.

12. (1p) Perform simulations with Nx = Ny = 5000, tend = 50 (i.e. 50 time steps, so maxi-
mum 50 movements). Consider two simulation cases to obtain DE and DE2 for a reason-
able large number of simulations (e.g. 1000):

• A set consisting of simulations where all snake lengths are accepted (also the ones
that stop prematurely because they ran out of acceptable moves)
• A set consisting of simulations with just snakes of length 50.

Fit a distribution (what kind of distribution seems suitable?) and analyze the mean prop-
erties. Compare to a comparable simulation of a truly random walk.

13. (1p, Explorer) Adjust the simulations with your own rules/inspiration and perform a small
investigation using the simulation tool. (for instance; add more snakes, study the effect
of larger simulation times, allow also diagonal steps, etc).

14. (0p, Challenger) A few ideas for those who like a challenge:

• Create a domain with periodic boundary conditions, i.e. walls that let the snake/-
mouse appear on the opposite side. Compare and explain
• Compare the statistics of snake lengths and end-to-end distances in a 2D space with
a 3D space.
• Simulate mice as single nodes moving around on the grid and let the snake(s) grow
only when they catch a mouse.

4 Epilogue
In this assignment, you have done a very simple physical simulation. Random walks, a term
coined by Karl Pearson [1], are used as model concepts in science for a wide range of topics
(e.g. for Brownian motion, diffusion, economics, computer networking, ecology, etc). In this
assignment, we have used the random walk and self-avoiding walk as so-called Monte-Carlo
simulation techniques. The chains could resemble the formation of polymer chains (growing 1
monomer at the time). Paul Flory argues that the repulsion of monomers at close range would
be described by this self-avoiding behavior.
I used for inspiration online sources [2] and [3], which I also recommend as a starting point for
further reading, if you are interested in this particular simulation application.

References
[1] Pearson, K. The Nature of the Random Walk. Nature, No. 1865, Vol. 72 (1905).

4
[2] Barkema, G.T. Monte Carlo Simulation of Lattice Polymer Models. Online document. http:
//wwwhome.lorentz.leidenuniv.nl/~zuiden/CP_Website/2014/SAW.pdf

[3] Bousquet-Mélou, M. Two-dimensional self-avoiding walks. Online document. http://


www.labri.fr/perso/bousquet/Exposes/fpsac-saw.pdf

5
References

You might also like