# -*- coding: utf-8 -*-
# Created on Sat Sep 31 08:15:00 2024
# @author: mhebding
# simulations des courbes en RSF pour le circuit RLC serie

import matplotlib.pyplot as plt
from math import pi
import numpy as np

# Parametres
E = 5
f = 7341 # modifier

T = 1/f
w = 2*pi*f

R = 132
L = 0.1
C = 4.7*10**-9

w0 = 1/(L*C)**0.5
Q = 1/R*(L/C)**0.5
print('f0 = ', round(w0/(2*pi),0), 'Hz')
print('Q = ', round(Q,0))

# liste des temps 1000 points sur 3 periodes
t = np.linspace(0, 3*T, 1000)

# Calculs complexes
ZR = R
ZL = 1j*L*w
ZC = 1/(1j*C*w)
Zeq = ZR + ZL + ZC

e_ = E*np.exp(1j*w*t)
i_ = e_/Zeq
uC_ = ZC/Zeq*e_ # ponts diviseurs de tension
uL_ = ZL/Zeq*e_
uR_ = ZR/Zeq*e_

# remarque on peut calculer l'amplitude et le dephasage par :
# I=np.absolute(i_) et phiI=np.angle(i_)

def graphe(y1, y2): # trace y1 et y2 en fonction du temps a partir des expressions complexes 
    y1 = np.real(y1) # i(t) = Re(i_)
    y2 = np.real(y2)
    fig, ax1 = plt.subplots() # Affichage des données de l'axe principal
    ax1.plot(t, y1, color='tab:blue')
    ax1.tick_params(axis='y', labelcolor='tab:blue')
    ax2 = ax1.twinx()  # Affichage de l'axe secondaire # Création d'un deuxième axe qui partage le même axe x 
    ax2.plot(t, y2, color='tab:orange')
    ax2.tick_params(axis='y', labelcolor='tab:orange')
    plt.show()

# exemple : graphe(e_, i_) et jouer sur la frequence en haut