python - 2D Numpy Array Beginner troubles -
my issues trying work arrays each elements tuple of 2 values.
specifically problem generate random 2-dimensional walk of 200 (but testing 2) steps max distance, 100 (try 2) of walks per stage, , commencing each stage @ largest distance origin of previous stage.
i can generate array of random steps , them return final position (x,y) value , calculate distance of them origin of each walk:
that's defined in these functions:
#............................................................getpositioninteger def getposint(description) : """asks user enter positive integer""" askagain = false while askagain == false: try: posint = eval(raw_input("\n %s : " %description)) assert 0 < posint , "not positive integer" assert type(posint) == int , "not positive integer" askagain = true except : print "input failed, not positive integer" return posint #...............................................................initialposition def initialposition() : """initial position of walker @ start of random walk""" return (0.0, 0.0) #......................................................................distance def distance(posa, posb) : """distance between 2 positions""" xi = posa[0] ; yi = posa[1] xf = posb[0] ; yf = posb[1] return np.sqrt((xf-xi)**2+(yf-yi)**2) #..................................................................getpositions def getpositions(start, nsteps, maxstep): xarray = maxstep * np.random.random(nsteps+1)* np.cos(2.0 * np.pi * random.random()) yarray = maxstep * np.random.random(nsteps+1)* np.sin(2.0 * np.pi * random.random()) xarray[0] = start[0] yarray[0] = start[-1] xarray = np.cumsum(xarray) yarray = np.cumsum(yarray) return (xarray[-1], yarray[-1])
but can't array of final position of each walk per stage in (x,y) form per stage
here's main script , i'm having trouble:
import numpy np import matplotlib.pylab plt import random import time max_step_size = 0.90 # maximum size of single step [m] random.seed(12345) #..........................................................................main def main (): '''''' print "random walk in stages in 2 dimensions" userdata = getdetails() print "\nplease wait while random walks generated , analyzed..." num_stages = userdata[0] num_walks = userdata[1] num_steps = userdata[2] stagestart = initialposition() stage in np.arange(num_stages): walks = np.zeros((num_walks, num_walks), dtype=np.ndarray) walk in walks: walk = getpositions(stagestart, num_steps, max_step_size) print walk print walks
you see i'm having trouble making (x,y) style array, [0 0] should [0.0 , 0.0] , printed twice , additionally, not changing final position.
i appreciate , help, advice or references can provide. -sid
Comments
Post a Comment