Pymecavideo 8.0
Étude cinématique à l'aide de vidéos
template_ipynb.py
1import nbformat as nbf
2
3def genere_notebook(pts, graphs=(True,True,True,True,True)):
4 """
5 fonction qui génère et retourne un Notebook Jupyter
6 Arguments :
7 * pts : tuple [str] (t, x, y)
8 * graphs : tuple [True/False] (chronogramme, vitesse, vecteurs variation de vitesse, accélération, énergies)
9 """
10 nb = nbf.v4.new_notebook()
11
12 entete1 = """\
13%matplotlib inline
14%config InlineBackend.figure_format = 'svg'"""
15
16 entete2 = """\
17# Imports
18import numpy as np
19import matplotlib.pyplot as plt
20# Paramètres matplotlib
21plt.rcParams["figure.figsize"] = (9, 6)
22plt.style.use('seaborn-whitegrid')
23markersize = 10
24style = 'b+'"""
25
26 coord = """\
27# Coordonnées de Pymecavideo
28t = {0}
29x = {1}
30y = {2}""".format(pts[0], pts[1], pts[2])
31
32 chrono0 = """### Chronogramme des positions"""
33
34 chrono1 = """\
35plt.plot(x, y, style, markersize = markersize)
36plt.ylabel('Hauteur (m)')
37plt.xlabel('Distance (m)')
38plt.title("Chronogramme des positions")
39plt.gca().set_aspect('equal', adjustable='datalim')
40plt.show()"""
41
42 vitesse0 = """### Vecteurs vitesse"""
43
44 vitesse1 = """\
45Δx = np.array([x[i+1]-x[i-1] for i in range(1, len(x)-1)])
46Δy = np.array([y[i+1]-y[i-1] for i in range(1, len(y)-1)])
47Δt = np.array([t[i+1]-t[i-1] for i in range(1, len(t)-1)])
48vx = Δx/Δt
49vy = Δy/Δt
50plt.plot(x, y, style, markersize = markersize)
51plt.ylabel('Hauteur (m)')
52plt.title("Vecteurs vitesse")
53plt.xlabel('Distance (m)')
54plt.grid(True)
55plt.quiver(x[1:-1], y[1:-1], vx, vy, scale_units = 'xy', angles = 'xy', width = 0.003)
56plt.gca().set_aspect('equal', adjustable='datalim')
57plt.show()"""
58
59 variation0 = """### Vecteurs variation de vitesse"""
60
61 variation1 = """\
62Δvx = np.array([vx[i+1]-vx[i-1] for i in range(1, len(vx)-1)])
63Δvy = np.array([vy[i+1]-vy[i-1] for i in range(1, len(vy)-1)])
64plt.plot(x, y, style, markersize = markersize)
65plt.ylabel('Hauteur (m)')
66plt.xlabel('Distance (m)')
67plt.title("Vecteurs variation de vitesse")
68plt.grid(True)
69plt.quiver(x[2:-2], y[2:-2], Δvx, Δvy, scale_units = 'xy', angles = 'xy', width = 0.003)
70plt.gca().set_aspect('equal', adjustable='datalim')
71plt.show()"""
72
73 acceleration0 = """### Vecteurs accélération"""
74
75 acceleration1 = """\
76Δt_ = np.array([t[i+1]-t[i-1] for i in range(2, len(t)-2)])
77Δvx = np.array([vx[i+1]-vx[i-1] for i in range(1, len(vx)-1)])
78Δvy = np.array([vy[i+1]-vy[i-1] for i in range(1, len(vy)-1)])
79ax = Δvx/Δt_
80ay = Δvy/Δt_
81plt.plot(x, y, style, markersize = markersize)
82plt.ylabel('Hauteur (m)')
83plt.xlabel('Distance (m)')
84plt.title("Vecteurs accélération")
85plt.grid(True)
86plt.quiver(x[2:-2], y[2:-2], ax, ay, scale_units = 'xy', angles = 'xy', width = 0.003)
87plt.gca().set_aspect('equal', adjustable='datalim')
88plt.show()"""
89
90 energie0 = """### Energies"""
91
92 energie1 = """\
93# Données
94m = 1.00 # Masse du système (kg)
95g = 9.81 # Intensité de la pesanteur (N/kg)"""
96
97 energie2 = """\
98t_ = t[1:-1]
99Δx = np.array([x[i+1]-x[i-1] for i in range(1, len(x)-1)])
100Δy = np.array([y[i+1]-y[i-1] for i in range(1, len(y)-1)])
101Δt = np.array([t[i+1]-t[i-1] for i in range(1, len(t)-1)])
102vx = Δx/Δt
103vy = Δy/Δt
104v = np.sqrt(vx**2+vy**2)
105Ec = 0.5*m*v**2
106Ep = m*g*y[1:-1]
107Em = Ec + Ep
108plt.plot(t_, Ec, label = 'Ec')
109plt.plot(t_, Ep, label = 'Ep')
110plt.plot(t_, Em, label = 'Em')
111plt.xlabel('Temps (s)')
112plt.ylabel('Energies (J)')
113plt.legend()
114plt.show()"""
115
116 #Méta-données
117 kernelspec = { "display_name" : "Python 3", "language" : "python", "name" : "python3"}
118 language_info = { "codemirror_mode": {"name": "ipython", "version": 3}}
119 nb['metadata']['kernelspec'] = kernelspec
120 nb['metadata']['language_info'] = language_info
121 nb['metadata']['file_extension'] = ".py"
122 nb['metadata']['mimetype'] = "text/x-python"
123 nb['metadata']['name'] = "python"
124 nb['metadata']['nbconvert_exporter'] = "python"
125 nb['metadata']['pygments_lexer'] = "ipython3"
126 nb['metadata']['version'] = "3.9.2"
127 # Cellules
128 nb['cells'] = [nbf.v4.new_code_cell(entete1),
129 nbf.v4.new_code_cell(entete2),
130 nbf.v4.new_code_cell(coord)]
131 if graphs[0] :
132 nb['cells'] += [nbf.v4.new_markdown_cell(chrono0),
133 nbf.v4.new_code_cell(chrono1)]
134 if graphs[1] :
135 nb['cells'] += [nbf.v4.new_markdown_cell(vitesse0),
136 nbf.v4.new_code_cell(vitesse1)]
137 if graphs[2] :
138 nb['cells'] += [nbf.v4.new_markdown_cell(variation0),
139 nbf.v4.new_code_cell(variation1)]
140 if graphs[3] :
141 nb['cells'] += [ nbf.v4.new_markdown_cell(acceleration0),
142 nbf.v4.new_code_cell(acceleration1)]
143 if graphs[4] :
144 nb['cells'] += [ nbf.v4.new_markdown_cell(energie0),
145 nbf.v4.new_code_cell(energie1),
146 nbf.v4.new_code_cell(energie2)]
147 return nb
148
149
150
151
152