Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
2018-10-06
Internal II
PROGRAMMING, DATA
STRUCTURES, AND
ALGORITHMS IN PYTHON
Submitted by
Darshana Sudheer
IMSc Physics
I150112
Discuss and write python program to find distance
from initial point to final point in a non reversing
random walk. Iterate the program 10,000 times and
find average distance.
Random walk
Imagine a perfume bottle opened in the front of a classroom and the fragrance
soon drifting throughout the room. The fragrance spreads because some
molecules evaporate from the bottle and then collide randomly with other
molecules in the air, eventually reaching your nose even though you are
hidden in the last row. The model we shall develop to describe the path
traveled by a molecule is called a random walk. “Random” because it is chance
collisions that determine the direction in which a perfume molecule travels,
and “walk” because it takes a series of “steps” for the molecule to get from
here to there.
There are different types of random walk.We can classify on the basis of
memory
Random walk: A walk with no memory . The walk is recurrent – that is, the
walker returns to the starting point with probability. The expected number
of steps to return to the starting point is infinite.
As given below
x=2
y=2
for i in range(10):
arr=np.random.randint(0,4)
if (arr==0):
x=0
y=1
if(arr==1):
x=1
y=0
if(arr==2):
x=-1
y=0
if(arr==3):
x=0
y=-1
x=0 y=1
x =0 y=-1
If particle moves west then it cannot move to east but it can move north, south
or more west.
y=2
for i in range(10):
arr=np.random.randint(0,4)
if (arr==0):
if(s!=3):
x=0
y=1
else:
x=0
y=0
continue
if(arr==1):
if(s!=2):
x=1
y=0
else:
x=0
y=0
continue
if(arr==2):
if(s!=1):
x=-1
y=0
else:
x=0
y=0
continue
if(arr==3):
if(s!=0):
x=0
y=-1
else:
x=0
y=0
continue
if(s!=arr):
Distance
We assume that a “walker” takes sequential steps
The walker starts at the origin and take N steps in the x-y plane, each of length
1. The first step has a horizontal (x) component or a vertical (y) component.
The second step has a horizontal component or a vertical component ,
similarly in case of last step .
the distances along the x and y axes just add algebraically. Accordingly, the
total x distance from the origin is found out
Likewise, , the total y distance is found out.
Algorithm
Add x value and y value separately for N steps(N=10).It should be done
inside the for loop.
Outside the for loop,find sum of (sum x)2 and (sum y)2.
h=0
k=0
x=2
y=2
h=h+x
k=k+y
q=math.sqrt(h*h+k*k)
https://compmath.wordpress.com/walks-with-memory/
http://www.eng.uc.edu/~beaucag/Classes/IntrotoPolySci/End-to-
endDistance.htm
https://en.wikipedia.org/wiki/Self-avoiding_walk