Final Project

Name:Li Jinhong
Class:材料班
Student ID:2014301020141

Back Ground

A random walk is a mathematical object which describes a path that consists of a succession of random steps. For example, the path traced by a molecule as it travels in a liquid or a gas, the search path of a foraging animal, superstring behavior, the price of a fluctuating stock and the financial status of a gambler can all be approximated by random walk models, even though they may not be truly random in reality.The term random walk was first introduced by Karl Pearson in 1905.



Abstract
Random Walks
Random Walks and Diffusion

The Main Body
What is Random Walk
The simplest random walk to understand is a 1-dimensional walk. Suppose that the black dot below is sitting on a number line. The black dot starts in the center.


Then, it takes a step, either forward or backward, with equal probability. It keeps taking steps either forward or backward each time. Let's call the 1st step a1, the second step a2, the third step a3 and so on. Each "a" is either equal to +1 (if the step is forward) or -1 (if the step is backward). The picture below shows a black dot that has taken 5 steps and ended up at -1 on the number line.

code:
import randomimport matplotlib.pyplot as pltclass random_walks: def init(self,n=100,step_length=0.1): self.x=[0] self.y=[0] self.x2=[0] self.n=n self.l=step_length def walk1(self):#random walk with step_length=1 for i in range(1,self.n): self.x.append(i) temp=random.random() if temp < 0.5: self.y.append(self.y[-1]-self.l) elif temp > 0.5: self.y.append(self.y[-1]+self.l) self.x2.append(self.x2[-1]+self.l2) def walk2(self):#random walk with random step-length for i in range(1,self.n): self.x.append(i) temp=random.random() self.l=random.random() if temp < 0.5: self.y.append(self.y[-1]-self.l) elif temp > 0.5: self.y.append(self.y[-1]+self.l) self.x2.append(self.x2[-1]+self.l2) def show1(self): plt.plot(self.x,self.y,'o') plt.title('random walk in one dimension') plt.xlabel('step number') plt.ylabel('x') plt.grid(True) def show2(self): plt.plot(self.x,self.x2,'.',label='$<x^2>$ versus time') plt.title('random walk in one dimension') plt.xlabel('step number') plt.ylabel('$<x^2>$') plt.legend(frameon=True) plt.grid(True)a=random_walks()a.walk1()a.show1()b=random_walks()b.walk1()b.show1()

random walk with step length=1:

x versus step number(time), for two random walks in one dimension


random walk with random step lengths:


Random Walks and Diffusion
An alternative way to describe the same physics involves the density of particles, ρ(x,y,z,t), which can be conveniently defined if the system contains a large number of particles (walkers). The idea, known as coarse graining, is to consider regions of space that are big enough to contain a large number of particles so that the density ( =mass/ volume) can be meaningfully defined. The density is then proportional to the probability per unit volume per unit time, denoted by P(x,y,z, t), to find a particle at (x, y, z) at time t. Thus, ρ and P obey the same equation.To find this equation, we focus back on an individual random walker. We assume that it is confined to take steps on a simple-cubic lattice, and that it makes one "walking step" each time step. P(i, j, k, n) is the probability to find the particle at the side (i, j, k) at time n. Since we are on a simple cubic lattice, there are 6 different nearest neighbor sites. If the walker is on one of these sites at time n-1, there is a probability of 1/6 that it will then move to site (i, j, k) at time n. Hence, the total probability to arrive at (i, j, k) is :

Rearranging the equation,and it suggests takking the continuum limit,which lead to:

This derivation shows the close connection between the random walks and diffusion.The density ρ obeys the same equation:

For ease of notation we will assume that ρ is a function of only one spatial dimension, x, although everything we do below can readily be extended to two or three dimensions. We can then write

so that the first index corresponds to space and the second to time.

and the finite-difference version of this is

rearranging to express the density at time step n+1 in terms of ρ at step n we find

code:
import pylab as pltimport numpy as np N=101dx=2./(N-1)dt=0.1D=1./4(dx2)/dtclass diffusion: def init(self,step): self.step=step self.x=np.linspace(-1,1,N) self.y=np.linspace(0,0,N) self.old_y=np.linspace(0,0,N) self.y[50]=1 def update(self): for i in range(N): self.old_y[i]=self.y[i] for i in range(1,N-1): self.y[i]=self.old_y[i]+Ddt/(dx2)(self.old_y[i+1]+self.old_y[i-1]-2self.old_y[i]) def fire(self): for i in range(self.step): self.update() i+=1 plt.plot(self.x,self.y,label="step="+str(self.step))A=diffusion(1000)A.fire()A=diffusion(100)A.fire()A=diffusion(10)A.fire()A=diffusion(50)A.fire()A=diffusion(200)A.fire()A=diffusion(500)A.fire()plt.legend(loc="best")plt.show()


import matplotlib.pyplot as pltimport numpy as npimport randomclass rand_walks: def init(self,l=100,N=5000,time=1000): self.l=l self.N=N self.n=time self.loca=[[0]self.l] def walk(self): self.loca[-1][int(self.l/2)]=self.N counter=0 while(1): counter+=1 temp=[0]self.l for i in range(self.l-2): for j in range(self.loca[-1][i+1]): rand=random.random() if rand>0.5: temp[i+2]+=1 elif rand<0.5: temp[i]+=1 self.loca.append(temp) if counter>self.n: break def show(self): x=np.arange(0,100,1) plt.plot(x,self.loca[-1],'.') plt.plot(x,self.loca[-1],label='time=%.f'%self.n) plt.title('random walks of %.f particles'%self.N) plt.xlabel('x') plt.ylabel('number of particles') plt.grid(True) plt.legend(frameon=True)b=rand_walks(time=10)b.walk()b.show()b=rand_walks(time=100)b.walk()b.show()b=rand_walks(time=1000)b.walk()b.show()





Acknowledgement
Nicholas J.Giodano's computational physics.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容