Calculating Mean Square Displacement for a single particle in 1d [closed]












1















I have a particle trajectory in 1D, j= and for the time=np.arange(0, 10 + dt, dt) where dt is the time step. I have calculate the MSD according to this article.



I have searched in google as well as here for 1d MSD in python but did not find any suitable one as my python knowledge is very beginner level. I have written one code and it is working without any error but I am not sure that it represent the same thing according to the given article. Here is my code,



j_i = np.array(j)
MSD=
diff_i=
tau_i=
for l in range(0,len(time)):
tau=l*dt
tau_i.append(tau)
for i in range(0,(len(time)-l)):
diff=(j_i[l+i]-j_i[i])**2

diff_i.append(diff)

MSD_j=np.sum(diff_i)/np.max(time)
MSD.append(MSD_j)


Can anyone please check verify the code and give suggestion if it is wrong.










share|improve this question















closed as too broad by Samuel Liew Dec 2 '18 at 2:11


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.



















  • Because you are explicitly asking for a code review, your question is better suited for Code Review.

    – Bram Vanroy
    Nov 23 '18 at 9:19











  • ohh sorry i did not know about that. @BramVanroy

    – Tanvir
    Nov 23 '18 at 9:23













  • Ignore suggestions to move questions to CR.

    – hpaulj
    yesterday
















1















I have a particle trajectory in 1D, j= and for the time=np.arange(0, 10 + dt, dt) where dt is the time step. I have calculate the MSD according to this article.



I have searched in google as well as here for 1d MSD in python but did not find any suitable one as my python knowledge is very beginner level. I have written one code and it is working without any error but I am not sure that it represent the same thing according to the given article. Here is my code,



j_i = np.array(j)
MSD=
diff_i=
tau_i=
for l in range(0,len(time)):
tau=l*dt
tau_i.append(tau)
for i in range(0,(len(time)-l)):
diff=(j_i[l+i]-j_i[i])**2

diff_i.append(diff)

MSD_j=np.sum(diff_i)/np.max(time)
MSD.append(MSD_j)


Can anyone please check verify the code and give suggestion if it is wrong.










share|improve this question















closed as too broad by Samuel Liew Dec 2 '18 at 2:11


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.



















  • Because you are explicitly asking for a code review, your question is better suited for Code Review.

    – Bram Vanroy
    Nov 23 '18 at 9:19











  • ohh sorry i did not know about that. @BramVanroy

    – Tanvir
    Nov 23 '18 at 9:23













  • Ignore suggestions to move questions to CR.

    – hpaulj
    yesterday














1












1








1








I have a particle trajectory in 1D, j= and for the time=np.arange(0, 10 + dt, dt) where dt is the time step. I have calculate the MSD according to this article.



I have searched in google as well as here for 1d MSD in python but did not find any suitable one as my python knowledge is very beginner level. I have written one code and it is working without any error but I am not sure that it represent the same thing according to the given article. Here is my code,



j_i = np.array(j)
MSD=
diff_i=
tau_i=
for l in range(0,len(time)):
tau=l*dt
tau_i.append(tau)
for i in range(0,(len(time)-l)):
diff=(j_i[l+i]-j_i[i])**2

diff_i.append(diff)

MSD_j=np.sum(diff_i)/np.max(time)
MSD.append(MSD_j)


Can anyone please check verify the code and give suggestion if it is wrong.










share|improve this question
















I have a particle trajectory in 1D, j= and for the time=np.arange(0, 10 + dt, dt) where dt is the time step. I have calculate the MSD according to this article.



I have searched in google as well as here for 1d MSD in python but did not find any suitable one as my python knowledge is very beginner level. I have written one code and it is working without any error but I am not sure that it represent the same thing according to the given article. Here is my code,



j_i = np.array(j)
MSD=
diff_i=
tau_i=
for l in range(0,len(time)):
tau=l*dt
tau_i.append(tau)
for i in range(0,(len(time)-l)):
diff=(j_i[l+i]-j_i[i])**2

diff_i.append(diff)

MSD_j=np.sum(diff_i)/np.max(time)
MSD.append(MSD_j)


Can anyone please check verify the code and give suggestion if it is wrong.







python numpy physics






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 9:17









Silmathoron

1,0211821




1,0211821










asked Nov 23 '18 at 9:02









TanvirTanvir

3810




3810




closed as too broad by Samuel Liew Dec 2 '18 at 2:11


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









closed as too broad by Samuel Liew Dec 2 '18 at 2:11


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.















  • Because you are explicitly asking for a code review, your question is better suited for Code Review.

    – Bram Vanroy
    Nov 23 '18 at 9:19











  • ohh sorry i did not know about that. @BramVanroy

    – Tanvir
    Nov 23 '18 at 9:23













  • Ignore suggestions to move questions to CR.

    – hpaulj
    yesterday



















  • Because you are explicitly asking for a code review, your question is better suited for Code Review.

    – Bram Vanroy
    Nov 23 '18 at 9:19











  • ohh sorry i did not know about that. @BramVanroy

    – Tanvir
    Nov 23 '18 at 9:23













  • Ignore suggestions to move questions to CR.

    – hpaulj
    yesterday

















Because you are explicitly asking for a code review, your question is better suited for Code Review.

– Bram Vanroy
Nov 23 '18 at 9:19





Because you are explicitly asking for a code review, your question is better suited for Code Review.

– Bram Vanroy
Nov 23 '18 at 9:19













ohh sorry i did not know about that. @BramVanroy

– Tanvir
Nov 23 '18 at 9:23







ohh sorry i did not know about that. @BramVanroy

– Tanvir
Nov 23 '18 at 9:23















Ignore suggestions to move questions to CR.

– hpaulj
yesterday





Ignore suggestions to move questions to CR.

– hpaulj
yesterday












1 Answer
1






active

oldest

votes


















1














The code is mostly correct, here is a modified version where:




  • I simplified some expressions (e.g. range)

  • I corrected the average, directly using np.mean because the MSD is a squared displacement [L^2], not a ratio [L^2] / [T].


Final code:



j_i    = np.array(j)
MSD =
diff_i =
tau_i =

for l in range(len(time)):
tau = l*dt
tau_i.append(tau)

for i in range(len(time)-l):
diff = (j_i[l+i]-j_i[i])**2
diff_i.append(diff)

MSD_j = np.mean(diff_i)
MSD.append(MSD_j)


EDIT: I realized I forgot to mention it because I was focusing on the code, but the ensemble average denoted by <.> in the paper should, as the name implies, be performed over several particles, preferentially comparing the initial position of each particle with its new position after a time tau, and not as you did with a kind of time-running average



EDIT 2: here is a code that shows how to do a proper ensemble average to implement exactly the formula in the article



js     = # an array of shape (N, M), with N the number of particles and
# M the number of time points
MSD_i = np.zeros((N, M))
taus =

for l in range(len(time)):
taus.append(l*dt) # store the values of tau

# compute all squared displacements at current tau
MSD_i[:, l] = np.square(js[:, 0] - js[:, l])

# then, compute the ensemble average for each tau (over the N particles)
MSD = np.mean(MSD_i, axis=0)


And now you can plot MSD versus taus and Bob's your uncle






share|improve this answer


























  • Is it not necessary to divide the mean by time?

    – Tanvir
    Nov 23 '18 at 9:26













  • No, at least if j is indeed a position (look at the unit of the MSD in the article: it is in micrometer squared); the <.> operator is just an average, which is here done over discrete values, so no time is involved

    – Silmathoron
    Nov 23 '18 at 9:28













  • ok. got it. thanks for your correction.

    – Tanvir
    Nov 23 '18 at 10:09











  • I just realized I forgot an important note on the physics part, please see the edit

    – Silmathoron
    Nov 23 '18 at 12:50











  • so for many particles, I have to calculate the MSD by the same way using the final code for each particle and then sum all the MSD and divide by the number of particle right?

    – Tanvir
    Nov 23 '18 at 17:17


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The code is mostly correct, here is a modified version where:




  • I simplified some expressions (e.g. range)

  • I corrected the average, directly using np.mean because the MSD is a squared displacement [L^2], not a ratio [L^2] / [T].


Final code:



j_i    = np.array(j)
MSD =
diff_i =
tau_i =

for l in range(len(time)):
tau = l*dt
tau_i.append(tau)

for i in range(len(time)-l):
diff = (j_i[l+i]-j_i[i])**2
diff_i.append(diff)

MSD_j = np.mean(diff_i)
MSD.append(MSD_j)


EDIT: I realized I forgot to mention it because I was focusing on the code, but the ensemble average denoted by <.> in the paper should, as the name implies, be performed over several particles, preferentially comparing the initial position of each particle with its new position after a time tau, and not as you did with a kind of time-running average



EDIT 2: here is a code that shows how to do a proper ensemble average to implement exactly the formula in the article



js     = # an array of shape (N, M), with N the number of particles and
# M the number of time points
MSD_i = np.zeros((N, M))
taus =

for l in range(len(time)):
taus.append(l*dt) # store the values of tau

# compute all squared displacements at current tau
MSD_i[:, l] = np.square(js[:, 0] - js[:, l])

# then, compute the ensemble average for each tau (over the N particles)
MSD = np.mean(MSD_i, axis=0)


And now you can plot MSD versus taus and Bob's your uncle






share|improve this answer


























  • Is it not necessary to divide the mean by time?

    – Tanvir
    Nov 23 '18 at 9:26













  • No, at least if j is indeed a position (look at the unit of the MSD in the article: it is in micrometer squared); the <.> operator is just an average, which is here done over discrete values, so no time is involved

    – Silmathoron
    Nov 23 '18 at 9:28













  • ok. got it. thanks for your correction.

    – Tanvir
    Nov 23 '18 at 10:09











  • I just realized I forgot an important note on the physics part, please see the edit

    – Silmathoron
    Nov 23 '18 at 12:50











  • so for many particles, I have to calculate the MSD by the same way using the final code for each particle and then sum all the MSD and divide by the number of particle right?

    – Tanvir
    Nov 23 '18 at 17:17
















1














The code is mostly correct, here is a modified version where:




  • I simplified some expressions (e.g. range)

  • I corrected the average, directly using np.mean because the MSD is a squared displacement [L^2], not a ratio [L^2] / [T].


Final code:



j_i    = np.array(j)
MSD =
diff_i =
tau_i =

for l in range(len(time)):
tau = l*dt
tau_i.append(tau)

for i in range(len(time)-l):
diff = (j_i[l+i]-j_i[i])**2
diff_i.append(diff)

MSD_j = np.mean(diff_i)
MSD.append(MSD_j)


EDIT: I realized I forgot to mention it because I was focusing on the code, but the ensemble average denoted by <.> in the paper should, as the name implies, be performed over several particles, preferentially comparing the initial position of each particle with its new position after a time tau, and not as you did with a kind of time-running average



EDIT 2: here is a code that shows how to do a proper ensemble average to implement exactly the formula in the article



js     = # an array of shape (N, M), with N the number of particles and
# M the number of time points
MSD_i = np.zeros((N, M))
taus =

for l in range(len(time)):
taus.append(l*dt) # store the values of tau

# compute all squared displacements at current tau
MSD_i[:, l] = np.square(js[:, 0] - js[:, l])

# then, compute the ensemble average for each tau (over the N particles)
MSD = np.mean(MSD_i, axis=0)


And now you can plot MSD versus taus and Bob's your uncle






share|improve this answer


























  • Is it not necessary to divide the mean by time?

    – Tanvir
    Nov 23 '18 at 9:26













  • No, at least if j is indeed a position (look at the unit of the MSD in the article: it is in micrometer squared); the <.> operator is just an average, which is here done over discrete values, so no time is involved

    – Silmathoron
    Nov 23 '18 at 9:28













  • ok. got it. thanks for your correction.

    – Tanvir
    Nov 23 '18 at 10:09











  • I just realized I forgot an important note on the physics part, please see the edit

    – Silmathoron
    Nov 23 '18 at 12:50











  • so for many particles, I have to calculate the MSD by the same way using the final code for each particle and then sum all the MSD and divide by the number of particle right?

    – Tanvir
    Nov 23 '18 at 17:17














1












1








1







The code is mostly correct, here is a modified version where:




  • I simplified some expressions (e.g. range)

  • I corrected the average, directly using np.mean because the MSD is a squared displacement [L^2], not a ratio [L^2] / [T].


Final code:



j_i    = np.array(j)
MSD =
diff_i =
tau_i =

for l in range(len(time)):
tau = l*dt
tau_i.append(tau)

for i in range(len(time)-l):
diff = (j_i[l+i]-j_i[i])**2
diff_i.append(diff)

MSD_j = np.mean(diff_i)
MSD.append(MSD_j)


EDIT: I realized I forgot to mention it because I was focusing on the code, but the ensemble average denoted by <.> in the paper should, as the name implies, be performed over several particles, preferentially comparing the initial position of each particle with its new position after a time tau, and not as you did with a kind of time-running average



EDIT 2: here is a code that shows how to do a proper ensemble average to implement exactly the formula in the article



js     = # an array of shape (N, M), with N the number of particles and
# M the number of time points
MSD_i = np.zeros((N, M))
taus =

for l in range(len(time)):
taus.append(l*dt) # store the values of tau

# compute all squared displacements at current tau
MSD_i[:, l] = np.square(js[:, 0] - js[:, l])

# then, compute the ensemble average for each tau (over the N particles)
MSD = np.mean(MSD_i, axis=0)


And now you can plot MSD versus taus and Bob's your uncle






share|improve this answer















The code is mostly correct, here is a modified version where:




  • I simplified some expressions (e.g. range)

  • I corrected the average, directly using np.mean because the MSD is a squared displacement [L^2], not a ratio [L^2] / [T].


Final code:



j_i    = np.array(j)
MSD =
diff_i =
tau_i =

for l in range(len(time)):
tau = l*dt
tau_i.append(tau)

for i in range(len(time)-l):
diff = (j_i[l+i]-j_i[i])**2
diff_i.append(diff)

MSD_j = np.mean(diff_i)
MSD.append(MSD_j)


EDIT: I realized I forgot to mention it because I was focusing on the code, but the ensemble average denoted by <.> in the paper should, as the name implies, be performed over several particles, preferentially comparing the initial position of each particle with its new position after a time tau, and not as you did with a kind of time-running average



EDIT 2: here is a code that shows how to do a proper ensemble average to implement exactly the formula in the article



js     = # an array of shape (N, M), with N the number of particles and
# M the number of time points
MSD_i = np.zeros((N, M))
taus =

for l in range(len(time)):
taus.append(l*dt) # store the values of tau

# compute all squared displacements at current tau
MSD_i[:, l] = np.square(js[:, 0] - js[:, l])

# then, compute the ensemble average for each tau (over the N particles)
MSD = np.mean(MSD_i, axis=0)


And now you can plot MSD versus taus and Bob's your uncle







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 21:46

























answered Nov 23 '18 at 9:20









SilmathoronSilmathoron

1,0211821




1,0211821













  • Is it not necessary to divide the mean by time?

    – Tanvir
    Nov 23 '18 at 9:26













  • No, at least if j is indeed a position (look at the unit of the MSD in the article: it is in micrometer squared); the <.> operator is just an average, which is here done over discrete values, so no time is involved

    – Silmathoron
    Nov 23 '18 at 9:28













  • ok. got it. thanks for your correction.

    – Tanvir
    Nov 23 '18 at 10:09











  • I just realized I forgot an important note on the physics part, please see the edit

    – Silmathoron
    Nov 23 '18 at 12:50











  • so for many particles, I have to calculate the MSD by the same way using the final code for each particle and then sum all the MSD and divide by the number of particle right?

    – Tanvir
    Nov 23 '18 at 17:17



















  • Is it not necessary to divide the mean by time?

    – Tanvir
    Nov 23 '18 at 9:26













  • No, at least if j is indeed a position (look at the unit of the MSD in the article: it is in micrometer squared); the <.> operator is just an average, which is here done over discrete values, so no time is involved

    – Silmathoron
    Nov 23 '18 at 9:28













  • ok. got it. thanks for your correction.

    – Tanvir
    Nov 23 '18 at 10:09











  • I just realized I forgot an important note on the physics part, please see the edit

    – Silmathoron
    Nov 23 '18 at 12:50











  • so for many particles, I have to calculate the MSD by the same way using the final code for each particle and then sum all the MSD and divide by the number of particle right?

    – Tanvir
    Nov 23 '18 at 17:17

















Is it not necessary to divide the mean by time?

– Tanvir
Nov 23 '18 at 9:26







Is it not necessary to divide the mean by time?

– Tanvir
Nov 23 '18 at 9:26















No, at least if j is indeed a position (look at the unit of the MSD in the article: it is in micrometer squared); the <.> operator is just an average, which is here done over discrete values, so no time is involved

– Silmathoron
Nov 23 '18 at 9:28







No, at least if j is indeed a position (look at the unit of the MSD in the article: it is in micrometer squared); the <.> operator is just an average, which is here done over discrete values, so no time is involved

– Silmathoron
Nov 23 '18 at 9:28















ok. got it. thanks for your correction.

– Tanvir
Nov 23 '18 at 10:09





ok. got it. thanks for your correction.

– Tanvir
Nov 23 '18 at 10:09













I just realized I forgot an important note on the physics part, please see the edit

– Silmathoron
Nov 23 '18 at 12:50





I just realized I forgot an important note on the physics part, please see the edit

– Silmathoron
Nov 23 '18 at 12:50













so for many particles, I have to calculate the MSD by the same way using the final code for each particle and then sum all the MSD and divide by the number of particle right?

– Tanvir
Nov 23 '18 at 17:17





so for many particles, I have to calculate the MSD by the same way using the final code for each particle and then sum all the MSD and divide by the number of particle right?

– Tanvir
Nov 23 '18 at 17:17



Popular posts from this blog

Feedback on college project

Futebolista

Albești (Vaslui)