0

I'm making a code to simulate a Brownian motion.

from random import random
import matplotlib.pyplot as plt
import numpy as np

N=100
p=0.5
l=1
x1=[]
x2=[]

x1.append(0)
x2.append(0)

for i in range(1, N):
    step = -l if random() < p else l
    X1 = x1[i-l] + step
    x1.append(X1)

for i in range(1, N):
    step = -l if random() < p else l
    X2 = x2[i-l] + step
    x2.append(X2)

x1mean=np.array(x1)
x2mean=np.array(x2)

mean=[]
for j in range (0,N):
    mean.append((x1mean[j]+x2mean[j])/2.0)

plt.plot(mean)
plt.plot(x1)
plt.plot(x2)
plt.show()

This code makes the displacement for 2 diferent particles, but in order to calculate the mean displacement properly, I would need to have a great number of particles, likes 100. As you can see, I'm looking for a way to condensate the code because I cannot repetat the same code 100 times.

Is there a way to create a loop that makes all this code in function of 1 variable, i.e. the number of particles?

Thanks.

melpomene
  • 84,125
  • 8
  • 85
  • 148
J.Agusti
  • 161
  • 11
  • After the `import`s, replace the first line, `N=100`, with `def function_name(N):`, and then indent the rest of the code beneath it. Afterwards you can call the new function and pass the number of particles as the argument value to it: i.e. `function_name(42)`. – martineau Oct 07 '17 at 14:21

2 Answers2

1

I can't provide you a working python code, because until now I did not write a single line of python code. But I can give you an idea how to solve your problem.


Assumptions:

N : Number of Moves
P : Number of Particles

Step 1:
Create a method generating your array/list and returning it. So you can re-use it and avoid copying your code.

def createParticleMotion(N, p, l):
    x1=[]
    x1.append(0)

    for i in range(1, N):
        step = -l if random() < p else l
        X1 = x1[i-l] + step
        x1.append(X1)

    return x1

Step 2:
Create a list of lists, lets call it particleMotions. The list it selves has P list of your N moves. Fill the list within a for loop for you number of particles P by calling the method from the first step and append the list paticleMotions by the returned list/array.

May be the answer for Python: list of lists will help you creating this.

Step 3:
After you created and filled particleMotions use this list within a double for loop and calculate the mean and store it in a list of means.

mean=[]
for n in range (0,N):
    sum=0
    for p in range (0,P):
        sum = sum + particleMotions[p][n]

    mean.append(sum/P)

And now you can use a next for loop to plot your result.

for particle in range (0,P):
    plt.plot(particleMotions[particle])

So again don't blame me for syntax errors. I am no phyton developer. I just want to give you a way to solve your problem.

Martin Backasch
  • 1,829
  • 3
  • 20
  • 30
0

This?

from random import random
import matplotlib.pyplot as plt
import numpy as np

N=100
p=0.5
l=1

mydict = {}

for n in range(100):
    mydict[n] = []
    mydict[n].append(0)

    for i in range(1, N):
        step = -l if random() < p else l
        X1 = mydict[n][i-l] + step
        mydict[n].append(X1)

for k,v in mydict.iteritems():
    plt.plot(v)

# mean
plt.plot([np.mean(i) for i in mydict.values()])

plt.show()
Anton vBR
  • 18,287
  • 5
  • 40
  • 46