update examples
This commit is contained in:
parent
3efb7d13b8
commit
64b1474e43
4 changed files with 56 additions and 79 deletions
|
|
@ -1,58 +0,0 @@
|
||||||
import legrandchien as lgc
|
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
import matplotlib.animation as animation
|
|
||||||
|
|
||||||
g = lgc.Const(10)
|
|
||||||
m = lgc.Const(1)
|
|
||||||
|
|
||||||
x = lgc.Var("x")
|
|
||||||
y = lgc.Var("y")
|
|
||||||
|
|
||||||
T = 0.5 * m * (x.diff() * x.diff() + y.diff() * y.diff())
|
|
||||||
V = m * g * y
|
|
||||||
L = T - V
|
|
||||||
|
|
||||||
dico = {
|
|
||||||
"x" : 0,
|
|
||||||
"d_x" : 1,
|
|
||||||
"y" : 0,
|
|
||||||
"d_y" : 1
|
|
||||||
}
|
|
||||||
|
|
||||||
values = L.solve(dico, 10, 0.1)
|
|
||||||
|
|
||||||
# Extraire les données pour x et y
|
|
||||||
times = sorted(values.keys())
|
|
||||||
positions_x = [values[t]["x"] for t in times]
|
|
||||||
positions_y = [values[t]["y"] for t in times]
|
|
||||||
|
|
||||||
# Créer la figure
|
|
||||||
fig, ax = plt.subplots(figsize=(8, 8))
|
|
||||||
ax.set_xlim(min(positions_x) - 5, max(positions_x) + 5)
|
|
||||||
ax.set_ylim(min(positions_y) - 5, max(positions_y) + 5)
|
|
||||||
ax.set_xlabel("X (m)")
|
|
||||||
ax.set_ylabel("Y (m)")
|
|
||||||
ax.grid(True)
|
|
||||||
|
|
||||||
# Point animé
|
|
||||||
point, = ax.plot([], [], 'ro', markersize=15, label="Particule")
|
|
||||||
# Trajectoire
|
|
||||||
line, = ax.plot([], [], 'b-', alpha=0.3, linewidth=2, label="Trajectoire")
|
|
||||||
# Texte temps
|
|
||||||
time_text = ax.text(0.02, 0.95, '', transform=ax.transAxes, fontsize=12)
|
|
||||||
|
|
||||||
ax.legend()
|
|
||||||
|
|
||||||
def animate(frame):
|
|
||||||
# Point courant
|
|
||||||
point.set_data([positions_x[frame]], [positions_y[frame]])
|
|
||||||
# Trajectoire jusqu'au point courant
|
|
||||||
line.set_data(positions_x[:frame + 1], positions_y[:frame + 1])
|
|
||||||
# Texte
|
|
||||||
time_text.set_text(f"t = {times[frame]:.2f}s\nx = {positions_x[frame]:.2f}m, y = {positions_y[frame]:.2f}m")
|
|
||||||
return point, line, time_text
|
|
||||||
|
|
||||||
ani = animation.FuncAnimation(fig, animate, frames=len(times),
|
|
||||||
interval=50, blit=True, repeat=True)
|
|
||||||
|
|
||||||
plt.show()
|
|
||||||
|
|
@ -35,7 +35,8 @@ init = {
|
||||||
"d_theta2" : 0
|
"d_theta2" : 0
|
||||||
}
|
}
|
||||||
|
|
||||||
data = L.solve(init, 10, 0.01, True)
|
dt = 0.01
|
||||||
|
data = L.solve(init, 10, dt, True)
|
||||||
|
|
||||||
# Extraire les positions
|
# Extraire les positions
|
||||||
times = sorted(data.keys())
|
times = sorted(data.keys())
|
||||||
|
|
@ -47,6 +48,17 @@ y1_positions = [r1.value * np.cos(p["theta1"]) for p in positions]
|
||||||
x2_positions = [x1_positions[i] + r2.value * np.sin(p["theta2"]) for i, p in enumerate(positions)]
|
x2_positions = [x1_positions[i] + r2.value * np.sin(p["theta2"]) for i, p in enumerate(positions)]
|
||||||
y2_positions = [y1_positions[i] + r2.value * np.cos(p["theta2"]) for i, p in enumerate(positions)]
|
y2_positions = [y1_positions[i] + r2.value * np.cos(p["theta2"]) for i, p in enumerate(positions)]
|
||||||
|
|
||||||
|
# Calculer l'intervalle de frame pour 25 fps
|
||||||
|
fps = 25
|
||||||
|
frame_skip = max(1, int(1 / (fps * dt)))
|
||||||
|
|
||||||
|
# Filtrer les frames
|
||||||
|
display_indices = list(range(0, len(x1_positions), frame_skip))
|
||||||
|
x1_display = [x1_positions[i] for i in display_indices]
|
||||||
|
y1_display = [y1_positions[i] for i in display_indices]
|
||||||
|
x2_display = [x2_positions[i] for i in display_indices]
|
||||||
|
y2_display = [y2_positions[i] for i in display_indices]
|
||||||
|
|
||||||
# Créer la figure et l'axe
|
# Créer la figure et l'axe
|
||||||
fig, ax = plt.subplots(figsize=(8, 8))
|
fig, ax = plt.subplots(figsize=(8, 8))
|
||||||
ax.set_xlim(-3, 3)
|
ax.set_xlim(-3, 3)
|
||||||
|
|
@ -61,12 +73,12 @@ line2, = ax.plot([], [], 'o-', lw=2, markersize=8, label='Pendule 2')
|
||||||
trace, = ax.plot([], [], 'r-', alpha=0.3, lw=1, label='Trace')
|
trace, = ax.plot([], [], 'r-', alpha=0.3, lw=1, label='Trace')
|
||||||
|
|
||||||
def animate(frame):
|
def animate(frame):
|
||||||
line1.set_data([0, x1_positions[frame]], [0, y1_positions[frame]])
|
line1.set_data([0, x1_display[frame]], [0, y1_display[frame]])
|
||||||
line2.set_data([x1_positions[frame], x2_positions[frame]], [y1_positions[frame], y2_positions[frame]])
|
line2.set_data([x1_display[frame], x2_display[frame]], [y1_display[frame], y2_display[frame]])
|
||||||
trace.set_data(x2_positions[:frame+1], y2_positions[:frame+1])
|
trace.set_data(x2_display[:frame+1], y2_display[:frame+1])
|
||||||
return line1, line2, trace
|
return line1, line2, trace
|
||||||
|
|
||||||
ax.legend()
|
ax.legend()
|
||||||
anim = animation.FuncAnimation(fig, animate, frames=len(x1_positions), interval=50, blit=True, repeat=True)
|
anim = animation.FuncAnimation(fig, animate, frames=len(x1_display), interval=40, blit=True, repeat=True)
|
||||||
|
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
@ -33,9 +33,9 @@ V += k2 * (r2 - r20) * (r2 - r20)
|
||||||
L = T - V
|
L = T - V
|
||||||
|
|
||||||
init = {
|
init = {
|
||||||
"theta1" : -3 * np.pi/4,
|
"theta1" : - np.pi/4,
|
||||||
"d_theta1" : 0,
|
"d_theta1" : 0,
|
||||||
"theta2" : -np.pi/2,
|
"theta2" : - np.pi/4,
|
||||||
"d_theta2" : 0,
|
"d_theta2" : 0,
|
||||||
"r1" : 1,
|
"r1" : 1,
|
||||||
"d_r1" : 0,
|
"d_r1" : 0,
|
||||||
|
|
@ -43,7 +43,8 @@ init = {
|
||||||
"d_r2" : 0
|
"d_r2" : 0
|
||||||
}
|
}
|
||||||
|
|
||||||
data = L.solve(init, 5, 0.01)
|
dt = 0.001
|
||||||
|
data = L.solve(init, 10, dt)
|
||||||
|
|
||||||
# Extraire les positions
|
# Extraire les positions
|
||||||
times = sorted(data.keys())
|
times = sorted(data.keys())
|
||||||
|
|
@ -55,7 +56,16 @@ y1_positions = [p["r1"] * np.cos(p["theta1"]) for p in positions]
|
||||||
x2_positions = [x1_positions[i] + p["r2"] * np.sin(p["theta2"]) for i, p in enumerate(positions)]
|
x2_positions = [x1_positions[i] + p["r2"] * np.sin(p["theta2"]) for i, p in enumerate(positions)]
|
||||||
y2_positions = [y1_positions[i] + p["r2"] * np.cos(p["theta2"]) for i, p in enumerate(positions)]
|
y2_positions = [y1_positions[i] + p["r2"] * np.cos(p["theta2"]) for i, p in enumerate(positions)]
|
||||||
|
|
||||||
# Créer la figure et l'axe
|
fps = 25
|
||||||
|
frame_skip = max(1, int(1 / (fps * dt)))
|
||||||
|
|
||||||
|
display_indices = list(range(0, len(x1_positions), frame_skip))
|
||||||
|
x1_display = [x1_positions[i] for i in display_indices]
|
||||||
|
y1_display = [y1_positions[i] for i in display_indices]
|
||||||
|
x2_display = [x2_positions[i] for i in display_indices]
|
||||||
|
y2_display = [y2_positions[i] for i in display_indices]
|
||||||
|
|
||||||
|
# Créer la figure
|
||||||
fig, ax = plt.subplots(figsize=(8, 8))
|
fig, ax = plt.subplots(figsize=(8, 8))
|
||||||
ax.set_xlim(-3, 3)
|
ax.set_xlim(-3, 3)
|
||||||
ax.set_ylim(-3, 3)
|
ax.set_ylim(-3, 3)
|
||||||
|
|
@ -69,12 +79,13 @@ line2, = ax.plot([], [], 'o-', lw=2, markersize=8, label='Pendule 2')
|
||||||
trace, = ax.plot([], [], 'r-', alpha=0.3, lw=1, label='Trace')
|
trace, = ax.plot([], [], 'r-', alpha=0.3, lw=1, label='Trace')
|
||||||
|
|
||||||
def animate(frame):
|
def animate(frame):
|
||||||
line1.set_data([0, x1_positions[frame]], [0, y1_positions[frame]])
|
line1.set_data([0, x1_display[frame]], [0, y1_display[frame]])
|
||||||
line2.set_data([x1_positions[frame], x2_positions[frame]], [y1_positions[frame], y2_positions[frame]])
|
line2.set_data([x1_display[frame], x2_display[frame]], [y1_display[frame], y2_display[frame]])
|
||||||
trace.set_data(x2_positions[:frame+1], y2_positions[:frame+1])
|
trace.set_data(x2_display[:frame+1], y2_display[:frame+1])
|
||||||
return line1, line2, trace
|
return line1, line2, trace
|
||||||
|
|
||||||
ax.legend()
|
ax.legend()
|
||||||
anim = animation.FuncAnimation(fig, animate, frames=len(x1_positions), interval=50, blit=True, repeat=True)
|
anim = animation.FuncAnimation(fig, animate, frames=len(x1_display), interval=40, blit=True, repeat=True)
|
||||||
|
|
||||||
|
plt.show()
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
@ -3,7 +3,7 @@ import matplotlib.pyplot as plt
|
||||||
import matplotlib.animation as animation
|
import matplotlib.animation as animation
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
g = lgc.Const(10)
|
g = lgc.Const(-10)
|
||||||
m = lgc.Const(1)
|
m = lgc.Const(1)
|
||||||
k = lgc.Const(10)
|
k = lgc.Const(10)
|
||||||
r0 = lgc.Const(1)
|
r0 = lgc.Const(1)
|
||||||
|
|
@ -19,14 +19,15 @@ V = m * g * r * lgc.cos(theta) + k * (r - r0) * (r - r0)
|
||||||
|
|
||||||
L = T - V
|
L = T - V
|
||||||
|
|
||||||
dico = {
|
init = {
|
||||||
"theta" : -3.14/2,
|
"theta" : -3.14/2,
|
||||||
"d_theta" : 0,
|
"d_theta" : 0,
|
||||||
"r" : 1.2,
|
"r" : 1.2,
|
||||||
"d_r" : 0,
|
"d_r" : 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
data = L.solve(dico, 10, 0.01)
|
dt = 0.001
|
||||||
|
data = L.solve(init, 10, dt, True)
|
||||||
|
|
||||||
# Extraire les positions
|
# Extraire les positions
|
||||||
times = sorted(data.keys())
|
times = sorted(data.keys())
|
||||||
|
|
@ -35,6 +36,15 @@ positions = [data[t] for t in times]
|
||||||
x_positions = [p["r"] * np.sin(p["theta"]) for p in positions]
|
x_positions = [p["r"] * np.sin(p["theta"]) for p in positions]
|
||||||
y_positions = [p["r"] * np.cos(p["theta"]) for p in positions]
|
y_positions = [p["r"] * np.cos(p["theta"]) for p in positions]
|
||||||
|
|
||||||
|
# Calculer l'intervalle de frame pour 25 fps
|
||||||
|
fps = 25
|
||||||
|
frame_skip = max(1, int(1 / (fps * dt)))
|
||||||
|
|
||||||
|
# Filtrer les frames
|
||||||
|
display_indices = list(range(0, len(x_positions), frame_skip))
|
||||||
|
x_display = [x_positions[i] for i in display_indices]
|
||||||
|
y_display = [y_positions[i] for i in display_indices]
|
||||||
|
|
||||||
# Créer la figure et l'axe
|
# Créer la figure et l'axe
|
||||||
fig, ax = plt.subplots(figsize=(8, 8))
|
fig, ax = plt.subplots(figsize=(8, 8))
|
||||||
ax.set_xlim(-3, 3)
|
ax.set_xlim(-3, 3)
|
||||||
|
|
@ -42,15 +52,17 @@ ax.set_ylim(-3, 3)
|
||||||
ax.set_xlabel("X (m)")
|
ax.set_xlabel("X (m)")
|
||||||
ax.set_ylabel("Y (m)")
|
ax.set_ylabel("Y (m)")
|
||||||
ax.grid(True)
|
ax.grid(True)
|
||||||
|
ax.invert_yaxis()
|
||||||
|
|
||||||
line, = ax.plot([], [], 'o-', lw=2, markersize=8)
|
line, = ax.plot([], [], 'o-', lw=2, markersize=8, label='Pendule')
|
||||||
trace, = ax.plot([], [], 'r-', alpha=0.3, lw=1)
|
trace, = ax.plot([], [], 'r-', alpha=0.3, lw=1, label='Trace')
|
||||||
|
|
||||||
def animate(frame):
|
def animate(frame):
|
||||||
line.set_data([0, x_positions[frame]], [0, y_positions[frame]])
|
line.set_data([0, x_display[frame]], [0, y_display[frame]])
|
||||||
trace.set_data(x_positions[:frame+1], y_positions[:frame+1])
|
trace.set_data(x_display[:frame+1], y_display[:frame+1])
|
||||||
return line, trace
|
return line, trace
|
||||||
|
|
||||||
anim = animation.FuncAnimation(fig, animate, frames=len(x_positions), interval=50, blit=True, repeat=True)
|
ax.legend()
|
||||||
|
anim = animation.FuncAnimation(fig, animate, frames=len(x_display), interval=40, blit=True, repeat=True)
|
||||||
|
|
||||||
plt.show()
|
plt.show()
|
||||||
Loading…
Reference in a new issue