teaching:exos:simulations_random_walks_codes

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
teaching:exos:simulations_random_walks_codes [2018/11/05 11:40] – [Représenter le déplacement d'un objet] villersdteaching:exos:simulations_random_walks_codes [2018/11/05 12:09] (Version actuelle) – [Avec analyse de la distribution :] villersd
Ligne 108: Ligne 108:
 time.sleep(5) # on attend quelques secondes time.sleep(5) # on attend quelques secondes
 window.destroy() window.destroy()
-</codeh>+</code>
  
 ===== Représenter le déplacement de nombreux points ===== ===== Représenter le déplacement de nombreux points =====
-<sxh python; title : 04_tkinter_many_moves.py>+<code python 04_tkinter_many_moves.py>
 #!/usr/bin/python #!/usr/bin/python
 # -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
  
-from Tkinter import *+from tkinter import *
 import time import time
 +from random import * 
  
 window = Tk() window = Tk()
-sizex=400 +sizex = 400 
-sizey=600+sizey = 600
 canvas = Canvas(window, width = sizex, height = sizey) canvas = Canvas(window, width = sizex, height = sizey)
 canvas.pack() canvas.pack()
 x = 100 # initial left-most edge of first ball x = 100 # initial left-most edge of first ball
 y = 30 # initial top-most edge of first ball y = 30 # initial top-most edge of first ball
-r=20                  # ball diameter  +r = 16                  # ball diameter  
-depx=2            # displacement at each move in x direction +depx = 2            # displacement at each move in x direction 
-depy=0            # displacement at each move in y direction+depy = 0            # displacement at each move in y direction
  
 # create balls: # create balls:
-no_particles= 20 +no_particles = 20 
-dy = (sizey-2.)/(no_particles+1)       # y initial separation between balls +dy = (sizey-2.*y)/(no_particles+1)       # y initial separation between balls 
-print dy +print(dy) 
-ball_list=[]+ball_list = []
 for i in range(no_particles): for i in range(no_particles):
-    ball=canvas.create_oval(x,y,x+r,y+r,fill="blue")+    ball = canvas.create_oval(x,y,x+r,y+r,fill="blue")
     y = y+dy     y = y+dy
     ball_list.append(ball)     ball_list.append(ball)
  
 #moves #moves
-no_moves=100+no_moves = 100
 for j in range(no_moves): for j in range(no_moves):
     for ball in ball_list:     for ball in ball_list:
-        canvas.move(ball, depx, depy)+        canvas.move(ball, depx, choice([-2, 2]) ) 
 +#        canvas.move(ball, depx, depy)
     canvas.after(10)     canvas.after(10)
     canvas.update()     canvas.update()
Ligne 149: Ligne 151:
 time.sleep(5) # on attend quelques secondes time.sleep(5) # on attend quelques secondes
 window.destroy() window.destroy()
-</sxh>+</code>
  
 ===== Marche aléatoire d'un petit nombre de pas ===== ===== Marche aléatoire d'un petit nombre de pas =====
  
-<sxh python; title : 05_tkinter_random_walk_few_steps_1D.py>+<code python 05_tkinter_random_walk_few_steps_1D.py>
 #!/usr/bin/env python #!/usr/bin/env python
 # -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
  
-from Tkinter import *+from tkinter import *
 from random import choice     # http://docs.python.org/library/random.html from random import choice     # http://docs.python.org/library/random.html
 import numpy as np import numpy as np
Ligne 164: Ligne 166:
  
 window = Tk() window = Tk()
-sizex=200 +sizex = 200 
-sizey=600+sizey = 600
 canvas = Canvas(window, width = sizex, height = sizey) canvas = Canvas(window, width = sizex, height = sizey)
 canvas.pack() canvas.pack()
 x = 100 # initial left-most edge of first ball x = 100 # initial left-most edge of first ball
 y = 1 # initial top-most edge of first ball y = 1 # initial top-most edge of first ball
-r=4                  # ball diameter  +r = 4                  # ball diameter  
-depx=10            # displacement at each move in x direction +depx = 10            # displacement at each move in x direction 
-depy=0+depy = 0
  
 # create balls: # create balls:
-no_particles= 100 +no_particles = 6400 
-dy = (sizey-2.)/(no_particles+1)        # y initial separation between balls +dy = (sizey-2.*y)/(no_particles+1)        # y initial separation between balls 
-print dy +print(dy) 
-ball_list=[]+ball_list = []
 for i in range(no_particles): for i in range(no_particles):
-    ball=canvas.create_oval(x,y,x+r,y+r,fill="red")+    ball = canvas.create_oval(x,y,x+r,y+r,fill="red")
     y = y+dy     y = y+dy
     ball_list.append(ball)     ball_list.append(ball)
  
 #moves   #moves  
-no_moves= # number of moves+no_moves =  # number of moves
 for j in range(no_moves): for j in range(no_moves):
     for ball in ball_list:     for ball in ball_list:
Ligne 196: Ligne 198:
 xpos=[] xpos=[]
 for ball in ball_list: for ball in ball_list:
-    posi=canvas.coords(ball)+    posi = canvas.coords(ball)
     xpos.append(((no_moves+1.)/no_moves)*(posi[0]-x)/depx)     xpos.append(((no_moves+1.)/no_moves)*(posi[0]-x)/depx)
     # le facteur (no_moves+1.)/no_moves) permet de gérer la largeur des barres de l'histogramme     # le facteur (no_moves+1.)/no_moves) permet de gérer la largeur des barres de l'histogramme
-xh=np.array(xpos)  # see http://www.scipy.org/Cookbook/BuildingArrays +xh = np.array(xpos)  # see http://www.scipy.org/Cookbook/BuildingArrays 
-#print xh +#print(xh
  
 fig = plt.figure() fig = plt.figure()
 ax = fig.add_subplot(111) ax = fig.add_subplot(111)
 n, bins, patches = ax.hist(xh, (no_moves)+1, facecolor='green', alpha=0.75) n, bins, patches = ax.hist(xh, (no_moves)+1, facecolor='green', alpha=0.75)
-print n,bins, patches+print(n,bins, patches)
  
 plt.show() plt.show()
Ligne 211: Ligne 213:
 #window.mainloop() #window.mainloop()
  
-</sxh>+</code>
  
 ===== Marche aléatoire d'un grand nombre de pas ===== ===== Marche aléatoire d'un grand nombre de pas =====
  
-<sxh python; title : 06_tkinter_random_walk_many_steps_1D.py>+<code python 06_tkinter_random_walk_many_steps_1D.py>
 #!/usr/bin/env python #!/usr/bin/env python
 # -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
  
-from Tkinter import *+from tkinter import *
 from random import choice     # http://docs.python.org/library/random.html from random import choice     # http://docs.python.org/library/random.html
 import numpy as np import numpy as np
Ligne 226: Ligne 228:
  
 window = Tk() window = Tk()
-sizex=400 +sizex = 400 
-sizey=400+sizey = 400
 canvas = Canvas(window, width = sizex, height = sizey) canvas = Canvas(window, width = sizex, height = sizey)
 canvas.pack() canvas.pack()
 x = 200 # initial left-most edge of first ball x = 200 # initial left-most edge of first ball
 y = 1 # initial top-most edge of first ball y = 1 # initial top-most edge of first ball
-r=4                  # ball diameter  +r = 4                  # ball diameter  
-depx=1            # displacement at each move in x direction +depx = 1            # displacement at each move in x direction 
-depy=0+depy = 0
  
 # create balls: # create balls:
-no_particles= 2000+no_particles = 1600
 dy = (sizey-2.)/(no_particles+1)         # y initial separation between balls dy = (sizey-2.)/(no_particles+1)         # y initial separation between balls
-print dy +print(dy) 
-ball_list=[]+ball_list = []
 for i in range(no_particles): for i in range(no_particles):
-    ball=canvas.create_oval(x,y,x+r,y+r,fill="blue")+    ball = canvas.create_oval(x,y,x+r,y+r,fill="blue")
     y = y+dy     y = y+dy
     ball_list.append(ball)     ball_list.append(ball)
  
 #moves #moves
-no_moves=1000+no_moves = 200
 for j in range(no_moves): for j in range(no_moves):
     for ball in ball_list:     for ball in ball_list:
Ligne 256: Ligne 258:
 #analysis - histogram #analysis - histogram
 # see http://matplotlib.sourceforge.net/examples/api/histogram_demo.html # see http://matplotlib.sourceforge.net/examples/api/histogram_demo.html
-xpos=[]+xpos = []
 for ball in ball_list: for ball in ball_list:
-    posi=canvas.coords(ball)+    posi = canvas.coords(ball)
     xpos.append((posi[0]-x)/depx)     xpos.append((posi[0]-x)/depx)
-xh=np.array(xpos)  # see http://www.scipy.org/Cookbook/BuildingArrays+xh = np.array(xpos)  # see http://www.scipy.org/Cookbook/BuildingArrays
 #  compute the mean mu and sigma  from xh (and/or theoretical value from random walk result) #  compute the mean mu and sigma  from xh (and/or theoretical value from random walk result)
-mu=np.mean(xh) +mu = np.mean(xh) 
-sigma=np.std(xh)+sigma = np.std(xh)
 fig = plt.figure() fig = plt.figure()
 ax = fig.add_subplot(111) ax = fig.add_subplot(111)
 # print xh  # print xh 
 n, bins, patches = ax.hist(xh, 10, facecolor='green', alpha=0.75) n, bins, patches = ax.hist(xh, 10, facecolor='green', alpha=0.75)
-print n,bins, patches+print(n,bins, patches)
 # hist uses np.histogram to create 'n' and 'bins'. cf. http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html # hist uses np.histogram to create 'n' and 'bins'. cf. http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html
  
Ligne 279: Ligne 281:
  
 #window.mainloop() #window.mainloop()
-</sxh>+ 
 +</code>
  
 ==== Avec analyse de la distribution : ==== ==== Avec analyse de la distribution : ====
-<sxh python; title : 07_tkinter_random_walk_many_steps_1D-analysis.py> +<code python 07_tkinter_random_walk_many_steps_1D-analysis.py>
-#!/usr/bin/env python+
 # -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
  
-from Tkinter import *+from tkinter import *
 from random import choice     # http://docs.python.org/library/random.html from random import choice     # http://docs.python.org/library/random.html
 import numpy as np import numpy as np
Ligne 293: Ligne 295:
  
 window = Tk() window = Tk()
-sizex=400 +sizex = 400 
-sizey=400+sizey = 400
 canvas = Canvas(window, width = sizex, height = sizey) canvas = Canvas(window, width = sizex, height = sizey)
 canvas.pack() canvas.pack()
 x = 200 # initial left-most edge of first ball x = 200 # initial left-most edge of first ball
 y = 1 # initial top-most edge of first ball y = 1 # initial top-most edge of first ball
-r=4                  # ball diameter  +r = 4                  # ball diameter  
-depx=1            # displacement at each move in x direction +depx = 1            # displacement at each move in x direction 
-depy=0+depy = 0
  
 # create balls: # create balls:
-no_particles= 1000+no_particles = 1000
 dy = (sizey-2.)/(no_particles+1)         # y initial separation between balls dy = (sizey-2.)/(no_particles+1)         # y initial separation between balls
 #print dy #print dy
 ball_list=[] ball_list=[]
 for i in range(no_particles): for i in range(no_particles):
-    ball=canvas.create_oval(x,y,x+r,y+r,fill="blue")+    ball = canvas.create_oval(x,y,x+r,y+r,fill="blue")
     y = y+dy     y = y+dy
     ball_list.append(ball)     ball_list.append(ball)
  
 #moves #moves
-no_moves=900+no_moves = 400
 for j in range(no_moves): for j in range(no_moves):
     for ball in ball_list:     for ball in ball_list:
-        canvas.move(ball, choice([-1,-1,-1,-1,-1,1,1,1,1,1])*depx, depy)+        canvas.move(ball, choice([-1,-1,-1,-1,1,1,1,1,1,1])*depx, depy) #drift
     canvas.after(1)     canvas.after(1)
     canvas.update()     canvas.update()
Ligne 323: Ligne 325:
 #analysis - histogram #analysis - histogram
 # see http://matplotlib.sourceforge.net/examples/api/histogram_demo.html # see http://matplotlib.sourceforge.net/examples/api/histogram_demo.html
-xpos=[]+xpos = []
 for ball in ball_list: for ball in ball_list:
-    posi=canvas.coords(ball)+    posi = canvas.coords(ball)
     xpos.append(posi[0]-x)     xpos.append(posi[0]-x)
-xh=np.array(xpos)  # see http://www.scipy.org/Cookbook/BuildingArrays+xh = np.array(xpos)  # see http://www.scipy.org/Cookbook/BuildingArrays
 #  compute the mean mu and sigma  from xh (and/or theoretical value from random walk result) #  compute the mean mu and sigma  from xh (and/or theoretical value from random walk result)
-mu=np.mean(xh) +mu = np.mean(xh) 
-sigma=np.std(xh)+sigma = np.std(xh)
 fig = plt.figure() fig = plt.figure()
 ax = fig.add_subplot(111) ax = fig.add_subplot(111)
 # print xh  # print xh 
 n, bins, patches = ax.hist(xh, 20, facecolor='green', alpha=0.75) n, bins, patches = ax.hist(xh, 20, facecolor='green', alpha=0.75)
-print mu, sigma +print(mu, sigma) 
-print n,bins, patches+print(n,bins, patches)
 # hist uses np.histogram to create 'n' and 'bins'. # hist uses np.histogram to create 'n' and 'bins'.
 # np.histogram returns the bin edges, so there will be ii probability # np.histogram returns the bin edges, so there will be ii probability
Ligne 354: Ligne 356:
  
 #window.mainloop() #window.mainloop()
-</sxh>+</code>
  • teaching/exos/simulations_random_walks_codes.1541414425.txt.gz
  • Dernière modification : 2018/11/05 11:40
  • de villersd