I'm trying to create a program which plots the different paths and points which you can take on the Cartesian plane using the following algorithm: Starting from the origin (0, 0), in step 1, you do one of the following:
*jump 1/2 to the right or 1/2 to left or terminate where you are.
If the movement hasn't ended yet, then in step 2:
*jump 1/4 upwards or 1/4 downwards or terminate where you are.
If the movement hasn't ended yet, then in step 3:
*jump 1/8 to the right or 1/8 to the left or terminate where you are,etc.
You would essentially do this indefinitely, where in step k, if k is even and the movement has not end yet, you would jump (1/2)^k upwards or downwards. Similarly, in step k if k is odd you jump (1/2)^k to the right or left or terminate where you are.
I want to plot the set of endpoints you can reach in this way in finitely many steps. Is the following code correct? By the way,the coloured circles at the end of each path is an endpoint. Are these plotted correctly?
import numpy
import pylab
import random
import matplotlib.pyplot as plt
axes = plt.axes()
k = 50
x = numpy.zeros(k)
y = numpy.zeros(k)
lst = []
for j in range(1,30):
for i in range(1,k):
dir = [-1,1][random.randint(0,1)]
if i%2!=0:
x[i] = x[i - 1] + 1/(2**(i)) * dir
y[i] = y[i - 1]
else:
x[i] = x[i - 1]
y[i] = y[i - 1] + 1/(2**(i)) * dir
plt.plot(x, y,marker = 'o')
plt.title("Different paths the random walk movement sequences could take")
pylab.show()
Aucun commentaire:
Enregistrer un commentaire