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.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子飒赃,更是在濱河造成了極大的恐慌禾酱,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,997評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件霉赡,死亡現(xiàn)場(chǎng)離奇詭異橄务,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)穴亏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門蜂挪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人嗓化,你說(shuō)我怎么就攤上這事棠涮。” “怎么了刺覆?”我有些...
    開封第一講書人閱讀 163,359評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵严肪,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我隅津,道長(zhǎng)诬垂,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,309評(píng)論 1 292
  • 正文 為了忘掉前任伦仍,我火速辦了婚禮,結(jié)果婚禮上充蓝,老公的妹妹穿的比我還像新娘隧枫。我一直安慰自己喉磁,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評(píng)論 6 390
  • 文/花漫 我一把揭開白布官脓。 她就那樣靜靜地躺著协怒,像睡著了一般。 火紅的嫁衣襯著肌膚如雪卑笨。 梳的紋絲不亂的頭發(fā)上孕暇,一...
    開封第一講書人閱讀 51,258評(píng)論 1 300
  • 那天,我揣著相機(jī)與錄音赤兴,去河邊找鬼妖滔。 笑死,一個(gè)胖子當(dāng)著我的面吹牛桶良,可吹牛的內(nèi)容都是我干的座舍。 我是一名探鬼主播,決...
    沈念sama閱讀 40,122評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼陨帆,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼曲秉!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起疲牵,我...
    開封第一講書人閱讀 38,970評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤承二,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后瑰步,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體矢洲,經(jīng)...
    沈念sama閱讀 45,403評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評(píng)論 3 334
  • 正文 我和宋清朗相戀三年缩焦,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了读虏。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,769評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡袁滥,死狀恐怖盖桥,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情题翻,我是刑警寧澤揩徊,帶...
    沈念sama閱讀 35,464評(píng)論 5 344
  • 正文 年R本政府宣布,位于F島的核電站嵌赠,受9級(jí)特大地震影響塑荒,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜姜挺,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評(píng)論 3 327
  • 文/蒙蒙 一齿税、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧炊豪,春花似錦凌箕、人聲如沸拧篮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)串绩。三九已至,卻和暖如春芜壁,著一層夾襖步出監(jiān)牢的瞬間礁凡,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工沿盅, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留把篓,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,831評(píng)論 2 370
  • 正文 我出身青樓腰涧,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親紊浩。 傳聞我的和親對(duì)象是個(gè)殘疾皇子窖铡,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容