
import numpy as np
from matplotlib.pyplot import *
from scipy.integrate import odeint
                                    
	
def periode(L,r,C,re,R,T,alpha,Ve,t0,ie0,i0,N=100):
	def systeme1(Y,t): # Y[0] : ie, Y[1] : i
		dY0 = (Y[1]-Y[0])/(re*C)
		dY1 = (Ve-re*Y[0]-(r+R)*Y[1])/L
		return [dY0,dY1]
	temps1 = np.linspace(t0,t0+T*alpha,N//2)
	sol = odeint(systeme1,[ie0,i0],temps1,rtol=1e-4,atol=1e-4)
	ie1 = sol[:,0]
	i1 = sol[:,1]
	def systeme2(Y,t):
		dY0 = (-Y[0])/(re*C)
		dY1 = (-(r+R)*Y[1])/L
		return [dY0,dY1]
	temps2 = np.linspace(t0+T*alpha,t0+T,N//2)
	sol = odeint(systeme2,[ie1[-1],i1[-1]],temps2,rtol=1e-4,atol=1e-4)
	ie2 = sol[:,0]
	i2 = sol[:,1]
	temps = np.concatenate((temps1,temps2))
	ie = np.concatenate((ie1,ie2))
	i =  np.concatenate((i1,i2))
	return temps,ie,i
                                    

def simulation(L,r,C,re,R,T,alpha,Ve,t0,ie0,i0,P,N=100):
    tab_temps = np.zeros(N*P)
    tab_ie = np.zeros(N*P)
    tab_i = np.zeros(N*P)
    k = 0
    for p in range(P):
        temps,ie,i = periode(L,r,C,re,R,T,alpha,Ve,t0,ie0,i0,N)
        ie0 = ie[-1]
        i0 = i[-1]
        t0 += T
        tab_temps[k:k+N] = temps
        tab_ie[k:k+N] = ie
        tab_i[k:k+N] = i
        k += N
    return tab_temps,tab_ie,tab_i
                                    

L = 0.5e-3
r = 1
C = 2000e-6
R = 10
Ve = 10
re = 1
alpha = 0.5
ie0 = Ve/re
i0 = 0
t0 = 0
T = 5e-5 # 20 kHz
P = 1500
temps,ie,i = simulation(L,r,C,re,R,T,alpha,Ve,t0,ie0,i0,P)
figure(figsize=(16,8))
subplot(211)
title(r"$V_e=10\ {\rm V},\ \alpha=0{,}5$",fontsize=16)
plot(temps,ie)
grid()
ylabel(r"$i_e\ (\rm A)$",fontsize=16)
subplot(212)
plot(temps,R*i)
grid()
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)
                                    

figure(figsize=(16,8))
subplot(211)
title(r"$V_e=10\ {\rm V},\ \alpha=0{,}5$",fontsize=16)
plot(temps,ie)
xlim(0.02,0.021)
ylim(0,0.5)
grid()
ylabel(r"$i_e\ (\rm A)$",fontsize=16)
subplot(212)
plot(temps,R*i)
xlim(0.02,0.021)
grid()
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)									
									

def ondulation(x):
	N = len(x)
	x = x[N//2:N]
	return (x.max()-x.min())/x.mean()
	
ondulVs = ondulation(R*i)
									

Cs = 1000e-6
f = np.logspace(2,6,1000)
w = f*2*np.pi
H1 = R/(r+R+1j*L*w)
GdB1 = 20*np.log10(np.absolute(H1))
H2 = 1/(1+(r+1j*L*w)*(1/R+1j*Cs*w))
GdB2 = 20*np.log10(np.absolute(H2))
figure()
plot(f,GdB1,label="L")
plot(f,GdB2,label="LC")
legend(loc="upper right")
xscale('log')
grid()
xlabel("f (Hz)",fontsize=16)
ylabel("GdB",fontsize=16)
			 

def periode(L,r,C,re,R,Cs,T,alpha,Ve,t0,ie0,i20,i0,N=100):
	def systeme1(Y,t): # Y[0] : ie, Y[1] : i2, Y[2] : i
		dY0 = (Y[2]-Y[0])/(re*C)
		dY1 = (Ve-re*Y[0]-r*Y[1]-R*Y[2])/L
		dY2 = (Y[1]-Y[2])/(Cs*R)
		return [dY0,dY1,dY2]
	temps1 = np.linspace(t0,t0+T*alpha,N//2)
	sol = odeint(systeme1,[ie0,i20,i0],temps1,rtol=1e-4,atol=1e-4)
	ie_1 = sol[:,0]
	i2_1 = sol[:,1]
	i_1 = sol[:,2]
	def systeme2(Y,t):
		dY0 = (-Y[0])/(re*C)
		dY1 = (-r*Y[1]-R*Y[2])/L
		dY2 = (Y[1]-Y[2])/(Cs*R)
		return [dY0,dY1,dY2]
	temps2 = np.linspace(t0+T*alpha,t0+T,N//2)
	sol = odeint(systeme2,[ie_1[-1],i2_1[-1],i_1[-1]],temps2,rtol=1e-4,atol=1e-4)
	ie_2 = sol[:,0]
	i2_2 = sol[:,1]
	i_2 = sol[:,2]
	temps = np.concatenate((temps1,temps2))
	ie = np.concatenate((ie_1,ie_2))
	i2 =  np.concatenate((i2_1,i2_2))
	i = np.concatenate((i_1,i_2))
	return temps,ie,i2,i
                                    

def simulation(L,r,C,re,R,Cs,T,alpha,Ve,t0,ie0,i20,i0,P,N=100):
	tab_temps = np.zeros(N*P)
	tab_ie = np.zeros(N*P)
	tab_i2 = np.zeros(N*P)
	tab_i = np.zeros(N*P)
	k = 0
	for p in range(P):
		temps,ie,i2,i = periode(L,r,C,re,R,Cs,T,alpha,Ve,t0,ie0,i20,i0,N)
		ie0 = ie[-1]
		i20 = i2[-1]
		i0 = i[-1]
		t0 += T
		tab_temps[k:k+N] = temps
		tab_ie[k:k+N] = ie
		tab_i2[k:k+N] = i2
		tab_i[k:k+N] = i
		k += N
	return tab_temps,tab_ie,tab_i2,tab_i
                                    

L = 0.5e-3
r = 1
C = 2000e-6
R = 10
Cs = 1000e-6
Ve = 10
re = 1
alpha = 0.5
T = 5e-5 # 20 kHz
P = 1500
temps,ie,i2,i = simulation(L,r,C,re,R,Cs,T,alpha,Ve,0,Ve/re,0,0,P)
figure(figsize=(16,8))
subplot(211)
title(r"$V_e=10\ {\rm V},\ \alpha=0{,}5$",fontsize=16)
plot(temps,ie,label=r"$i_e$")
plot(temps,i2,label=r"$i_2$")
legend(loc="upper right")
grid()
ylabel(r"$i\ (\rm A)$",fontsize=16)
subplot(212)
plot(temps,R*i)
grid()
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)
                                    

ondulVs = ondulation(R*i)
									

L = 0.5e-3
r = 1
C = 2000e-6
R = 200
Cs = 1000e-6
Ve = 10
re = 1
alpha = 0.5
T = 5e-5 # 20 kHz
P = 1500
temps,ie,i2,i = simulation(L,r,C,re,R,Cs,T,alpha,Ve,0,Ve/re,0,0,P)
figure(figsize=(16,8))
subplot(211)
title(r"$V_e=10\ {\rm V},\ \alpha=0{,}5$",fontsize=16)
plot(temps,ie,label=r"$i_e$")
plot(temps,i2,label=r"$i_2$")
legend(loc="upper right")
grid()
ylabel(r"$i\ (\rm A)$",fontsize=16)
subplot(212)
plot(temps,R*i)
grid()
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)
                                    

ondulVs = ondulation(R*i)
									

figure(figsize=(16,6))
title(r"$V_e=10\ {\rm V},\ \alpha=0{,}5$",fontsize=16)
plot(temps,ie,label=r"$i_e$")
plot(temps,i2,label=r"$i_2$")
legend(loc="upper right")
grid()
ylabel(r"$i\ (\rm A)$",fontsize=16)
xlim(0.02,0.0202)
ylim(-0.2,0.2)		
									
                                     
def solution(valProp,vectProp,Yp,t0,t1,Yp0,N):
	Va = vectProp[:,0]
	Vb = vectProp[:,1]
	Vc = vectProp[:,2]
	lamb_a = valProp[0]
	lamb_b = valProp[1]
	lamb_c = valProp[2]
	ea = np.exp(lamb_a*t0)
	eb = np.exp(lamb_b*t0)
	ec = np.exp(lamb_c*t0)
	D = np.array([[ea*Va[0],eb*Vb[0],ec*Vc[0]],[ea*Va[1],eb*Vb[1],ec*Vc[1]],[ea*Va[2],eb*Vb[2],ec*Vc[2]]])
	E = np.array([Yp0[0]-Yp[0],Yp0[1]-Yp[1],Yp0[2]-Yp[2]])
	C = solve(D,E)
	t = np.linspace(t0,t1,N)
	a_a = C[0]*np.exp(lamb_a*t)
	a_b = C[1]*np.exp(lamb_b*t)
	a_c = C[2]*np.exp(lamb_c*t)
	Y0 = a_a*Va[0]+a_b*Vb[0]+a_c*Vc[0]+Yp[0]
	Y1 = a_a*Va[1]+a_b*Vb[1]+a_c*Vc[1]+Yp[1]
	Y2 = a_a*Va[2]+a_b*Vb[2]+a_c*Vc[2]+Yp[1]
	return t,[Y0,Y1,Y2]
                
              
def periode2(valProp1,vectProp1,valProp2,vectProp2,Yp1,Yp2,T,alpha,Ve,t,ie0,i20,i0,N=100):
	temps1,Y1 = solution(valProp1,vectProp1,Yp1,0,alpha*T,[ie0,i20,i0],int(alpha*N))
	ie_1 = Y1[0]
	i2_1 = Y1[1]
	i_1 = Y1[2]
	temps2,Y2 = solution(valProp2,vectProp2,Yp2,alpha*T,T,[ie_1[-1],i2_1[-1],i_1[-1]],N-int(alpha*N))
	ie_2 = Y2[0]
	i2_2 = Y2[1]
	i_2 = Y2[2]
	temps = np.concatenate((t+temps1,t+temps2))
	ie = np.concatenate((ie_1,ie_2))
	i2 = np.concatenate((i2_1,i2_2))
	i = np.concatenate((i_1,i_2))
	return temps,np.real(ie),np.real(i2),np.real(i)   
    

from numpy.linalg import solve,eig

def simulation2(L,r,C,re,R,Cs,T,alpha,Ve,t0,ie0,i20,i0,P,N=100):
	tab_temps = np.zeros(N*P)
	tab_ie = np.zeros(N*P)
	tab_i2 = np.zeros(N*P)
	tab_i = np.zeros(N*P)
	A1 = np.array([[-1/(re*C),0,1/(re*C)],[-re/L,-r/L,-R/L],[0,1/(R*Cs),-1/(R*Cs)]])
	B1 = np.array([0,Ve/L,0])
	Yp1 = solve(A1,-B1)
	valProp1, vectProp1 = eig(A1)
	A2 = np.array([[-1/(re*C),0,0],[0,-r/L,-R/L],[0,1/(R*Cs),-1/(R*Cs)]])
	B2 = np.array([0,0,0])
	Yp2 = solve(A2,-B2)
	valProp2, vectProp2 = eig(A2)
	k = 0
	t = t0
	for p in range(P):
		temps,ie,i2,i = periode2(valProp1,vectProp1,valProp2,vectProp2,Yp1,Yp2,T,alpha,Ve,t,ie0,i20,i0,N)
		ie0 = ie[-1]
		i20 = i2[-1]
		i0 = i[-1]
		t += T
		tab_temps[k:k+N] = temps
		tab_ie[k:k+N] = ie
		tab_i2[k:k+N] = i2
		tab_i[k:k+N] = i
		k += N
	return tab_temps,tab_ie,tab_i2,tab_i
    
    

L = 0.5e-3
r = 1
C = 2000e-6
R = 10
Cs = 1000e-6
Ve = 10
re = 1
alpha = 0.5
T = 5e-5 # 20 kHz
P = 1500
temps,ie,i2,i = simulation2(L,r,C,re,R,Cs,T,alpha,Ve,0,Ve/re,0,0,P)
figure(figsize=(16,8))
subplot(211)
title(r"$V_e=10\ {\rm V},\ \alpha=0{,}5$",fontsize=16)
plot(temps,ie)
grid()
ylabel(r"$i_e\ (\rm A)$",fontsize=16)
subplot(212)
plot(temps,R*i)
grid()
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)
                                    

ondulVs = ondulation(R*i)
									

figure(figsize=(16,6))
plot(temps,ie,label="ie")
plot(temps,i2,label="i2")
grid()
xlabel("t (s)",fontsize=16)
ylabel("i (A)",fontsize=16)
legend(loc="upper right")
ylim(0,2)
									

L = 0.5e-3
r = 1
C = 2000e-6
R = 10
Cs = 1000e-6
Ve = 10
re = 1
T = 5e-5 # 20 kHz
P = 500
figure(figsize=(16,6))
alpha = 0.2
temps,ie,i2,i = simulation2(L,r,C,re,R,Cs,T,alpha,Ve,0,Ve/re,0,0,P)
plot(temps,R*i)
alpha = 0.8
temps,ie,i2,i = simulation2(L,r,C,re,R,Cs,T,alpha,Ve,temps[-1],ie[-1],i2[-1],i[-1],P)
plot(temps,R*i)
alpha = 0.2
temps,ie,i2,i = simulation2(L,r,C,re,R,Cs,T,alpha,Ve,temps[-1],ie[-1],i2[-1],i[-1],P)
plot(temps,R*i)
grid()
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)
title(r"$V_e=10\ {\rm V},\ \alpha=0{,}2\rightarrow 0{,}8\rightarrow 0{,}2$",fontsize=16)
									 

gain = []
alpha = np.linspace(0.01,0.99,30)
L = 0.5e-3
r = 1
C = 2000e-6
R = 10
Cs = 1000e-6
Ve = 10
re = 1
T = 5e-5 # 20 kHz
P = 1500
for k in range(len(alpha)):
	temps,ie,i2,i = simulation2(L,r,C,re,R,Cs,T,alpha[k],Ve,0,Ve/re,0,0,P)
	Vs = R*i
	N = len(Vs)
	Vs = Vs[N//2:N]
	gain.append(Vs.mean()/Ve)
figure(figsize=(8,6))
title(r"$R=10\,\rm\Omega$",fontsize=16)
plot(alpha,gain)
xlim(0,1)
ylim(0,1)
plot([0,1],[0,R/(R+r)],"k--")
grid()
xlabel(r"$\alpha$",fontsize=16)
ylabel(r"$\frac{Vs}{Ve}$",fontsize=16)
			

gain = []
alpha = np.linspace(0.01,0.99,30)
L = 0.5e-3
r = 1
C = 2000e-6
R = 100
Cs = 1000e-6
Ve = 10
re = 1
T = 5e-5 # 20 kHz
P = 1500
for k in range(len(alpha)):
	temps,ie,i2,i = simulation2(L,r,C,re,R,Cs,T,alpha[k],Ve,0,Ve/re,0,0,P)
	Vs = R*i
	N = len(Vs)
	Vs = Vs[N//2:N]
	gain.append(Vs.mean()/Ve)
figure(figsize=(8,6))
title(r"$R=100\,\rm\Omega$",fontsize=16)
plot(alpha,gain)
xlim(0,1)
ylim(0,1)
plot([0,1],[0,R/(R+r)],"k--")
grid()
xlabel(r"$\alpha$",fontsize=16)
ylabel(r"$\frac{Vs}{Ve}$",fontsize=16)
			

gain = 	[]
r = np.logspace(-2,1,10)
alpha = 0.5
L = 0.5e-3
C = 2000e-6
R = 10
Cs = 1000e-6
Ve = 10
re = 1
T = 5e-5 # 20 kHz
P = 1500
for k in range(len(r)):
	temps,ie,i2,i = simulation2(L,r[k],C,re,R,Cs,T,alpha,Ve,0,Ve/re,0,0,P)
	Vs = R*i
	N = len(Vs)
	Vs = Vs[N//2:N]
	gain.append(Vs.mean()/Ve)
figure(figsize=(8,6))
title(r"$\alpha=0,5$",fontsize=16)
plot(r,gain)
ylim(0,1)
xscale('log')
grid()
xlabel(r"$r\ (\rm\Omega)$",fontsize=16)
ylabel(r"$\frac{Vs}{Ve}$",fontsize=16)
			

gain = 	[]
re = np.logspace(-2,1,10)
alpha = 0.5
L = 0.5e-3
r = 1
C = 2000e-6
R = 10
Cs = 1000e-6
Ve = 10
T = 5e-5 # 20 kHz
P = 1500
for k in range(len(re)):
	temps,ie,i2,i = simulation2(L,r,C,re[k],R,Cs,T,alpha,Ve,0,Ve/re[k],0,0,P)
	Vs = R*i
	N = len(Vs)
	Vs = Vs[N//2:N]
	gain.append(Vs.mean()/Ve)
figure(figsize=(8,6))
title(r"$\alpha=0,5$",fontsize=16)
plot(re,gain)
ylim(0,1)
xscale('log')
grid()
xlabel(r"$r_e\ (\rm\Omega)$",fontsize=16)
ylabel(r"$\frac{V_s}{V_e}$",fontsize=16)
			

def rendement(L,r,C,re,R,Cs,T,alpha,P):
	Ve = 1
	temps,ie,i2,i = simulation2(L,r,C,re,R,Cs,T,alpha,Ve,0,Ve/re,0,0,P)
	Vs = R*i
	N = len(Vs)
	Vs = Vs[N//2:N]
	ie = ie[N//2:N]
	return (Vs**2).mean()/R/(Ve*ie).mean()
			 

rend = []
alpha = np.linspace(0.02,0.99,50)
L = 0.5e-3
r = 1
C = 2000e-6
R = 10
Cs = 1000e-6
Ve = 10
re = 1
T = 5e-5 # 20 kHz
P = 1500
for k in range(len(alpha)):
	rend.append(rendement(L,r,C,re,R,Cs,T,alpha[k],P))
figure(figsize=(8,6))
title(r"$R=10\,\rm\Omega$",fontsize=16)
plot(alpha,rend)
xlim(0,1)
ylim(0,1)
grid()
xlabel(r"$\alpha$",fontsize=16)
ylabel(r"$\frac{P_c}{P_c}$",fontsize=16)
			

L = 0.5e-3
r = 1
C = 2000e-6
R = 10
Cs = 1000e-6
Ve = 10
re = 1
alpha = 0.5
T = 1e-4 # 10 kHz
P = 1500
temps,ie,i2,i = simulation2(L,r,C,re,R,Cs,T,alpha,Ve,0,Ve/re,0,0,P)
figure(figsize=(16,8))
subplot(311)
title(r"$V_e=10\ {\rm V},\ \alpha=0{,}5$",fontsize=16)
plot(temps,ie,label=r"$i_e$")
ylabel(r"$i_e\ (\rm A)$",fontsize=16)
grid()
subplot(312)
plot(temps,i2,label=r"$i_2$")
ylabel(r"$i_2\ (\rm A)$",fontsize=16)
grid()
ylim(0,2)
subplot(313)
plot(temps,R*i)
ylim(0,5)
grid()
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)
                                    

[temps,Vs,i2,ie] = np.load("convertisseur-buck-1.npy")
i2 = -i2
figure(figsize=(16,8))
subplot(311)
title(r"$V_e=10\ {\rm V},\ \alpha=0{,}5$",fontsize=16)
plot(temps,ie,label=r"$i_e$")
ylabel(r"$i_e\ (\rm A)$",fontsize=16)
grid()
subplot(312)
plot(temps,i2,label=r"$i_2$")
ylabel(r"$i_2\ (\rm A)$",fontsize=16)
grid()
ylim(0,2)
subplot(313)
plot(temps,Vs)
ylim(0,5)
grid()
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)
								

[f1,R1,X1] = np.loadtxt('impedance-bobine-500mV.csv',unpack=True,skiprows=31,delimiter=',')
[f2,R2,X2] = np.loadtxt('impedance-bobine-1V.csv',unpack=True,skiprows=31,delimiter=',')
[f3,R3,X3] = np.loadtxt('impedance-bobine-2V.csv',unpack=True,skiprows=31,delimiter=',')
[f4,R4,X4] = np.loadtxt('impedance-bobine-5V.csv',unpack=True,skiprows=31,delimiter=',')
figure(figsize=(12,8))
subplot(211)
plot(f1,R1,label='0.5 V')
plot(f2,R2,label='1 V')
plot(f3,R3,label='2 V')
plot(f4,R4,label='5 V')
grid()
xscale('log')
yscale('log')
ylabel(r"$R\ (\rm\Omega)$",fontsize=14)
legend(loc='upper left')
subplot(212)
plot(f1,X1,label='0.5 V')
plot(f2,X2,label='1 V')
plot(f3,X3,label='2 V')
plot(f4,X4,label='5 V')
grid()
xscale('log')
yscale('log')
ylabel(r"$X\ (\rm\Omega)$",fontsize=14)
legend(loc='upper left')      
             

p1 = np.polyfit(f1,X1,deg=1)
L1 = p1[0]/(2*np.pi)
p2 = np.polyfit(f2,X2,deg=1)
L2 = p2[0]/(2*np.pi)
p3 = np.polyfit(f3,X3,deg=1)
L3 = p3[0]/(2*np.pi)
p4 = np.polyfit(f4,X4,deg=1)
L4 = p4[0]/(2*np.pi)
             

[ta,K1,K2] = np.loadtxt("buck-DC5-PWM-r0,5-R100.txt",unpack=True,skiprows=1)
[tb,Vs] = np.loadtxt("buck-DC5-Vs-r0,5-R100.txt",unpack=True,skiprows=1)
figure(figsize=(16,6))
plot(ta,K1,label=r"$K_1$")
plot(ta,K2,label=r"$K_2$")
plot(tb,Vs,label=r"$V_s$")
xlabel("t (s)",fontsize=16)
ylabel("Volts",fontsize=16)
grid()
ylim(0,5)
legend(loc="upper right")
title(r"$\alpha=0{,}5,\ R=100\ {\rm \Omega}$",fontsize=16)
            

[ta,K1,K2] = np.loadtxt("buck-DC5-PWM-r0,1-R100.txt",unpack=True,skiprows=1)
[tb,Vs] = np.loadtxt("buck-DC5-Vs-r0,1-R100.txt",unpack=True,skiprows=1)
figure(figsize=(16,6))
plot(ta,K1,label=r"$K_1$")
plot(ta,K2,label=r"$K_2$")
plot(tb,Vs,label=r"$V_s$")
xlabel("t (s)",fontsize=16)
ylabel("Volts",fontsize=16)
grid()
ylim(0,5)
legend(loc="upper right")
title(r"$\alpha=0{,}1,\ R=100\ {\rm \Omega}$",fontsize=16)
            

[ta,K1,K2] = np.loadtxt("buck-DC5-PWM-r0,9-R100.txt",unpack=True,skiprows=1)
[tb,Vs] = np.loadtxt("buck-DC5-Vs-r0,9-R100.txt",unpack=True,skiprows=1)
figure(figsize=(16,6))
plot(ta,K1,label=r"$K_1$")  
plot(ta,K2,label=r"$K_2$")
plot(tb,Vs,label=r"$V_s$")
xlabel("t (s)",fontsize=16)
ylabel("Volts",fontsize=16)
grid()
ylim(0,5)
legend(loc="upper right")
title(r"$\alpha=0{,}9,\ R=100\ {\rm \Omega}$",fontsize=16)
            

alpha = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.95,0.99]
Vs_moy = [0.51,1.007,1.504,2.000,2.498,2.994,3.489,3.987,4.483,4.732,4.931]
Vs_ard = [0.48,0.988,1.477,1.971,2.48,2.984,3.478,3.986,4.476,4.722,4.921]
figure(figsize=(8,6))
plot(alpha,Vs_moy,label="Vs oscillo")
plot(alpha,Vs_ard,label="Vs arduino")
grid()
xlabel(r"$\alpha$",fontsize=16)
ylabel("Volts",fontsize=16)
xlim(0,1)
ylim(0,5)
legend(loc="upper left")
			

[ta,K1,K2] = np.loadtxt("buck-DC5-PWM-Ve-ie-r0,5-R100.txt",unpack=True,skiprows=1)
[tb,Ve,ie] = np.loadtxt("buck-DC5-Ve-ie-r0,5-R100.txt",unpack=True,skiprows=1)
figure(figsize=(16,6))
subplot(211)
plot(ta,K1,label=r"$K_1$")  
plot(ta,K2,label=r"$K_2$")
plot(tb,Ve,label=r"$V_e$")

grid()
ylim(0,6)
legend(loc="upper right")
title(r"$\alpha=0{,}5,\ R=100\ {\rm \Omega}$",fontsize=16)
subplot(212)
plot(tb,ie,label=r"$R_1i_e$")
grid()
xlabel("t (s)",fontsize=16)
ylabel(r"$i_e\ (\rm A)$",fontsize=16)
            

[ta,K1,K2] = np.loadtxt("buck-DC5-PWM-Ve-ie-r0,5-R100-TM5.txt",unpack=True,skiprows=1)
[tb,Ve,ie] = np.loadtxt("buck-DC5-Ve-ie-r0,5-R100-TM5.txt",unpack=True,skiprows=1)
figure(figsize=(16,6))
subplot(211)
plot(ta,K1,label=r"$K_1$")  
plot(ta,K2,label=r"$K_2$")
plot(tb,Ve,label=r"$V_e$")

grid()
ylim(0,6)
legend(loc="upper right")
title(r"$\alpha=0{,}5,\ R=100\ {\rm \Omega},\ TM=5$",fontsize=16)
subplot(212)
plot(tb,ie,label=r"$R_1i_e$")
grid()
xlabel("t (s)",fontsize=16)
ylabel(r"$i_e\ (\rm A)$",fontsize=16)
            

[ta,K1,K2] = np.loadtxt("buck-DC5-PWM-Ve-ie-r0,5-R100-TM10.txt",unpack=True,skiprows=1)
[tb,Ve,ie] = np.loadtxt("buck-DC5-Ve-ie-r0,5-R100-TM10.txt",unpack=True,skiprows=1)
figure(figsize=(16,6))
subplot(211)
plot(ta,K1,label=r"$K_1$")  
plot(ta,K2,label=r"$K_2$")
plot(tb,Ve,label=r"$V_e$")

grid()
ylim(0,6)
legend(loc="upper right")
title(r"$\alpha=0{,}5,\ R=100\ {\rm \Omega},\ TM=10$",fontsize=16)
subplot(212)
plot(tb,ie,label=r"$R_1i_e$")
grid()
xlabel("t (s)",fontsize=16)
ylabel(r"$i_e\ (\rm A)$",fontsize=16)
            

[ta,K1,K2] = np.loadtxt("buck-DC5-PWM-Ve-ie-r0,5-R100-TM20.txt",unpack=True,skiprows=1)
[tb,Ve,ie] = np.loadtxt("buck-DC5-Ve-ie-r0,5-R100-TM20.txt",unpack=True,skiprows=1)
figure(figsize=(16,6))
subplot(211)
plot(ta,K1,label=r"$K_1$")  
plot(ta,K2,label=r"$K_2$")
plot(tb,Ve,label=r"$V_e$")

grid()
ylim(0,6)
legend(loc="upper right")
title(r"$\alpha=0{,}5,\ R=100\ {\rm \Omega},\ TM=20$",fontsize=16)
subplot(212)
plot(tb,ie,label=r"$R_1i_e$")
grid()
xlabel("t (s)",fontsize=16)
ylabel(r"$i_e\ (\rm A)$",fontsize=16)
            

[ta,K1,K2] = np.loadtxt("buck-DC5-PWM-Vs-ie-r0,5-R100-TM10.txt",unpack=True,skiprows=1)
[tb,Vs,ie] = np.loadtxt("buck-DC5-Vs-ie-r0,5-R100-TM10.txt",unpack=True,skiprows=1)
figure(figsize=(16,6))
plot(ta,K1,label=r"$K_1$")
plot(ta,K2,label=r"$K_2$")
plot(tb,Vs,label=r"$V_s$")
xlabel("t (s)",fontsize=16)
ylabel("Volts",fontsize=16)
grid()
ylim(0,5)
legend(loc="upper right")
title(r"$\alpha=0{,}5,\ R=100\ {\rm \Omega}$",fontsize=16)
            

[ta,K1,K2] = np.loadtxt("buck-DC5-PWM-Vs-ie-r0,99-R100-TM10.txt",unpack=True,skiprows=1)
[tb,Vs,ie] = np.loadtxt("buck-DC5-Vs-ie-r0,99-R100-TM10.txt",unpack=True,skiprows=1)
figure(figsize=(16,6))
plot(ta,K1,label=r"$K_1$")
plot(ta,K2,label=r"$K_2$")
plot(tb,Vs,label=r"$V_s$")
xlabel("t (s)",fontsize=16)
ylabel("Volts",fontsize=16)
grid()
ylim(0,5)
legend(loc="upper right")
title(r"$\alpha=0{,}99,\ R=100\ {\rm \Omega}$",fontsize=16)
            

[t,Ve,ie] = np.loadtxt("buck-DC5-Ve-ie-r0,5-R100-TM10-1ms.txt",unpack=True,skiprows=1)
Pe = (Ve*ie).mean()
Vs = 2.45
R = 100
Pc = Vs**2/R
rend1 = Pc/Pe
Ve = 5
rend2 = Pc/(Ve*ie.mean())
			

[t,Ve,ie] = np.loadtxt("buck-DC5-Ve-ie-r0,8-R100-TM10-1ms.txt",unpack=True,skiprows=1)
Pe = (Ve*ie).mean()
Vs = 2.45
R = 100
Pc = Vs**2/R
rend1 = Pc/Pe
Ve = 5
rend2 = Pc/(Ve*ie.mean())
			

alpha = [0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.95,0.99]
Vs_moy = [0.975,1.4719,1.9641,2.450,2.924,3.373,3.833,4.319,4.564,4.767]
ie = [2.21,5.31,9.29,13.96,19.01,24.47,30.96,38.8,43.21,47.1] # R1*ie en mV
ie = np.array(ie)
Vs_moy = np.array(Vs_moy)
Ve = 5.0
R = 100
figure(figsize=(8,6))
plot(alpha,Vs_moy/Ve,label="Gain")
plot(alpha,Vs_moy**2/R/(Ve*ie*1e-3),label=r"$\frac{P_c}{P_e}$")
xlim(0,1)
ylim(0,1)
grid()
xlabel(r"$\alpha$",fontsize=16)
legend(loc="upper left")
title(r"$R=100\ {\rm \Omega},\  TM=10$",fontsize=16)
			

alpha = [0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.95,0.99]
Vs_moy = [1.006,1.501,1.996,2.488,2.981,3.470,3.958,4.445,4.686,4.879]
ie = [2.79,5.96,9.96,14.79,20,25.98,32.85,40.96,45.50,49.9] # R1*ie en mV
ie = np.array(ie)
Vs_moy = np.array(Vs_moy)
Ve = 5.0
R = 100
figure(figsize=(8,6))
plot(alpha,Vs_moy/Ve,label="Gain")
plot(alpha,Vs_moy**2/R/(Ve*ie*1e-3),label=r"$\frac{P_c}{P_e}$")
xlim(0,1)
ylim(0,1)
grid()
xlabel(r"$\alpha$",fontsize=16)
legend(loc="upper left")
title(r"$R=100\ {\rm \Omega},\  TM=0$",fontsize=16)
			

alpha = [0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.95,0.99]
Vs_moy = [1.577,2.1993,2.6748,3.0533,3.4304,3.573,3.8744,4.4112,4.6836,4.9227]
ie = [6.1363,11.55,16.58,20.955,26.066,27.96,32.16,40.56,45.35,49.36] # R1*ie en mV
ie = np.array(ie)
Vs_moy = np.array(Vs_moy)
Ve = 5.0
R = 100
figure(figsize=(8,6))
plot(alpha,Vs_moy/Ve,label="Gain")
plot(alpha,Vs_moy**2/R/(Ve*ie*1e-3),label=r"$\frac{P_c}{P_e}$")
xlim(0,1)
ylim(0,1)
grid()
xlabel(r"$\alpha$",fontsize=16)
legend(loc="upper left")
title(r"$R=100\ {\rm \Omega},\  TM=0,\ \rm{Non\ synchrone}$",fontsize=16)
			

alpha = [0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.95,0.99]
Vs_moy = [0.8383,1.3074,1.7741,2.2351,2.6885,3.1319,3.5636,3.9818,4.1886,4.3583]
ie = [10.59,24.92,45.92,73.03,105.51,143.72,187.08,235.54,261.82,283.88] # R1*ie en mV
ie = np.array(ie)
Vs_moy = np.array(Vs_moy)
Ve = 5.0
R = 15
figure(figsize=(8,6))
plot(alpha,Vs_moy/Ve,label="Gain")
plot(alpha,Vs_moy**2/R/(Ve*ie*1e-3),label=r"$\frac{P_c}{P_e}$")
xlim(0,1)
ylim(0,1)
grid()
xlabel(r"$\alpha$",fontsize=16)
legend(loc="upper left")
title(r"$R=15\ {\rm \Omega},\  TM=10$",fontsize=16)
			

alpha = [0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.95,0.99]
Vs_moy = [0.58916,1.0892,1.6028,2.1155,2.619,3.1165,3.5975,4.0656,4.2919,4.4722]
ie = [8.66,23.15,44.74,73.03,107.61,148.00,193.66,244.84,271.71,298.36] # R1*ie en mV
ie = np.array(ie)
Vs_moy = np.array(Vs_moy)
Ve = 5.0
R = 100
figure(figsize=(8,6))
plot(alpha,Vs_moy/Ve,label="Gain")
plot(alpha,Vs_moy**2/R/(Ve*ie*1e-3),label=r"$\frac{P_c}{P_e}$")
xlim(0,1)
ylim(0,1)
grid()
xlabel(r"$\alpha$",fontsize=16)
legend(loc="upper left")
title(r"$R=15\ {\rm \Omega},\  TM=0,\ \rm{Non\ synchrone}$",fontsize=16)
			


figure(figsize=(16,6))
[t,Vs] = np.loadtxt("buck-DC5-r0,2-0,8-Vs-R15.txt",skiprows=1,unpack=True)
plot(t,Vs,label=r"$\alpha=0,2\rightarrow 0,8$")
[t,Vs] = np.loadtxt("buck-DC5-r0,8-0,2-Vs-R15.txt",skiprows=1,unpack=True)
plot(t,Vs,label=r"$\alpha=0,8\rightarrow 0,2$")
grid()
ylim(0,6)
legend(loc="upper right")
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)
title(r"$R=15\,{\rm\Omega}$",fontsize=16)

                                    

figure(figsize=(16,6))
[t,Vs,D3] = np.loadtxt("buck-DC5-Vs-D3-Kp0,1-R15.txt",skiprows=1,unpack=True)
plot(t,Vs,label=r"$V_s$")
plot(t,D3,label=r"D3")
grid()
ylim(0,6)
legend(loc="upper right")
xlabel("t (s)",fontsize=16)
ylabel(r"$V\ (\rm V)$",fontsize=16)
title(r"$R=15\,{\rm\Omega},\ K_p=0,1,\ K_i=0,\ K_d=0$",fontsize=16)

                                    

figure(figsize=(16,6))
[t,Vs] = np.loadtxt("buck-DC5-Vs-Kp0,4-R15.txt",skiprows=1,unpack=True)
plot(t,Vs,label=r"$V_s$")
grid()
ylim(0,6)
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)
title(r"$R=15\,{\rm\Omega},\ K_p=0,4,\ K_i=0,\ T_d=0$",fontsize=16)

                                    

figure(figsize=(16,6))
[t,Vs] = np.loadtxt("buck-DC5-Vs-Kp0,5-R15.txt",skiprows=1,unpack=True)
plot(t,Vs,label=r"$V_s$")
grid()
ylim(0,6)
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)
title(r"$R=15\,{\rm\Omega},\ K_p=0,5,\ K_i=0,\ T_d=0$",fontsize=16)

                                    

figure(figsize=(16,6))
[t,Vs] = np.loadtxt("buck-DC5-Vs-Kp0,3-R15-1-4V.txt",skiprows=1,unpack=True)
plot(t,Vs,label=r"$V_s$")
grid()
ylim(0,6)
xlim(-0.05,0.05)
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)
title(r"$R=15\,{\rm\Omega},\ K_p=0,3,\ K_i=0,\ T_d=0$",fontsize=16)

                                    

figure(figsize=(16,6))
[t,Vs] = np.loadtxt("buck-DC5-Vs-Kp0,3-Ti0,01-R15-1-4V.txt",skiprows=1,unpack=True)
plot(t,Vs,label=r"$V_s$")
grid()
ylim(0,6)
xlim(-0.05,0.05)
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)
title(r"$R=15\,{\rm\Omega},\ K_p=0,3,\ T_i=0,01,\ T_d=0$",fontsize=16)

                                    

figure(figsize=(16,6))
[t,Vs] = np.loadtxt("buck-DC5-Vs-Kp0,1-Ti0,01-R15-1-4V.txt",skiprows=1,unpack=True)
plot(t,Vs,label=r"$V_s$")
grid()
ylim(0,6)
xlim(-0.05,0.05)
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)
title(r"$R=15\,{\rm\Omega},\ K_p=0,1,\ T_i=0,01,\ T_d=0$",fontsize=16)

                                    

figure(figsize=(16,6))
[t,Vs] = np.loadtxt("buck-DC5-Vs-Kp0,25-Ti0,01-R15-1-4V.txt",skiprows=1,unpack=True)
plot(t,Vs,label=r"$V_s$")
grid()
ylim(0,6)
xlim(-0.05,0.05)
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)
title(r"$R=15\,{\rm\Omega},\ K_p=0,25,\ T_i=0,01,\ T_d=0$",fontsize=16)

                                    

figure(figsize=(16,6))
[t,Vs] = np.loadtxt("buck-DC5-Vs-Kp0,25-Ti0,01-Td0,001-R15-1-4V.txt",skiprows=1,unpack=True)
plot(t,Vs,label=r"$V_s$")
grid()
ylim(0,6)
xlim(-0.05,0.05)
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)
title(r"$R=15\,{\rm\Omega},\ K_p=0,25,\ T_i=0,01,\ T_d=0,001$",fontsize=16)

                                    

figure(figsize=(16,6))
[t,Vs] = np.loadtxt("buck-DC5-Vs-Kp0,25-Ti0,01-Td0,002-R15-1-4V.txt",skiprows=1,unpack=True)
plot(t,Vs,label=r"$V_s$")
grid()
ylim(0,6)
xlim(-0.05,0.05)
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)
title(r"$R=15\,{\rm\Omega},\ K_p=0,25,\ T_i=0,01,\ T_d=0,002$",fontsize=16)

                                    

figure(figsize=(16,6))
[t,Vs] = np.loadtxt("buck-DC5-Vs-Kp0,25-Ti0,01-Td0,0015-R15-1-4V.txt",skiprows=1,unpack=True)
plot(t,Vs,label=r"$V_s$")
grid()
ylim(0,6)
xlim(-0.05,0.05)
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)
title(r"$R=15\,{\rm\Omega},\ K_p=0,25,\ T_i=0,01,\ T_d=0,0015$",fontsize=16)

                                    

figure(figsize=(16,6))
[t,Vs] = np.loadtxt("buck-DC5-Vs-Kp0,25-Ti0,01-Td0,0015-R15-4-1V.txt",skiprows=1,unpack=True)
plot(t,Vs,label=r"$V_s$")
grid()
ylim(0,6)
xlim(-0.05,0.05)
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)
title(r"$R=15\,{\rm\Omega},\ K_p=0,25,\ T_i=0,01,\ T_d=0,0015$",fontsize=16)

                                    

figure(figsize=(16,6))
[t,Vs] = np.loadtxt("buck-DC5-Vs-Kp0,25-Ti0,009-Td0,0015-R15-1-4V.txt",skiprows=1,unpack=True)
plot(t,Vs)
[t,Vs] = np.loadtxt("buck-DC5-Vs-Kp0,25-Ti0,009-Td0,0015-R15-4-1V.txt",skiprows=1,unpack=True)
plot(t,Vs)
grid()
ylim(0,6)
xlim(-0.05,0.05)
xlabel("t (s)",fontsize=16)
ylabel(r"$V_s\ (\rm V)$",fontsize=16)
title(r"$R=15\,{\rm\Omega},\ K_p=0,25,\ T_i=0,009,\ T_d=0,0015$",fontsize=16)

                                    
