Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente Prochaine révisionLes deux révisions suivantes | ||
teaching:exos:simulations_random_walks_codes [2018/11/05 11:40] – [Représenter le déplacement d'un objet] villersd | teaching:exos:simulations_random_walks_codes [2018/11/05 11:55] – [Marche aléatoire d'un petit nombre de pas] villersd | ||
---|---|---|---|
Ligne 111: | Ligne 111: | ||
===== 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> |
# | # | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
- | from Tkinter | + | from tkinter |
import time | import time | ||
+ | from random import * | ||
window = Tk() | window = Tk() | ||
- | sizex=400 | + | sizex = 400 |
- | sizey=600 | + | sizey = 600 |
canvas = Canvas(window, | canvas = Canvas(window, | ||
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 | + | depx = 2 # displacement at each move in x direction |
- | depy=0 | + | depy = 0 # displacement at each move in y direction |
# create balls: | # create balls: | ||
- | no_particles= 20 | + | no_particles = 20 |
- | dy = (sizey-2.)/ | + | dy = (sizey-2.*y)/ |
- | 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, | + | ball = canvas.create_oval(x, |
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, | + | |
+ | # | ||
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> |
# | # | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
- | from Tkinter | + | from tkinter |
from random import choice | from random import choice | ||
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, | canvas = Canvas(window, | ||
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 | + | depx = 10 # displacement at each move in x direction |
- | depy=0 | + | depy = 0 |
# create balls: | # create balls: | ||
- | no_particles= | + | no_particles = 6400 |
- | dy = (sizey-2.)/ | + | dy = (sizey-2.*y)/ |
- | 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, | + | ball = canvas.create_oval(x, |
y = y+dy | y = y+dy | ||
ball_list.append(ball) | ball_list.append(ball) | ||
# | # | ||
- | no_moves=4 # number of moves | + | no_moves = 6 # 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.)/ | xpos.append(((no_moves+1.)/ | ||
# le facteur (no_moves+1.)/ | # le facteur (no_moves+1.)/ | ||
- | xh=np.array(xpos) | + | xh = np.array(xpos) |
- | #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, | n, bins, patches = ax.hist(xh, (no_moves)+1, | ||
- | print n,bins, patches | + | print(n,bins, patches) |
plt.show() | plt.show() | ||
Ligne 211: | Ligne 213: | ||
# | # | ||
- | </sxh> | + | </code> |
===== Marche aléatoire d'un grand nombre de pas ===== | ===== Marche aléatoire d'un grand nombre de pas ===== |