Introduction to Matlab-random walks

advertisement
Computational Tools
Introduction to Matlab: Random Walks
Page 1 of 3
Introduction to Matlab: Random Walks
Overview
Consider a drunkard, on foot, trying to find is or her way home. Or consider a long chain polymer with a C-C
backbone that has many repeat units. Or consider the price of your favorite stock in the stock market. The
theory of random walks as been used to described each of these phenomena, and more. The theory assumes that
you may take a step in any direction with equal probability, that the steps are all of uniform size, and that you
know where you start.
(x2,y2)
a
a
a
(x5,y5)
a
(x1,y1)
Graph of 2D random
walk.
a
(x4,y4)
(x3,y3)
In this tutorial, you will program a random walk using matlab. After successfully completing this tutorial you
should be able to




Use the random number generator
Use plotting functions in 2D
Make calculations using the basic operators, *,+,-,/,^
Retrieve and store variable information in arrays.
Describing the Problem
Real world problem: How far will a drunkard actually go “as the crow flies” after taking N steps of length a.
Or, what is the “end-to-end” distance of a polymer having N repeat units of length a. Both are the same
problem.
Problem Statement: (1) Find the average distance from the starting point of a succession of N random
incremental steps as a function of N. An average is necessary because each simulation will produce a different
result. (2) Find the distribution of distances for N = 3000.
Methodology:
1. Generating an array of increments randomly (see Table 1).
2. Normalize the increments to length a,xi*xi / [(xiyi]1/2
3. Sum the steps to create the random path, xi = xi-1 +xi* .
4. Plot the path.
5. Repeat the simulation to find (xN , yN) several times and record these
values.
6. Find an average distance from the origin of each simulation and
record these values.
Table 1: Array of Increments
xincrement
 x1
 x2
…
 xN
yincrement
y1
y2
…
yN
Computational Tools
Introduction to Matlab: Random Walks
Page 2 of 3
Instructions for Random Walk Basic Tutorial
Each number below is a step in the methodology. Generate code for each step and test its execution.
0. Open a matlab editor worksheet. Enter clear at the command line to remove all variables from memory.
1. Generate a list of 200 random x,y pairs over the range -1 to +1. Store these values in two 1 x 200
matrices named xsteps and ysteps respectively. These are the steps themselves, the increments.
a. The command rand*(high-low)+low will generate a random, real number between low and
high.
b. rand(n,m) will generate an n x m matrix of random numbers.
c. Combining the two hints will help you generate the arrays.
2. Normalize each pair by dividing each x and y component by its length and saving the result in new
arrays, xstep_norm and ystep_norm. A sample for the x calculation is shown. How? Using a for loop,
move through the xstep and ystep matrices normalizing the elements of each.
xstep_norm(1,i)=xstep(1,i)/(ystep(1,i)^2+xstep(1,i)^2)^(1/2)
3. Sum the steps in the walk to generate the random path. Stores these in two 1 x 200 matrices named
xpath and ypath, respectively. How? Using a for loop, add each increment to the one before it and keep
track of the sum. If you have forgotten how this should work, write it out on paper first or look below.
A sample for the x calculation is shown.
xpath(1,i) = xpath(1,i-1)+xstep_norm(1,i)
4. Plot the random walk using the plot(xpath, ypath) function.
5. Now save the file in the default directory. This directory should be in the PATH for matlab. If you type
the filename at the command line it will run your program again.
6. Run the program 10 times, writing down the final end points each time. Find the average distance from
the origin of the ten points.
You have completed the basic portion of this tutorial. Each time you rerun the program it generates a new plot.
Questions
You may complete the section in pairs. Assume you must calculate the xstep and xstep and ystep matrices using
cosand sin These questions are due in the next class period as part of the tutorial.
A. Which steps in the above sequence would need to change?
B. Rewrite the descriptions of the changed steps using text examples of required calculations.
C. Generate matlab code to calculate the steps using  as the random variable and use this in your program.
Computational Tools
Introduction to Matlab: Random Walks
Basic Tutorial - Random Walks Part 2
Page 3 of 3
Store the final x-y pairs in two more matrices named endx and endy respectively. Do this by giving them the
first positions in their matrices. You will be stuffing more things into those matrices.
o Example: endx(1,1) = xpath(1,201) You may use a similar command for y.
o Use ‘clear xpath ypath xsteps ysteps’ to remove those variables from memory
o Run your program again and store the next set of final path values in the next location in the
endx matrix using endx(I,2) = xpath(1,201); similarly for y.
o Clear the variables and run your program again. Do this 100 times (just kidding)
o This will get old quickly, so use a for loop over your program to do it for you.

How? Find the distance between the origin and each random point in your endx,endy pairs and store
those values in dmat.
o The distance formula is d=(x^2+y^2)^(1/2).
o endx(1,i) will access the i’th item if endx is a row-vector
o dmat(1,i)=(endx(1,i)^2+endy(1,i)^2)^(1/2) is a useful form to use for assigning distances into
the matrix dmat inside a for loop.
Food for thought:
1. How would you generate a program for the freely rotating chain?
Download