Skip to main content

2.3 Energiya va Impuls — Amaliyot

Laboratoriya ishi: Python da energiya va to'qnashuv simulyatsiyasi


1. Energiya Saqlanishi Simulyatsiyasi​

import numpy as np
import matplotlib.pyplot as plt

def energy_conservation_demo():
"""Erkin tushish — energiya saqlanishi"""

# Parametrlar
m = 2 # kg
h0 = 50 # m
g = 10 # m/s²
dt = 0.01 # s

# Boshlang'ich holatlar
y = h0
v = 0

# History
t_list = [0]
y_list = [h0]
v_list = [0]
KE_list = [0]
PE_list = [m * g * h0]
E_total = [m * g * h0]

t = 0
while y > 0:
# Harakat tenglamalari
a = -g
v += a * dt
y += v * dt
t += dt

# Energiyalar
KE = 0.5 * m * v**2
PE = m * g * max(0, y)

t_list.append(t)
y_list.append(max(0, y))
v_list.append(v)
KE_list.append(KE)
PE_list.append(PE)
E_total.append(KE + PE)

# Plot
fig, axes = plt.subplots(2, 2, figsize=(14, 10))

axes[0, 0].plot(t_list, y_list, 'b-', linewidth=2)
axes[0, 0].set_xlabel('Vaqt (s)')
axes[0, 0].set_ylabel('Balandlik (m)')
axes[0, 0].set_title('Pozitsiya')
axes[0, 0].grid(True)

axes[0, 1].plot(t_list, v_list, 'r-', linewidth=2)
axes[0, 1].set_xlabel('Vaqt (s)')
axes[0, 1].set_ylabel('Tezlik (m/s)')
axes[0, 1].set_title('Tezlik')
axes[0, 1].grid(True)

axes[1, 0].plot(t_list, KE_list, 'g-', linewidth=2, label='Kinetik')
axes[1, 0].plot(t_list, PE_list, 'b-', linewidth=2, label='Potensial')
axes[1, 0].set_xlabel('Vaqt (s)')
axes[1, 0].set_ylabel('Energiya (J)')
axes[1, 0].set_title('KE va PE')
axes[1, 0].legend()
axes[1, 0].grid(True)

axes[1, 1].plot(t_list, E_total, 'm-', linewidth=2)
axes[1, 1].set_xlabel('Vaqt (s)')
axes[1, 1].set_ylabel('Umumiy E (J)')
axes[1, 1].set_title('Umumiy Energiya (const)')
axes[1, 1].set_ylim([0, max(E_total) * 1.1])
axes[1, 1].grid(True)

plt.tight_layout()
plt.savefig('energy_conservation.png', dpi=150)
plt.show()

print(f"Boshlang'ich PE: {PE_list[0]:.1f} J")
print(f"Oxirgi KE: {KE_list[-1]:.1f} J")
print(f"Energiya xatosi: {abs(PE_list[0] - KE_list[-1]):.3f} J")

energy_conservation_demo()

2. Prujina-Massa Tizimi​

import numpy as np
import matplotlib.pyplot as plt

def spring_mass_energy():
"""Prujina-massa energiya almashinuvi"""

m = 1 # kg
k = 100 # N/m
A = 0.2 # m (amplituda)

omega = np.sqrt(k / m)
T = 2 * np.pi / omega

t = np.linspace(0, 3*T, 500)

# Harakat
x = A * np.cos(omega * t)
v = -A * omega * np.sin(omega * t)

# Energiyalar
KE = 0.5 * m * v**2
PE = 0.5 * k * x**2
E_total = KE + PE

# Plot
fig, axes = plt.subplots(2, 1, figsize=(12, 8))

axes[0].plot(t, x, 'b-', linewidth=2, label='Pozitsiya')
axes[0].plot(t, v/omega, 'r--', linewidth=2, label='Tezlik (scaled)')
axes[0].set_ylabel('x (m), v/ω (m)')
axes[0].set_title('Prujina-Massa Tebranishi')
axes[0].legend()
axes[0].grid(True)

axes[1].fill_between(t, 0, KE, alpha=0.5, label='Kinetik E')
axes[1].fill_between(t, KE, KE+PE, alpha=0.5, label='Potensial E')
axes[1].plot(t, E_total, 'k-', linewidth=2, label='Umumiy E')
axes[1].set_xlabel('Vaqt (s)')
axes[1].set_ylabel('Energiya (J)')
axes[1].set_title('Energiya Almashinuvi')
axes[1].legend()
axes[1].grid(True)

plt.tight_layout()
plt.savefig('spring_mass_energy.png', dpi=150)
plt.show()

spring_mass_energy()

3. 1D To'qnashuv Simulyatsiyasi​

import numpy as np
import matplotlib.pyplot as plt

class Particle1D:
def __init__(self, mass, x, v):
self.m = mass
self.x = x
self.v = v
self.history_x = [x]
self.history_v = [v]

def update(self, dt):
self.x += self.v * dt
self.history_x.append(self.x)
self.history_v.append(self.v)

def elastic_collision(p1, p2):
"""1D elastik to'qnashuv"""
m1, m2 = p1.m, p2.m
v1, v2 = p1.v, p2.v

p1.v = ((m1 - m2) * v1 + 2 * m2 * v2) / (m1 + m2)
p2.v = ((m2 - m1) * v2 + 2 * m1 * v1) / (m1 + m2)

def inelastic_collision(p1, p2, e=0):
"""Noelastik to'qnashuv (e = restitution coefficient)"""
m1, m2 = p1.m, p2.m
v1, v2 = p1.v, p2.v

# Impuls saqlanishi + restitution
v_cm = (m1 * v1 + m2 * v2) / (m1 + m2)

p1.v = v_cm + e * m2 * (v2 - v1) / (m1 + m2)
p2.v = v_cm + e * m1 * (v1 - v2) / (m1 + m2)

def collision_simulation(collision_type='elastic'):
# Ikkita zarracha
p1 = Particle1D(mass=2, x=0, v=5)
p2 = Particle1D(mass=3, x=8, v=-2)

dt = 0.01
t_max = 4
t = 0
t_list = [0]
collided = False

while t < t_max:
# To'qnashuvni tekshirish
if not collided and p1.x >= p2.x:
collided = True
if collision_type == 'elastic':
elastic_collision(p1, p2)
else:
inelastic_collision(p1, p2, e=0) # to'liq noelastik

p1.update(dt)
p2.update(dt)
t += dt
t_list.append(t)

# Energiya hisoblash
KE_initial = 0.5 * p1.m * 5**2 + 0.5 * p2.m * (-2)**2
KE_final = 0.5 * p1.m * p1.v**2 + 0.5 * p2.m * p2.v**2

# Plot
plt.figure(figsize=(12, 5))
plt.plot(t_list, p1.history_x, 'b-', linewidth=2, label=f'm₁={p1.m}kg')
plt.plot(t_list, p2.history_x, 'r-', linewidth=2, label=f'mā‚‚={p2.m}kg')
plt.xlabel('Vaqt (s)')
plt.ylabel('Pozitsiya (m)')
plt.title(f'{collision_type.capitalize()} To\'qnashuv\n'
f'KE_init={KE_initial:.1f}J, KE_final={KE_final:.1f}J')
plt.legend()
plt.grid(True)
plt.savefig(f'{collision_type}_collision.png', dpi=150)
plt.show()

return KE_initial, KE_final

# Elastik
print("=== Elastik To'qnashuv ===")
KEi, KEf = collision_simulation('elastic')
print(f"Energiya saqlanishi: {KEf/KEi*100:.1f}%")

# Noelastik
print("\n=== Noelastik To'qnashuv ===")
KEi, KEf = collision_simulation('inelastic')
print(f"Energiya saqlanishi: {KEf/KEi*100:.1f}%")

4. 2D To'qnashuv (Bilyard)​

import numpy as np
import matplotlib.pyplot as plt

class Ball2D:
def __init__(self, mass, pos, vel, radius=0.5):
self.m = mass
self.pos = np.array(pos, dtype=float)
self.vel = np.array(vel, dtype=float)
self.r = radius
self.history = [self.pos.copy()]

def update(self, dt):
self.pos += self.vel * dt
self.history.append(self.pos.copy())

def elastic_collision_2d(b1, b2):
"""2D elastik to'qnashuv"""
# Normal vektor
n = b2.pos - b1.pos
n = n / np.linalg.norm(n)

# Relative velocity
v_rel = b1.vel - b2.vel

# Relative velocity in collision normal
v_n = np.dot(v_rel, n)

# Yaqinlashayotganini tekshirish
if v_n > 0:
return

# Impuls
j = -(1 + 1) * v_n / (1/b1.m + 1/b2.m) # e=1 elastik

b1.vel += (j / b1.m) * n
b2.vel -= (j / b2.m) * n

def billiard_simulation():
# Ikki shar
ball1 = Ball2D(mass=1, pos=[0, 0], vel=[5, 1], radius=0.5)
ball2 = Ball2D(mass=1, pos=[6, 0.3], vel=[0, 0], radius=0.5)

dt = 0.01
t_max = 3
collided = False

for _ in np.arange(0, t_max, dt):
# To'qnashuvni tekshirish
dist = np.linalg.norm(ball1.pos - ball2.pos)
if not collided and dist <= ball1.r + ball2.r:
collided = True
elastic_collision_2d(ball1, ball2)

ball1.update(dt)
ball2.update(dt)

# Plot
traj1 = np.array(ball1.history)
traj2 = np.array(ball2.history)

plt.figure(figsize=(12, 6))
plt.plot(traj1[:, 0], traj1[:, 1], 'b-', linewidth=2, label='Ball 1')
plt.plot(traj2[:, 0], traj2[:, 1], 'r-', linewidth=2, label='Ball 2')

# Boshlang'ich va oxirgi pozitsiyalar
plt.scatter([0, 6], [0, 0.3], s=200, c=['blue', 'red'], marker='o', alpha=0.3)
plt.scatter([traj1[-1, 0], traj2[-1, 0]],
[traj1[-1, 1], traj2[-1, 1]],
s=200, c=['blue', 'red'], marker='o')

plt.xlabel('X (m)')
plt.ylabel('Y (m)')
plt.title('2D Elastik To\'qnashuv (Bilyard)')
plt.legend()
plt.axis('equal')
plt.grid(True)
plt.savefig('billiard_collision.png', dpi=150)
plt.show()

billiard_simulation()

5. Raketa Impuls Simulyatsiyasi​

import numpy as np
import matplotlib.pyplot as plt

def rocket_simulation():
"""Tsiolkovsky raketa tenglamasi simulyatsiyasi"""

# Parametrlar
m0 = 1000 # kg (boshlang'ich massa)
m_prop = 800 # kg (yonilg'i massasi)
v_exhaust = 3000 # m/s (exhaust tezligi)
burn_rate = 10 # kg/s
g = 10 # m/s²

dt = 0.1
burn_time = m_prop / burn_rate
total_time = burn_time + 50

t_list = [0]
m_list = [m0]
v_list = [0]
h_list = [0]
thrust_list = [0]

t = 0
m = m0
v = 0
h = 0

while t < total_time and h >= 0:
t += dt

if m > m0 - m_prop:
# Yonish davri
dm = burn_rate * dt
m -= dm
thrust = v_exhaust * burn_rate

# Tezlanish
a = thrust / m - g
else:
# Yonish tugadi
thrust = 0
a = -g

v += a * dt
h += v * dt

t_list.append(t)
m_list.append(m)
v_list.append(v)
h_list.append(max(0, h))
thrust_list.append(thrust)

# Tsiolkovsky formula
delta_v_theory = v_exhaust * np.log(m0 / (m0 - m_prop))
print(f"Tsiolkovsky delta-v (gravitatsiyasiz): {delta_v_theory:.0f} m/s")
print(f"Simulyatsiya max tezlik: {max(v_list):.0f} m/s")
print(f"Max balandlik: {max(h_list):.0f} m")

# Plot
fig, axes = plt.subplots(2, 2, figsize=(14, 10))

axes[0, 0].plot(t_list, h_list, 'b-', linewidth=2)
axes[0, 0].set_ylabel('Balandlik (m)')
axes[0, 0].set_title('Raketa Uchishi')
axes[0, 0].grid(True)

axes[0, 1].plot(t_list, v_list, 'r-', linewidth=2)
axes[0, 1].set_ylabel('Tezlik (m/s)')
axes[0, 1].set_title('Tezlik')
axes[0, 1].grid(True)

axes[1, 0].plot(t_list, m_list, 'g-', linewidth=2)
axes[1, 0].set_xlabel('Vaqt (s)')
axes[1, 0].set_ylabel('Massa (kg)')
axes[1, 0].set_title('Massa')
axes[1, 0].grid(True)

axes[1, 1].plot(t_list, thrust_list, 'm-', linewidth=2)
axes[1, 1].set_xlabel('Vaqt (s)')
axes[1, 1].set_ylabel('Thrust (N)')
axes[1, 1].set_title('Itarish Kuchi')
axes[1, 1].grid(True)

plt.tight_layout()
plt.savefig('rocket_simulation.png', dpi=150)
plt.show()

rocket_simulation()

6. Ballistik Mayatnik​

import numpy as np
import matplotlib.pyplot as plt

def ballistic_pendulum(m_bullet, v_bullet, m_block, L):
"""
Ballistik mayatnik: o'q blokka kirib qoladi
"""
# 1-bosqich: Noelastik to'qnashuv (impuls saqlanishi)
v_combined = m_bullet * v_bullet / (m_bullet + m_block)

# 2-bosqich: Ko'tarilish (energiya saqlanishi)
g = 10
h = v_combined**2 / (2 * g)

# Burchak
if h < L:
theta = np.arccos(1 - h/L)
else:
theta = np.pi # To'liq aylansa

return v_combined, h, np.degrees(theta)

# Simulyatsiya
m_bullet = 0.01 # 10 g
m_block = 2 # 2 kg
L = 1 # 1 m ip
v_bullets = np.linspace(100, 1000, 10)

heights = []
angles = []

for v in v_bullets:
_, h, theta = ballistic_pendulum(m_bullet, v, m_block, L)
heights.append(h)
angles.append(theta)

# Plot
fig, axes = plt.subplots(1, 2, figsize=(12, 5))

axes[0].plot(v_bullets, heights, 'bo-', linewidth=2)
axes[0].set_xlabel('O\'q tezligi (m/s)')
axes[0].set_ylabel('Ko\'tarilish balandligi (m)')
axes[0].set_title('Ballistik Mayatnik')
axes[0].grid(True)

axes[1].plot(v_bullets, angles, 'ro-', linewidth=2)
axes[1].set_xlabel('O\'q tezligi (m/s)')
axes[1].set_ylabel('Burchak (°)')
axes[1].set_title('Og\'ish Burchagi')
axes[1].grid(True)

plt.tight_layout()
plt.savefig('ballistic_pendulum.png', dpi=150)
plt.show()

# Teskari masala: burchakdan tezlikni topish
theta_measured = 30 # degrees
h = L * (1 - np.cos(np.radians(theta_measured)))
v_combined = np.sqrt(2 * 10 * h)
v_bullet_calc = v_combined * (m_bullet + m_block) / m_bullet

print(f"\nTeskari masala:")
print(f"Burchak: {theta_measured}°")
print(f"Ko'tarilish: {h:.3f} m")
print(f"O'q tezligi: {v_bullet_calc:.0f} m/s")

7. Topshiriq: Dron Qo'nishi​

def drone_landing():
"""
Dron soft landing simulyatsiyasi

Maqsad: 10 m balandlikdan 0.5 m/s dan kam tezlikda qo'nish

TODO:
1. Energiya hisobini qo'shing
2. Optimal thrust profilini toping
3. Energiya sarfini minimallashtiring
"""

m = 2 # kg
g = 10 # m/s²
h0 = 10 # m
v_max_land = 0.5 # m/s

# Sizning kodingiz...
pass

# drone_landing()

āœ… Laboratoriya Tekshirish Ro'yxati​

  • Energiya saqlanishi (erkin tushish)
  • Prujina-massa energiya almashinuvi
  • 1D to'qnashuv (elastik va noelastik)
  • 2D to'qnashuv (bilyard)
  • Raketa simulyatsiyasi
  • Ballistik mayatnik
  • Dron qo'nishi topshiriq

Keyingi Mavzu​

šŸ“– 2.4 Tebranishlar