-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrandom_walker.py
72 lines (51 loc) · 1.49 KB
/
random_walker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2019, sumeet92k
# All rights reserved. Please read the "license.txt" for license terms.
#
# Project Title: Materials Modeling Course Tutorials
#
# Developer: Sumeet Khanna
# Contact Info: https://github.com/sumeet92k
"""
TUTORIAL 6: Monte Carlo Random-Walker
Finds the relationship between mean squared displacement and time for a random
walker using Monte-Carlo technique
"""
import numpy as np
import matplotlib.pyplot as plt
import random
nexperiments = 1000
njumps = 200
x, y = np.zeros(njumps), np.zeros(njumps)
def perform_exp():
x[0], y[0] = 0, 0
for i in range(1, njumps):
random_number = np.random.random()
if random_number < 0.25:
# jump left
x[i] = x[i-1] - 1
y[i] = y[i-1]
elif random_number < 0.5:
x[i] = x[i-1] + 1
y[i] = y[i-1]
elif random_number < 0.75:
# jump above
x[i] = x[i-1]
y[i] = y[i-1] + 1
else:
# jump below
x[i] = x[i-1]
y[i] = y[i-1] - 1
return x, y
distance_squared = np.zeros(njumps)
for exp in range(nexperiments):
x, y = perform_exp()
for i in range(njumps):
distance_squared[i] = distance_squared[i] + (x[i]**2 + y[i]**2)
distance_squared = distance_squared/nexperiments
plt.plot(distance_squared)
plt.xlabel('njumps')
plt.ylabel('msd')
plt.grid()
plt.show()