Indian Institute of Technology Gandhinagar
PH 509: Computational Physics
ASSIGNMENT 5
Due date: 29 OCTOBER
Name: [Anoop Singh (16510011) , Sanu Gangwar (16510072) , Akash Arya (16510006)]
1 Problem 2
(Directed random walk in 2D) Simulate a directed random walk in two dimensions which is biased
towards one direction. On a 2D lattice (50x50), the probability to move along y axis is greater than x (
py > 0 : 5, px + py = 1). Plot the graphs of Random walk for few walks. For each py ,have atleast 1000
step random walk and average for M = 100 runs. Plot hr2 i(t). Investigate if the motion still diffusive.
1.1 Approach
First we consider random walk in 2D lattice as we know when we move along 2D lattice,we can go 1 in
x axis and 1 in y axis, so as given in question we are dealing with directed random in which probability
is given by py > 0 : 5, px + py = 1).When probability is greater 0.5 we increases 1 in y-directin and vice
versa.After that we find the average of r2 and plot it with time step.
1.2 Program
from future import d i v i s i o n
import m a t p l o t l i b . p y p l o t a s p l t
import numpy a s np
We a r e c r e a t i n g a programm f o r 2D random walk which i s not p e r f e c t l y f a i r and has h i g h e r
def d i r l a t t i c e ( n s t e p , n walk ) :
r 2 d a v g = np . z e r o s ( n s t e p )
s t e p a r r = np . a r a n g e ( 0 , n s t e p , 1 )
w1 = [ ]
w2 = [ ]
V=np . a r a n g e ( 0 , n walk , 1 )
f o r i in range ( n walk ) :
#Auto i n s e r t i o n a c c o r d i n g t o Seed Mersine T w i s t e r Algorithm
r = np . random . RandomState ( )
x arr = [ ]
y arr =[]
# Initialisation
x = 0
y = 0
n=( n s t e p 1)/2
f o r j in range ( 1 0 0 ) :
r num = np . random . random ( )
i f ( r num <= 0 . 8 ) :
k=2np . random . r a n d i n t (2) 1
i f k==1:
y+=1
1
i f k==1:
y=1
x a r r . append ( x )
y a r r . append ( y )
e l i f ( r num > 0 . 2 ) :
k=2np . random . r a n d i n t (2) 1
i f k==1:
x+=1
i f k==1:
x=1
x a r r . append ( x )
y a r r . append ( y )
r 2 d a v g [ j ] += ( x 2 +y 2 )
#p r i n t r 2 d a v g [ j ]
#p r i n t x a r r
#p r i n t y a r r
m1=x a r r [ 1]
m2=y a r r [ 1]
w1 . append (m1)
w2 . append (m2)
i f ( i <4):
plt . figure (1)
p l t . p l o t ( x a r r , y a r r , - )
p l t . x l a b e l ( X --> )
p l t . y l a b e l ( Y --> )
p l t . t i t l e ( " Biased Random Walk in 2D " )
p l t . g r i d ( True )
p l t . show ( )
r 2 d a v g = r 2 d a v g / n walk
plt . figure ()
p l t . p l o t ( s t e p a r r , r2d avg , . )
p l t . x l a b e l ( " Total Steps --> " )
p l t . y l a b e l ( "$ <r ^2 > $ -->" )
p l t . t i t l e ( " Biased Random Walk in 2D " )
p l t . g r i d ( True )
p l t . show ( )
plt . figure ()
plt . subplot (1 ,2 ,1)
p l t . p l o t (V, w1 )
plt . subplot (1 ,2 ,2)
p l t . p l o t (V, w2 )
p l t . show ( )
n s t e p = 101
n walk = 1000
d i r l a t t i c e ( n s t e p , n walk )
2
1.3 Results
Plot given below shows the random walk on a 2D lattice biased in y-direction.
By this plot we can see motion is still diffusive.
By first plot as we can not say that the random walk is biased in y-direction,for confirmation we
plot last element of x and y array with V(as specified in program).