samedi 5 décembre 2015

how to repeat a function in python (many random walks)

enter image description hereI am making a function in python which simulates the random walk of a bacterium. But now I want to simulate the random walks, for example, of 200 bacteria. How should I do this? (maybe using one more for loop?) THANKS !!

here's my code and plot for one bacterium

x=20
y=40
def f(x,y):
return 4000-(x**2+y**2) #energy density

v = 2
k = 0.2
dt =0.1    
n_timepoints = 1000 #some constants
n_bact = 200




def simulation(n_bact,x,y,n_timepoints):#simulate the random walk of one bacterium


x=20
y=40
tracex = numpy.zeros((n_timepoints,))
tracey = numpy.zeros((n_timepoints,))
for i in range(n_timepoints):

    shift=[0,0,0,0,0,0,0,0,0,0]

    enew = f(x,y)
    shift.append(enew)
    shift = shift[-10:]
    de = shift[-1] - shift [0] #defference in energy within one sec
    t_half = 1+k*de/(10*dt)
    tau = t_half/numpy.log(2)
    p_not = numpy.exp(-(dt)/tau)#probability of not tunbling

    if random.random()<= p_not: #decide tumbles or not
            theta= 2*numpy.pi*random.random()
            tracex[i] = x
            tracey[i] = y        
            x=x+numpy.cos(theta)*v
            y=y+numpy.sin(theta)*v
    else:
            tracex[i]=x
            tracey[i]=y
            x=x
            y=y


return (tracex,tracey)
historyx,historyy= simulation(n_bact,x,y,n_timepoints)  
pyplot.figure()
pyplot.plot(historyy,historyx)
pyplot.show()`  




Aucun commentaire:

Enregistrer un commentaire