Physics 301 Computer Assignment 1: The Random Walk Winter 2012
Physics 301 Computer Assignment 1: The Random Walk Winter 2012
walk problem. It is directly related to many real world problem ranging from physics (diffusion of gases, electron motion in solids) to biology (various problems in genetics and ecology) to economics (price fluctuations). Although the basic random walk problem can be completely and e actly analy!ed using probability theory, we"re going to study the problem empirically with the use of random numbers. #any physics problems that lack an e act solution can nonetheless be well understood by looking at the statistics of many randomly generated events. $his statistical approach to understanding comple situations is called the Monte Carlo method, named for the #onte %arlo %asino in #onaco where fortunes are won and loss in games of chance. &or those of you who return for 'hysics ()*, we"ll use the #onte %arlo method to understand ferromagnetism. Part 1: The random walk in one dimension $he traditional statement of the random walk problem refers to a drunk trying to make his way down a narrow street. +e"s so inebriated that he"s e,ually likely to take a step to the right (one way along the street) or the left (the opposite way). If he starts underneath the lamp post, what can we say about where he"s likely to end up- (.ther than in the gutter.) /et"s state the problem more precisely. %onsider a particle that can move only on the x0a is, starting at x 1 ). It can move only in steps of length *, and for each step it has e,ual probability (2)02)) of moving in the positive or negative direction. 3here is it after N steps%learly x) 1 ) before any step is taken (N 1 )). After N 1 *, the particle has a 2)4 chance of being at x* 1 5* and a 2)4 chance of being at x* 1 6*. &rom each of these, it has a 2)4 chance of increasing x by * and a 2)4 chance of decreasing x by *. $he particle at x* 1 5* can go to 57 or )8 the particle at x* 1 6* can go to 67 or ). 9ou should convince yourself that x7 can be only 57, ), or 67, and the probabilities for these are 724, 2)4, and 724, respectively. :ote; #ake sure you notice that even values of N allow only even values of xN. $hat is x*)) might be ), 7, (, <, ... but cannot be an odd number such as =. 3hile it"s straightforward to use pencil and paper to track the possibilities for the first few steps, that ,uickly becomes unfeasible as N increases. +ence the role of the computer. 3e will attack the random walk program using > cel. $he basic computation task is to generate a series of xi values, i 1 ) to N, which we"ll place in one column of the spreadsheet from row * (i 1 )) to row N5* (i 1 N). >ach cell is calculated from the cell above it using (assuming %olumn ?) ?(i) 1 ?(i6*) @* where a 5* and a 6* each have a 2)4 chance of occurring.
It"s easy to generate random numbers in > cel using the function RAND(). 9ou have to have the parentheses, because this is a function, but there"s nothing inside the parentheses. :ow RAND() generates a random number between ) and *. 3e need a random number that is 5* 2)4 of the time and 6* 2)4 of the time. &irst, note that 2)4 of the numbers generated by RAND are in the range ).)6).2, and 2)4 are between ).26*.). Aecond, the function ROUND(x, 0) rounds whatever x is to the nearest integer () means !ero decimal places 6 i.e., an integer). 3ith a little thought, you should now be able use a combination of functions (that is, everything fits in * cell in the spreadsheet) to generate @* with 2)4 probability. 9ou"ll want to generate something like *))) random numbers this way and check that you really are generating e,ual numbers of * and 6*. :ow let"s do something with it. Benerate the series of integers ) to *))) in %olumn A. (.ne way is to use the &illCAeries command, under the >dit menu). 'ut a ) in ?* as the starting point of the random walk, then insert a formula in cells ?7 to ?*))* to randomly add @* to the cell above it. :ow you have a *)))0step random walk. ?ut looking at *))* numbers isn"t easy. &urther, this is Dust one of 7*))) possible *)))0step walks. $hat"s E*)))*)) walks, so we can"t look at all of them. ?ut we can look at a random sample of random walks. %opy %olumn ? to the ne t F columns. :ow you"ve got ten *)))0step random walks. And a nice way to look at the results is with a graph. Ao make an xy0graph with %olumn A as the x0a is and with *) graphs displayed. It should look something like
As a general rule of thumb for making > cel graphs for physics; $urn .&& the display of the legend8 we hardly ever need it. $urn .&& the hori!ontal grid lines. &ormat the plot area to :o &ill rather than the default gray background. &or this graph, set the hori!ontal a is to end at *))) and the vertical a is to go 6*)) to *)).
$here"s one more thing to learn; +ow to recalculate the worksheet. 3ith the calculations you might normally do in > cel, there"s only one answer. ?ut with random numbers, you can get entirely different answers with different random numbers. 9ou would like to recalculate the random walks with different random numbers, but without having to change anything in the worksheet. $he command for doing that is #anually %alculate, which calculates a new random number every time it encounters RAND(). .n a #ac, you recalculate the worksheet by pressing COMMAND + =. $hat is, pressing = while holding down COMMAND (the key with the (0leaf0 clover symbol). I don"t know on a '%, but find out by searching manually calculate in > cel +elp. After finding the #anually %alculate key, keep recalculating while watching the graphs. $he goal is to get some awareness of how much variability there is. 3hat"s changing every time3hat features are always present1: /ooking at the *) graphs you get each time, and hitting #anually %alculate several times, do you see any graphs that end e actly at x*))) 1 )- If so, does this occur fre,uently or rarely2: /ooking at the *) graphs you get each time, what would you estimate the average value of x*))) is- Bive your reasoning. (:ote; 9our reasoning should be !ased on your data, not on what you think ought to happen.) %an you also e plain why this ought to be the average3: As N increases, does the spread of the random walks 6 that is, the variation from most negative to most positive 6 increase, decrease, or stay about the same": Goes the spread increase linearly with N- &aster than linear- /ess than linear- $o answer this ,uestion, compare the spread 6 over many hits of #anually %alculate 6 at N 1 *))) to the spread at N 1 *)). ?ecause there"s no spread at N 1 ), a linear increase in spread would mean that the N 1 *))) spread is *) times the N 1 *)) spread. #ote: H*6H( are asking for your conclusions based simply on /..II:B at the graphs. 9ou shouldn"t be doing any calculations. Grawing conclusions from graphs is an important skill. &or 'art *, turn in * graph showing the ten random walks and the answers to ,uestions H* 6 H(. Part 2: The spread o$ random walks in one dimension In 'art * you should have found that the average value of x, denoted x , is !ero, which is not surprising, but that individual particles wander farther and farther from !ero as N increases. $his is the process of diffusion, where a substance initially highly concentrated at one point spreads out as time passes. 3hat can we learn about diffusion from studying the random walk$o answer this ,uestion, we"re going to need lots more data. 9ou"ve already used *),))) cells (*))) x *)) to track *) particles. It"s clearly not feasible to e tend this to hundreds or thousands of particles. &ortunately, > cel has some powerful tools that will let us substantially increase the power of our calculations 6 and help make you a power user of > cel.
$he calculation in > cel of a series of integers with ?(i) 1 ?(i6*) 5 * is an iteration, the same calculation done over and over. $he random walk calculation of 'art *, ?(i) 1 ?(i6*) @*, was also an iteration. In normal spreadsheet calculations, each iteration 6 each turn of the crank 6 gets its own cell. $hus very large iterative calculations re,uire very large 6 often unfeasible 6 numbers of cells. Auppose we don"t want to know all the intermediate values, only the very last value of a series of iterations. As an e ample, suppose we want to know the sum of all the integers up to 2); * 5 7 5 J 5 ... 5 2)- 9ou could easily do this in > cel, but taking up ,uite a few rows when all you really want to know is the very last value. Aumming integers to *),))) would be ,uite a pain. .pen a new spreadsheet, and in cell A7 type the formula = A2 + 1. $his instantly generates an error message that you have a circular reference, that the formula in A7 refers to A7. Interestingly, we can get around this restriction. I"ll e plain the procedure for a #ac8 a '% will be similar, but you '% users will have to figure it our. .n a #ac, go to > cel 'references (under the > cel menu) and select %alculation. 9ou"ll see that > cel, by default, is set to Automatic, meaning that the spreadsheet automatically recomputes each time you change anything. %hange this to #anual calculation, meaning that > cel will now not do any calculations until you press the #anually %alculate command you learned about in 'art *. Also check the Iteration bo in the same window, and set #a imum Iterations to 2). %lose the 'references window. ?ack to your spreadsheet. &irst, put a ) in cell A*. $hen change the cell A7 formula to
= IF(A1=0, 0 , A2+1)
%ell A* is now the switch. If A*1), cell A7 will be initiali!ed to ). If A*1* (or any other value), cell A7 e ecutes A75*. 3ith the Iterate bo checked, what was a circular reference will now be calculated as take what"s in A7, add * to it, but the result back into A7 as a new or updated value. It will do this for the number of times you"ve set as #a imum Iterations. $o run this procedure, first type ) in A*, then issue the #anually %alculate command. A7 should be ). %hange A* to *, and #anually %alculate again. ?y iterating, you"ve added * to the initial ) fifty times, so A7 should now be 2). 9ou"ve made an Iteration %ounterK %hange #a imum Iterations to 2)), reset the counter with A*1) and %alculate :ow, then run it with A*1* and %alculate :ow. A7 will count to 2)) faster than you can see. :ote; If you know a programming language, you"ll recogni!e that we"re doing the same thing you would do in a G. loop, &.L loop, 3+I/> loop, or some other loop structure. > cel is not a language, but the Iterate procedure allows you to do some of the things you might do with a loop. $o sum integers, we want to add the current value of the iteration to an e isting sum. 3e need a third cell. In AJ, enter the formula
= IF(A1=0, 0 , A3+A2)
$he A* switch initiali!es AJ to ). $hen as A7 increases *, 7, J, ..., that value is added to the growing sum in AJ. Merify that the sum ) 5 * 5 7 5 ... 5 2) 1 *7=2.
+int *; $o better see what"s happening, set #a imum Iterations to *. Initiali!e the counter, then set A*1* and press #anually %alculate. It will do * iteration, adding ) 5 * to get *. 3ithout reinitiali!ing, press #anually %alculate again. > cel will do one more iteration, increasing A7 to 7 and adding it to the * that was already in AJ, giving J. Another #anually %alculate will increase A7 to J and AJ to <, and so on. 3hen building a spreadsheet that uses iteration, it"s often good to iterate one step at a time to verify that things are behaving properly. +int 7; If you type something wrong, things can go horribly wrong in an iteration, sometimes getting stuck inside the iteration. $o break out and halt the iterations on a #ac, type COMMAND + .. $hat is, hit the period key . while holding down COMMAND. It will be something similar on a '%, but you might want to e periment to find out %: ?uild a J0cell spreadsheet to calculate factorials. Leport the formula you enter in each cell. %alculate 2)K and *7)K. 9ou can check the first on your calculator, but the second is probably a larger number than your calculator can handle. 3hat does this have to do with random walks- $he random walk calculation that took a *))*0 row0long column in 'art * can be collapsed into * cell that reports the final value x*)))K .r, by simply changing the value of #a imum Iterations to N, the final value xN for any N. ?uild a spreadsheet with %ell A* being the ) (to initiali!e) or * (to run) switch. %ell A7 being an iteration counter, as above. %ells ?* to ?*))) calculating *))) random walks by adding @* to the previous value. $hat is, each cell uses the power of iteration to compute the final position of an N0step random walk. $he *))) cells will compute xN for *))) independent random walks. %ell AJ calculating x as AVERAGE(B1:B1000). Aet AJ to show 7 decimal places, the other cells to show ) decimal places (integers). :ow there are so many calculation being performed, slowing down the spreadsheet, that you"ll be able to see the iteration counter increasing. $he x cell is a check that things are .I8 the average of *))) random walks should never stray very far from ).)). If it does, something is wrong. %heck with *)) steps, then *))) steps that everything is .I. :ote that *))) *)))0step random walks would have taken * million cells 6 *))) x *))) 6 as you were doing things in 'art *K (*))) *)))0step walks takes E* minute on my #ac.) 3hat we"d like to find out is how much the random walkers spread out 6 how much they diffuse 6 as N increases. Again, this is best done graphically. :ow, with lots of data, it will be best to make a histogram to summari!e results rather than looking at all *))) data points. An > cel spreadsheet can have several individual worksheets, accessed with the Aheet *, Aheet 7, etc. tabs at the bottom of the sheets. 9ou"ve written your random walk program on Aheet *. :ow go to Aheet 7, and in %olumn A put the numbers 62), 6(N, 6(<, ... (N, 2). (Lecall that x has only even
2
values if N is an even number.) .f the *))) random walkers on Aheet *, we want to find how many end at xN 1 62), how many end at xN 1 6(N, and so on up to xN 1 52). $his is a good use for the COUNTIF function, which you can use > cel +elp to read about. Ose COUNTIF in %olumn ?, with the range of cells to be counted referring back to ?*;?*))) on Aheet * while the condition for being counted is the neighboring value in %olumn A. In cell ?2J, put a formula to add all the values ?* to ?2*. 9ou have *))) walkers on Aheet *, each should have been counted once, so all the counts added together should total *))). :ote; Aomething about the order in which > cel does things during iterations seems to re,uire putting this data analysis on a separate sheet from the random walk calculations. I tried putting it all on one sheet, but couldn"t get it to work right. ?ut the spreadsheet probably calculates faster with the analysis on a separate sheet. :ow to test it. Bo to Aheet *, set #a imum Iterations to *)), initiali!e the sheet, then run it. Atay on Aheet * until the iteration counter reaches *)). $hen switch over to sheet 7. $he first few rows of %olumn ? should be ), then values should start increasing until reaching a ma imum at or near the row in which %olumn A is !ero (i.e., having x*)) 1 )), then decrease until the final few rows in %olumn ? are ). $he counter beneath %olumn ? should be e actly *))). Assuming all is .I, it"s time to graph. #ake an xy0graph of %olumns A and ?, this time displaying points only (make them bigK), no connecting lines. 9ou"ll need to manually set the x0 a is to go from 6<) to <) or 6() to () or something like that. 9ou want to make sure ) gets a label. $he graph should look something like;
I used a tick mark of (, so I wanted to end the a is at a multiple of (. I used @(( here. 3hat are we looking at- $he hori!ontal a is is xN, the final position at the end of an N0step
<
random walk. $he vertical a is is how many of the *))) random walks have that value of xN. &or e ample, we see that *< of the *))) *))0step walks ended at x*)) 1 7). 9ou can see that there"s a bell-shaped distribution of the end points of the random walk process. :ote; If you have a fast enough computer, or are patient enough, you can get smoother curves by using more random walkers. ?ut you have to make big changes to see much difference. $his is *))) walkers. $he curve will look better and smoother if you use 7))), but an increase from *))) to, say, **)) would hardly be noticeable. ())) walkers would reduce the fluctuations by a factor of 7 to make an even better curve. Lemember that all the walkers started at x 1 ). $he spread of the distribution, as seen in the graph, tells us how much the walkers have diffused after N steps. &or N 1 *)), the area 67) P x P 7) seems to have been hit by the diffusers, but none have yet reached as far as x 1 @2). 3e want to learn how the spread varies with N, but we need to decide how to measure spread. If the number of walkers Q, it can be shown that the bell0shaped distribution seen in the graph becomes a Baussian curve. &or a Baussian, the appropriate way to measure width or spread is to measure the point corresponding to e6).2 E ).<). In other words, the spread, which we"ll call Rx, is found by reading the value of x at the point where the curve has dropped to <)4 of its peak value. :ow that"s theory. In reality, you"ve got a noisy curve, and it"s not e actly clear what the peak value is. ?ut you can estimate. Imagine a smooth curve through the points in the graph above. I"d say the peak of a smooth curve would be E2N. <)4 of that is EJ2. .n the left side, a smooth curve passes through J2 at x E 6*(. .n the right, at x E *7. Averaging these two, we could say that Rx E *J for this curve. A bell0shaped curve doesn"t have a sharp edge, but Rx (on both sides of center) seems a reasonable measure of how much diffusion has occurred. &: Osing at least *))) random walkers (7))) or ())) if you can), measure Rx using the method Dust described for N 1 2). Lepeat ( times, getting ( different graphs. Lepeat, ( times each, for N 1 *)), ()), and F)). ($he last one will be really noisy and hard to measure, but do your best.) 'ut your results in a table with ( columns (labeled 2), *)), ()), F))) and * row for each Rx. $hen, at the bottom of the columns, average the ( values of Rx for each N. Aubmit one graph for each N (a total of ( graphs) and the table of measurements. '; 'ropose a hypothesis of how Rx depends on N. $hat is, a specific mathematical e,uation by which you could predict what the spread Rx should be for a particular N. (Gon"t forget that your Rx values have some uncertainty because they"re a statistical sample8 don"t treat them as e actly known ,uantities.) ?e sure that your e,uation makes the correct prediction for N 1 ). $hen use your hypothesis to predict Rx after *),))) steps. (; In the diffusion of a gas from a point source, the number of steps in the random walk is essentially the number of collisions a molecule has with other molecules. At a given pressure and temperature, the number of collisions is proportional to the time t since the molecules were released. $hat is, N t, where is the proportionality symbol. ?ased on this, how should the spread of a diffusing cloud of gas atoms depend on t- Bive your answer as R x SSSS where some function of t goes in the blank. > plain your reasoning.
=
&or 'art 7, turn in ( graphs (with H<) and the answers to ,uestions H2 6 HN. Part 3: The random walk in two dimensions ) *+TRA CR*,-T $he one0dimensional random walk could move only right ( 1 )T) or left ( 1 *N)T), with step si!e *. :ow consider a particle that still has step si!e * but can move randomly at any angle between )T and J<)T, or, e,uivalently, between ) and 7U rad. ?ecause RAND() generates a random number between ) and *, 2*PI()*RAND()generates a random angle between ) and 7U rad. 3ith a step si!e of *, a particle at (xi, yi) moves to (xi5*, yi5*) 1 (xi 5 cos , yi 5 sin ), where is the randomly generated angle. Gevise a method to generate and plot five 7)))0step two0dimensional random walks. (#y spreadsheet is J) columns across, 7))) columns down.) It will look something like
Aet both a es to span 6N) to N), and adDust the hori!ontal and vertical si!e to make the over0all graph as close to square as you can. 9ou can plot more than 2 if you want, but they"re often so overlapped that more than 2 gets to be a huge messK +ere, much more than in one dimension, you really begin to sense the idea of diffusion. &or 'art J, turn in * graph showing the paths of 2 or more random walkers.