84 lines
No EOL
2.3 KiB
Python
84 lines
No EOL
2.3 KiB
Python
import legrandchien as lgc
|
|
import matplotlib.pyplot as plt
|
|
import matplotlib.animation as animation
|
|
import numpy as np
|
|
import cProfile
|
|
import pstats
|
|
|
|
g = lgc.Const(-10)
|
|
m1 = lgc.Const(1)
|
|
m2 = lgc.Const(1)
|
|
r1 = lgc.Const(1)
|
|
r2 = lgc.Const(1)
|
|
|
|
theta1 = lgc.Var("theta1")
|
|
theta2 = lgc.Var("theta2")
|
|
|
|
x1 = r1 * lgc.cos(theta1)
|
|
y1 = r1 * lgc.sin(theta1)
|
|
|
|
x2 = x1 + r2 * lgc.cos(theta2)
|
|
y2 = y1 + r2 * lgc.sin(theta2)
|
|
|
|
T = 0.5 * m1 * (x1.diff()*x1.diff() + y1.diff()*y1.diff())
|
|
T += 0.5 * m2 * (x2.diff()*x2.diff() + y2.diff()*y2.diff())
|
|
|
|
V = m1 * g * x1
|
|
V += m2 * g * x2
|
|
|
|
L = T - V
|
|
|
|
init = {
|
|
"theta1" : -3 * np.pi/4,
|
|
"d_theta1" : 0,
|
|
"theta2" : -np.pi/2,
|
|
"d_theta2" : 0
|
|
}
|
|
|
|
dt = 0.01
|
|
data = L.solve(init, 10, dt, True)
|
|
|
|
# Extraire les positions
|
|
times = sorted(data.keys())
|
|
positions = [data[t] for t in times]
|
|
|
|
x1_positions = [r1.value * np.sin(p["theta1"]) for p in positions]
|
|
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)]
|
|
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
|
|
fig, ax = plt.subplots(figsize=(8, 8))
|
|
ax.set_xlim(-3, 3)
|
|
ax.set_ylim(-3, 3)
|
|
ax.set_xlabel("X (m)")
|
|
ax.set_ylabel("Y (m)")
|
|
ax.grid(True)
|
|
ax.invert_yaxis()
|
|
|
|
line1, = ax.plot([], [], 'o-', lw=2, markersize=8, label='Pendule 1')
|
|
line2, = ax.plot([], [], 'o-', lw=2, markersize=8, label='Pendule 2')
|
|
trace, = ax.plot([], [], 'r-', alpha=0.3, lw=1, label='Trace')
|
|
|
|
def animate(frame):
|
|
line1.set_data([0, x1_display[frame]], [0, y1_display[frame]])
|
|
line2.set_data([x1_display[frame], x2_display[frame]], [y1_display[frame], y2_display[frame]])
|
|
trace.set_data(x2_display[:frame+1], y2_display[:frame+1])
|
|
return line1, line2, trace
|
|
|
|
ax.legend()
|
|
anim = animation.FuncAnimation(fig, animate, frames=len(x1_display), interval=40, blit=True, repeat=True)
|
|
|
|
plt.show() |