import numpy as np
import matplotlib.pyplot as plt

# Constants for the circuit
L = 90e-3  # Inductance in Henry
R = 20  # Resistance in Ohms
R1 = 50  # Additional resistance in Ohms
C = 40e-6  # Capacitance in Farads
E = 30  # Voltage in Volts

# Derived constants from the user's calculations
w = 1000 * (21.5)**0.5 / 9

# Define the time range for the transient analysis
t = np.linspace(0, 0.1, 1000)  # You can adjust the upper limit for longer time

# Voltage across the capacitor U_c(t)
U_c = (-30 * np.exp(-1000/9*t) * np.cos(w*t) -
       (30 * (86)**0.5) / 43 * np.exp(-1000/9*t) * np.sin(w*t) + 30)

# Current through the inductor i_L(t)
i_L = (3 * (86)**0.5) / 43 * np.exp(-1000/9*t) * np.sin(w*t)

# Current through R1 i_1(t)
i_1 = (L * (-1000/9 * (3*(86)**0.5)/43 * np.exp(-1000/9*t) * np.sin(w*t) +
             1000 * (21.5)**0.5 / 9 * (3*(86)**0.5)/43 * np.exp(-1000/9*t) * np.cos(w*t)) / R1 +
       R * ((3*(86)**0.5)/43 * np.exp(-1000/9*t) * np.sin(w*t)) / R1)

# Total current i(t)
i = (1.8 * 10**-3 * (-1000/9 * (3*(86)**0.5)/43 * np.exp(-1000/9*t) * np.sin(w*t) +
                      1000 * (21.5)**0.5 / 9 * (3*(86)**0.5)/43 * np.exp(-1000/9*t) * np.cos(w*t)) +
     0.4 * ((3*(86)**0.5)/43 * np.exp(-1000/9*t) * np.sin(w*t)) +
     ((3*(86)**0.5)/43 * np.exp(-1000/9*t) * np.sin(w*t)))

# Plotting all four graphs in a 2x2 subplot
plt.figure(figsize=(14, 10))

# Voltage across the capacitor U_c(t)
plt.subplot(221)
plt.plot(t, U_c, label='$U_c(t)$')
plt.title('Напряжение на конденсаторе')
plt.xlabel('Время (с)')
plt.ylabel('Напряжение (В)')
plt.grid(True)
plt.legend()

# Current through the inductor i_L(t)
plt.subplot(222)
plt.plot(t, i_L, label='$i_L(t)$', color='orange')
plt.title('Ток через индуктор')
plt.xlabel('Время (с)')
plt.ylabel('Ток (А)')
plt.grid(True)
plt.legend()

# Current through R1 i_1(t)
plt.subplot(223)
plt.plot(t, i_1, label='$i_1(t)$', color='red')
plt.title('Ток через R1')
plt.xlabel('Время (с)')
plt.ylabel('Ток (А)')
plt.grid(True)
plt.legend()

# Total current i(t)
plt.subplot(224)
plt.plot(t, i, label='$i(t)$', color='green')
plt.title('Общий ток')
plt.xlabel('Время (с)')
plt.ylabel('Ток (А)')
plt.grid(True)
plt.legend()

# Adjust layout to prevent overlap
plt.tight_layout()

# Show the plots
plt.show()
