The idea is that two capacitors are joined. Initially C_1 has a p.d. of 10 V and C_2 has zero. All constants can be changed.


In [32]:
C1 = 1e-6 # 1 micro-farad
C2 = 1e-6
V1 = 100
V2 = 0
Q1 = C1 * V1
Q2 = C2 * V2
E1 = 0.5*Q1*V1
E2 = 0.5*Q2*V2
E_start = 0.5*Q1*C1 + 0.5*Q2*C2
E_loss = 0
I = 0 # amps
R = 1000 # ohms
dt = 1e-6 # seconds

In [37]:
while(True):
    I = (V1-V2)/R
    ## Now, allow a small amount of charge to flow around the circuit
    dQ = I*dt
    Q1 -= dQ
    Q2 += dQ
    E_loss += I**2 * R * dt # energy loss in resistor
    E1 -= V1 * dQ
    E2 += V2 * dQ
    ## Now, calculate new voltages
    V1 = Q1 / C1
    V2 = Q2 / C2
    if I < 1e-5:
        break

E_final = 0.5*Q1*C1 + 0.5*Q2*C2

In [38]:
E_start


Out[38]:
4.9999999999999995e-11

In [39]:
E_final


Out[39]:
5.0000000000000034e-11

In [40]:
E_loss


Out[40]:
0.002502502477720431

In [ ]: