Vous êtes sur la page 1sur 4

from matplotlib.

widgets import Slider


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

f = 50
w = 2*pi*f
Vm = 230
alpha = 1
R = 100
L = 100
z = sqrt(R*R + L*L)
E = 100
# bita = 'undefined'
n = 300
x = np.linspace(0, 6*pi, n)
t = x/w
theta = 0
Vi = np.sin(x)*Vm
y = Vi
Ii = np.zeros(n)
Io = np.zeros(n)
Vo = np.zeros(n)
Vt = np.zeros(n)

m = int((2*n)/6)

fig, axs = plt.subplots(2, 2)

slider_R = plt.axes([0.15, 0.95, 0.3, 0.02])


slider_L = plt.axes([0.15, 0.9, 0.3, 0.02])
slider_A = plt.axes([0.6, 0.95, 0.3, 0.02])

R_slider = Slider(slider_R, 'R(ohm)', 10, 1000, valinit=R)


L_slider = Slider(slider_L, 'L(mH)', 10, 1000, valinit=L)
A_slider = Slider(slider_A, 'alpha(rad)', 0, pi-theta, valinit=alpha)

props = dict(boxstyle='round', facecolor='wheat', alpha=1)

def update(val):
R = R_slider.val
L = L_slider.val
alpha = A_slider.val

theta = np.arctan(w*L/R)
I0 = 0
for k in range(1, 4, 1):

for i in range(m):
wt = x[(k-1)*m+i]
# print(wt)
if(wt < alpha and wt >= 0):
# Io[i] = I0*np.exp(-1*(R/L)*(x[i]/w - alpha/w))
Io[i] = 0
elif(wt >= 2*(k-1)*pi+alpha and wt <= (2*k-1)*pi):
# print(wt, (2*k-1)*pi)
Io[(k-1)*m+i] = (Vm/z)*np.sin(wt-theta) + (I0-(Vm/z) *
np.sin(alpha-
theta))*np.exp(-1*(R/L)*(wt/w - (2*(k-1)*pi+alpha)/w))
if(round((2*k-1)*pi, 2) - round(wt, 2) < 0.1):
I0 = (Vm/z)*np.sin(pi-theta) + (I0-(Vm/z) *
np.sin(alpha-theta))*np.exp(-
1*(R/L)*(pi/w - alpha/w))
# print("Hello", wt, 2*(k-1)*pi)
elif(wt > (2*k-1)*pi and wt < 2*k*pi+alpha):
Io[(k-1)*m+i] = I0*np.exp(-1*(R/L)*(x[i]/w - alpha/w))

# print((pi+alpha)/w, 5*L/R, I0)


if((pi+alpha)/w < 5*L/R):
I0 = I0*np.exp(-1*(R/L)*(pi/w - alpha/w))
# print(I0)
else:
I0 = 0
for i in range(m):
# Mode - 2
if x[i] < alpha or (x[i] > pi and x[i] <= 2*pi):
Vo[i], Vo[i + m], Vo[i + 2 * m] = 0, 0, 0

Ii[i], Ii[i + m], Ii[i + 2 * m] = 0, 0, 0


# # Io for 1st cycle
# Io[i] = ((Vm/z)*np.sin(pi-theta) + (0-(Vm/z) *
# np.sin(alpha-theta))*np.exp(-
1*(R/L)*(pi/w - alpha/w)))*np.exp(-1*(R/L)*(x[i]/w - alpha/w))

# # Calculating I0
# if((pi+alpha)/w) < 5*L/R:
# I0 = (Vm/z)*np.sin(2*pi+alpha-theta) + (0-(Vm/z) *
# np.sin(alpha-
theta))*np.exp(-1*(R/L)*(2*pi+alpha/w - alpha/w))
# else:
# I0 = 0
# # Io for 2nd cycle
# Io[i + m] = ((Vm/z)*np.sin(pi-theta) + (I0-(Vm/z) *
# np.sin(alpha-theta))*np.exp(-
1*(R/L)*(pi/w - alpha/w)))*np.exp(-1*(R/L)*(x[i+m]/w - alpha/w))

# # Calculating I0
# if((pi+alpha)/w) < 5*L/R:
# I0 = (Vm/z)*np.sin(2*pi+alpha-theta) + (0-(Vm/z) *
# np.sin(alpha-
theta))*np.exp(-1*(R/L)*(2*pi+alpha/w - alpha/w))
# else:
# I0 = 0
# # Io for 3rd cycle
# Io[i + 2 * m] = ((Vm/z)*np.sin(pi-theta) + (I0-(Vm/z) *
# np.sin(alpha-
theta))*np.exp(-1*(R/L)*(pi/w - alpha/w)))*np.exp(-1*(R/L)*(x[i+2*m]/w - alpha/w))
# Mode - 1
elif x[i] <= pi and x[i] >= alpha:
Vo[i], Vo[i + m], Vo[i + 2 * m] = Vi[i], Vi[i], Vi[i]
# # Io for 1st cycle
# Io[i] = (Vm/z)*np.sin(x[i]-theta) + (0-(Vm/z) *
# np.sin(alpha-theta))*np.exp(-
1*(R/L)*(x[i]/w - alpha/w))

# # Calculating I0
# if((pi+alpha)/w) < 5*L/R:
# I0 = (Vm/z)*np.sin(2*pi+alpha-theta) + (0-(Vm/z) *
# np.sin(alpha-
theta))*np.exp(-1*(R/L)*(2*pi+alpha/w - alpha/w))
# else:
# I0 = 0
# # Io for 2nd cycle
# Io[i + m] = (Vm/z)*np.sin(x[i]-theta) + (I0-(Vm/z) *
# np.sin(alpha-
theta))*np.exp(-1*(R/L)*(x[i+m]/w - alpha/w))

# # Calculating I0
# if((pi+alpha)/w) < 5*L/R:
# I0 = (Vm/z)*np.sin(2*pi+alpha-theta) + (0-(Vm/z) *
# np.sin(alpha-
theta))*np.exp(-1*(R/L)*(2*pi+alpha/w - alpha/w))
# else:
# I0 = 0
# # Io for 3rd cycle
# Io[i + 2 * m] = (Vm/z)*np.sin(x[i]-theta) + (I0-(Vm/z)
# * np.sin(alpha-
theta))*np.exp(-1*(R/L)*(x[i+2*m]/w - alpha/w))
Ii[i], Ii[i + m], Ii[i + 2 * m] = Io[i], Io[i + m], Io[i + 2 * m]
# Mode - 2
# elif x[i] < 2 * pi:
# Vo[i], Vo[i + m], Vo[i + 2 * m] = 0, 0, 0
# Ii[i], Ii[i + m], Ii[i + 2 * m] = 0, 0, 0
# Io[i] = ((Vm/z)*np.sin(pi-theta) + (0-(Vm/z) *
# np.sin(alpha-theta))*np.exp(-
1*(R/L)*(pi/w - alpha/w)))*np.exp(-1*(R/L)*(x[i]/w - alpha/w))
# I0 = Io[i]
# Io[i + m] = ((Vm/z)*np.sin(pi-theta) + (I0-(Vm/z) *
# np.sin(alpha-theta))*np.exp(-
1*(R/L)*(pi/w - alpha/w)))*np.exp(-1*(R/L)*(x[i+m]/w - alpha/w))
# I0 = Io[i]
# Io[i + 2 * m] = ((Vm/z)*np.sin(pi-theta) + (I0-(Vm/z) *
# np.sin(alpha-
theta))*np.exp(-1*(R/L)*(pi/w - alpha/w)))*np.exp(-1*(R/L)*(x[i+2*m]/w - alpha/w))

if(5*w*L*10**(-3)/R < pi+alpha):


bita = 5*w*L*10**(-3)/R + pi
elif(5*w*L*10**(-3)/R >= pi+alpha):
bita = 'undefined'
axs[0, 0].clear()
axs[0, 0].plot(x, Vi, 'tab:blue')
axs[0, 0].set(xlabel='wt', ylabel='Vi(Input Voltage)')

axs[0, 1].clear()
axs[0, 1].plot(x, Vo, 'tab:orange')
axs[0, 1].set(xlabel='wt', ylabel='Vo(Output Voltage)')

axs[1, 0].clear()
axs[1, 0].plot(x, Ii, 'tab:green')
axs[1, 0].set(xlabel='wt', ylabel='Ii(input Current)')

axs[1, 1].clear()
axs[1, 1].plot(x, Io, 'tab:red')
axs[1, 1].set(xlabel='wt', ylabel='Io(output Current)')
Vrms = (Vm/(2*sqrt(pi)))*sqrt(pi-alpha+0.5*np.sin(2*alpha))
Vavg = (Vm/(2*pi))*(1+np.cos(2*alpha))
# Iavg = (Vavg-E)/R
# Pi = ((Vrms**2)/R)+(E*Iavg)

# axs[0, 0].clear()
if(type(bita) is str):
# print("Hello")
textstr = "Beta={}".format(bita)

else:
textstr = "Beta={}".format(round(bita, 2))
# print(type(bita))
# textstr = "Beta={}".format(round(bita, 2))
axs[0, 0].text(0.01, 0.30, textstr, transform=axs[0, 0].transAxes,
fontsize=10, verticalalignment='top', bbox=props)

axs[0, 0].text(0.01, 0.20, 'Vrms = {} V'.format(round(Vrms, 2)),


transform=axs[0, 0].transAxes,
fontsize=10, verticalalignment='top', bbox=props)

axs[0, 0].text(0.01, 0.10, 'Vavg = {} V'.format(round(Vavg, 2)),


transform=axs[0, 0].transAxes,
fontsize=10, verticalalignment='top', bbox=props)

fig.canvas.draw_idle()

R_slider.on_changed(update)
L_slider.on_changed(update)
A_slider.on_changed(update)

plt.show()

Vous aimerez peut-être aussi