用积分法计算RC电路的充放电曲线

2024-05-23 13:24:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过积分微分方程来计算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, tab_t, args=(tau, E))
tab_u1 = odeint(equa_diff, E, tab_t1, args=(tau, 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()

我想要这个:

I would like to have this

但我有一个:

But I have this

我已经尽力了,但还是没能改正


Tags: importplotnpdiffpltaxtablabel
1条回答
网友
1楼 · 发布于 2024-05-23 13:24:01

您必须考虑到充电和放电是由

充电

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()

enter image description here

相关问题 更多 >