forked from MaterSim/ComputationalPhysics300
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lec14hw.py
84 lines (65 loc) · 1.94 KB
/
lec14hw.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
Created on Sun Nov 18 14:37:44 2018
@author: cdibble2011
"""
%matplotlib inline
# Brownian Motion
import numpy as np
from numpy import linalg as LA
import matplotlib.pyplot as plt
plt.figure(figsize=[18, 12])
# init
L1 = 101
L2 = 101
N = 10000
N1 = np.linspace(0,N,N)
position = np.empty([N,2])
position[0,0] = int(L1/2)
position[0,1] = int(L2/2)
direction = np.array([[0,1],[0,-1],[1,0],[-1,0]])
# a few functions to scramble values
f = lambda x: x/np.exp(x)
g = lambda x: 1/(1+ x**2)
h = lambda x: x/3
# random walk
for i in range(1,N):
position1 = [-1,-1]
while min(position1)<0 or position1[0] >L1 or position1[1] >L2:
move = np.random.randint(len(direction))
position1 = position[i-1,:] + direction[move]
position[i,:] = position1
# split the (x,y) coordinates generated by Brownian motion
# and mix them up into random numbers
x, y = np.split(position,[-1], axis = 1)
randoms = f(y) + f(x) -g(y) + g(x) -h(y) +2*h(x)
# graph the brownian motion
plt.figure(figsize = (15,15))
plt.rcParams.update({'font.size': 22})
plt.xlim(0,101)
plt.ylim(0,101)
plt.title('Brownian motion')
plt.scatter(position[:,0], position[:,1], c=range(N), s=5)
plt.text(position[0,0], position[0,1], 'start', fontsize=16)
plt.text(position[-1,0], position[-1,1], 'end', fontsize=16)
plt.colorbar()
plt.show()
# graph the random numbers, should look like
# a stock price chart
plt.figure(figsize = (15,15))
plt.plot(N1,randoms,'--b',label = "randoms")
plt.plot(N1,x,'--k',label = "original x")
plt.plot(N1,y,'--g',label = "original y")
plt.title('Graph of Brownian motion and inputs')
plt.legend()
plt.show()
plt.figure(figsize = (15,15))
plt.plot(N1,f(x),'--b', label = "fx")
plt.plot(N1,f(y),'--r', label = "fy")
plt.plot(N1,g(x),'--o', label = "gx")
plt.plot(N1,g(y),'--k', label = "gy")
plt.plot(N1,h(x),'g-', label = "hx")
plt.plot(N1,h(y),'y-', label = "hy")
plt.title('Graph of subfunctions')
plt.legend()
plt.show()
print(randoms)