41 lines
935 B
Python
41 lines
935 B
Python
|
import numpy as np
|
||
|
import matplotlib.pyplot as plt
|
||
|
|
||
|
class BrownIterator(object):
|
||
|
def __init__(self, N, m):
|
||
|
self._N = N
|
||
|
self._max_m = m
|
||
|
self._i = 0
|
||
|
self._xs = None
|
||
|
self._ys = None
|
||
|
def __iter__(self):
|
||
|
self._xs = np.zeros(self._N)
|
||
|
self._ys = np.zeros(self._N)
|
||
|
self._i = 0
|
||
|
return self
|
||
|
def __next__(self):
|
||
|
self._i += 1
|
||
|
if(self._i > self._max_m):
|
||
|
raise StopIteration()
|
||
|
if(self._i == 1):
|
||
|
return self._xs, self._ys
|
||
|
|
||
|
theta = np.random.uniform(0, np.pi * 2, self._N)
|
||
|
|
||
|
self._xs = self._xs + np.cos(theta)
|
||
|
self._ys = self._ys + np.sin(theta)
|
||
|
return self._xs, self._ys
|
||
|
|
||
|
if( __name__ == "__main__"):
|
||
|
data = np.array([i for i in BrownIterator(1000, 321)])
|
||
|
print(data)
|
||
|
|
||
|
p1, = plt.plot(data[20,0], data[20,1], "ro", label="t = 20")
|
||
|
p2, = plt.plot(data[80,0], data[80,1], "bo", label="t = 80")
|
||
|
p3, = plt.plot(data[320,0], data[320,1], "go", label="t = 320")
|
||
|
|
||
|
plt.legend(handles=[p1, p2, p3])
|
||
|
plt.show()
|
||
|
|
||
|
|