2026. június 13., szombat

A kettős inga szimulációja python prg.

A szimuláció futtatásához szükséges a numpy és matplotlib csomagok telepítése (pip install numpy matplotlib scipy).
----------------------------

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from matplotlib.animation import FuncAnimation

# Paraméterek és kezdeti feltételek
G = 9.81  # Gravitációs gyorsulás ($m/s^2$)
L1, L2 = 1.0, 1.0  # Inga karok hossza (m)
m1, m2 = 1.0, 1.0  # Inga testek tömege (kg)

def dX_dt(t, X):
    theta1, z1, theta2, z2 = X
    
    delta = theta1 - theta2
    
    # Egyenletek a szöggyorsulásokra
    den1 = (m1 + m2) * L1 - m2 * L1 * np.cos(delta)**2
    d2theta1 = (m2 * L1 * z1**2 * np.sin(delta) * np.cos(delta) +
                m2 * G * np.sin(theta2) * np.cos(delta) +
                m2 * L2 * z2**2 * np.sin(delta) -
                (m1 + m2) * G * np.sin(theta1)) / den1
                
    den2 = (m1 + m2) * L2 - m2 * L2 * np.cos(delta)**2
    d2theta2 = (-m2 * L2 * z2**2 * np.sin(delta) * np.cos(delta) +
                (m1 + m2) * G * np.sin(theta1) * np.cos(delta) -
                (m1 + m2) * L2 * z1**2 * np.sin(delta) -
                (m1 + m2) * G * np.sin(theta2)) / den2
    
    return [z1, d2theta1, z2, d2theta2]

# Kezdeti szögek és szögsebességek (radiánban)
theta1_0 = np.pi / 2
z1_0 = 0.0
theta2_0 = np.pi / 2
z2_0 = 0.0
X0 = [theta1_0, z1_0, theta2_0, z2_0]

# Időlépések (0-tól 20 másodpercig, 0.05 s lépésközzel)
t = np.linspace(0, 20, 400)

# Mozgásegyenletek integrálása
X = odeint(dX_dt, X0, t, tfirst=True)

# Deriváltak kinyerése a szögekhez
theta1, theta2 = X[:, 0], X[:, 2]

# Átszámolás Descartes-koordinátákba
x1 = L1 * np.sin(theta1)
y1 = -L1 * np.cos(theta1)
x2 = x1 + L2 * np.sin(theta2)
y2 = y1 - L2 * np.cos(theta2)

# Animáció beállítása
fig, ax = plt.subplots(figsize=(6,6))
ax.set_xlim(-2.5, 2.5)
ax.set_ylim(-2.5, 2.5)
ax.set_aspect('equal')
ax.grid()

line, = ax.plot([], [], 'o-', lw=2, color='blue')
trail, = ax.plot([], [], '-', lw=1, color='red', alpha=0.5)

def update(i):
    # Az aktuális pontok az ábrázoláshoz
    this_x = [0, x1[i], x2[i]]
    this_y = [0, y1[i], y2[i]]
    line.set_data(this_x, this_y)
    
    # Nyomvonal az utolsó néhány pontból
    trail.set_data(x2[:i], y2[:i])
    return line, trail

ani = FuncAnimation(fig, update, frames=len(t), interval=50, blit=True)
plt.title("Kettős inga szimuláció")
plt.show()
------------


Nincsenek megjegyzések:

Megjegyzés küldése