问题描述
我正在尝试通过对微分方程积分来计算 RC 电路的充电和放电。
这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Cursor
from scipy.integrate import odeint
def equa_diff(E,tab_t,tau,t):
return E/(R*C)
fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111,facecolor="#FFFFCC")
cursor = Cursor(ax,useblit=True,color='blue',linewidth=1)
#Les paramètres physiques
E = 5
R = 1000.0
C = 1e-6
tau = R * C
u0 = 0
cond_init = [0,E / R]
#Les paramètres numériques
t_min = 0.0
t_max = 10e-3
n_t = 100
tab_t = np.linspace(t_min,t_max,n_t)
tab_t1 = tab_t + 0.01
tab_u = odeint(equa_diff,u0,args=(tau,E))
tab_u1 = odeint(equa_diff,E,tab_t1,0))
u_max = max(tab_u)
#La charge du condensateur
ax.plot(tab_t,tab_u,'r',label='uc(t)')
ax.plot(tab_t,np.ones(100) * E,'black',label='e(t)')
ax.plot(tab_t,tab_t / tau,'b',label="tangente à l'origine")
ax.plot(tab_t,-tab_u + E,'green',label='ur(t)')
#La décharge du condensateur
ax.plot(tab_t1,tab_u1,'r')
ax.plot(tab_t1,np.zeros(100),'black')
ax.plot(tab_t1,-tab_u1 + E,'green')
plt.xlabel('t')
plt.ylabel('u')
plt.title('Circuit en série RC')
plt.ylim(0,u_max * 1.2)
plt.legend(loc=4)
plt.show()
我想要这个:
但我有这个:
我已经尽力了,但我无法纠正它。
解决方法
你必须考虑到充电和放电是由
描述的充电
dUc/dt + Uc/RC = E/RC => dUc / dt = 1/RC * ( E - Uc)
放电
dUc/dt + Uc/RC = 0 => dUc / dt = -Uc/RC
您的函数缺少-Uc/RC
,如果您愿意,可以将其作为一个函数保留在python中,但我出于个人喜好将其拆分为充电和放电形式。为了充电过程,我还必须修改 t=0 处的切线,因为它没有给我正确的斜率。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Cursor
from scipy.integrate import odeint
#Les paramètres physiques
E = 5
R = 1000.0
C = 1e-6
tau = R * C
u0 = 0
def RC_Circuit_Charging(u,t):
return (E - u )/(R*C)
def RC_Circuit_Discharging(u,t):
return -u/(R*C)
fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111,facecolor="#FFFFCC")
cursor = Cursor(ax,useblit=True,color='blue',linewidth=1)
#Les paramètres numériques
t_min = 0.0
t_max = 10e-3
n_t = 100
tab_t = np.linspace(t_min,t_max,n_t)
tab_t1 = tab_t + 0.01
tab_u = odeint(RC_Circuit_Charging,u0,tab_t)
tab_u1 = odeint(RC_Circuit_Discharging,E,tab_t1)
u_max = max(tab_u)
#La charge du condensateur
ax.plot(tab_t,tab_u,'r',label='uc(t)')
ax.plot(tab_t,np.ones(100) * E,'black',label='e(t)')
ax.plot(tab_t,E * tab_t / tau,'b',label="tangente à l'origine") # tangent line du/dt at t = 0 so E - u(0) / RC = E / RC
ax.plot(tab_t,-tab_u + E,'green',label='ur(t)')
#La décharge du condensateur
ax.plot(tab_t1,tab_u1,'r')
ax.plot(tab_t1,np.zeros(100),'black')
ax.plot(tab_t1,-tab_u1 + E,'green')
plt.xlabel('t')
plt.ylabel('u')
plt.title('Circuit en série RC')
plt.ylim(0,u_max * 1.2)
plt.legend(loc=4)
plt.show()