πŸ“œ ⬆️ ⬇️

Electrical circuit simulation


This publication provides instructions for modeling an electrical circuit using the state variable method.

This publication is in order to be in Russian at least one how2 on the simulation of electrical circuits by this method. At one time I googled a lot and never once did I come across normal material. All the manuals and textbooks contained only theory. In addition, none of the materials I found contained a complete solution cycle:

scheme ⟢ equations ⟢ numerical solution ⟢ graphics.

Actually, this is the algorithm of actions.
')
The scheme is, now you need to make equations using the laws of Ohm and Kirchhoff.

Component equations:

Cβˆ— fracdUcdt=ic;Lβˆ— fracdildt=UL;


U1=i1βˆ—R1;U2=i2βˆ—R2;U3=i3βˆ—R3;U4=i4βˆ—R4;U1=i1βˆ—R1;


Contour equations:

Eβˆ’ULβˆ’U1=0;


U1βˆ’U2βˆ’U3=0;


U3βˆ’UCβˆ’U4=0;


Nodal equations:

iLβˆ’i1βˆ’i2=0;


i2βˆ’i3βˆ’i4=0;


ic=i4;



Now we need to derive differential equations. I note that in this method, it is customary to take charge of capacitors and flux couplings of inductances as state variables. Knowing these values, it is possible to derive any voltages and currents of nodes and branches. Also:

  1. Equations must be independent;
  2. The equations should include only state and source variables; all other variables must be expressed through state variables;
  3. The first derivative of the state variables must enter the left side of each equation; on the right side there should be no derivatives.

We derive the first differential equation:

UL=Eβˆ’U1=Eβˆ’U2βˆ’U3=Eβˆ’U2βˆ’UCβˆ’U4;


UL=Eβˆ’i2βˆ—R2βˆ’UCβˆ’iCβˆ—R4=Eβˆ’(iLβˆ’ fracEβˆ’ULR1)βˆ—R2βˆ’UCβˆ’iCβˆ—R4;


UL=Eβˆ’iLβˆ—R2+Eβˆ— fracR2R1βˆ’ULβˆ— fracR2R1βˆ’UCβˆ’iCβˆ—R4;


ULβˆ— fracR1+R2R1=Eβˆ— fracR1+R2R1βˆ’iLβˆ—R2βˆ’UCβˆ’iCβˆ—R4;


iC=iLβˆ’i1βˆ’i3=iLβˆ’ fracEβˆ’ULR1βˆ’ fracUC+iCβˆ—R4R3;


iC fracR3+R4R3=iLβˆ’ fracEβˆ’ULR1βˆ’ fracUCR3;


Substitute the capacitor current into the coil voltage equation:

ULβˆ— fracR1+R2R1=Eβˆ— fracR1+R2R1βˆ’iLβˆ—R2βˆ’UCβˆ’ fracR3βˆ—R4R3+R4βˆ—(iLβˆ’ fracER1+ fracULR1βˆ’ fracUCR3);


Transforming the equation, we get the first differential equation:

Lβˆ— fracdiLdt=UL=Eβˆ’ fracR1(R1+R2)βˆ—(R3+R4)+R3βˆ—R4βˆ—(iLβˆ—(R2βˆ—(R3+R4)+R3)βˆ’UCβˆ—R3);


We derive the second differential equation:

iC=iLβˆ’i1βˆ’i3=iLβˆ’ fracEβˆ’ULR1βˆ’ fracUCR3βˆ’ fraciCβˆ—R4R3;


Cβˆ— fracdUcdt=ic= fracR3Cβˆ—(R3+R4)βˆ—(iLβˆ’ fracEβˆ’ULR1βˆ’ fracUCR3);



Now we have a system of differential equations that can be solved numerically:

 begincasesLβˆ— fracdiLdt=Eβˆ’ fracR1(R1+R2)βˆ—(R3+R4)+R3βˆ—R4βˆ—(iLβˆ—(R2βˆ—(R3+R4)+R3)βˆ’UCβˆ—R3);Cβˆ— fracdUcdt= fracR3Cβˆ—(R3+R4)βˆ—(iLβˆ’ fracEβˆ’ULR1βˆ’ fracUCR3); endcases



We use the Euler method because it's the easiest and use Python:

Xi=Xiβˆ’1+hβˆ— fracdXiβˆ’1dt


Program
import numpy as np import matplotlib.pyplot as plt #Input voltage amplitude AMP = 21.0 #Active components r1 = 2000.0; r2 = 10.0; r3 = 10.0; r4 =2000.0; #Reactive components c=0.0001; l=0.06; #Time components T=0.01; t0=0.0; step=T/1000; tf=T*10 steps=int(tf/step); #Input voltage def E(t): n=int(t/T) if ((t >= n*T )and(t <= n*T + T/2)): return AMP else: return 0.0 time = np.arange(t0, tf, step) ul = []; il = []; uc = []; ic = []; y = []; for i in range(0, steps, 1): y.append(E(time[i])) def dIl_dt(t): return float((1.0/l)*(E(t) - (r1/((r1+r2)*(r3+r4)+r3*r4) * (il[int(t/step)]*(r2*(r3+r4)+r3) - uc[int(t/step)]*r3)))) def dUc_dt(t): return float((1.0/c)* (r3/(r3+r4)) * (il[int(t/step)]-(E(t)-ul[int(t/step)])/r1 - uc[int(t/step)]/r3)) #Start condition ul.append(E(0)); il.append(0.0); uc.append(0.0); ic.append(0.0); #Euler method for i in range(1, steps, 1): il.append(il[i-1] + step*dIl_dt(time[i-1])) uc.append(uc[i-1] + step*dUc_dt(time[i-1])) ul.append(l*dIl_dt(time[i])) ic.append(c*dUc_dt(time[i])) plt.figure("charts") e = plt.subplot(311) e.plot(time, y) e.set_xlabel('time (s)') e.set_ylabel('E(t), (V)', color='b') plt.grid(True) UL = plt.subplot(312) UL.plot(time, ul) UL.set_xlabel('time (s)') UL.set_ylabel('Ul(t), (V)', color = 'b') IL = UL.twinx() IL.plot(time, il, 'r') IL.set_ylabel('Il(t), (A)', color = 'r') plt.grid(True) UC = plt.subplot(313) UC.plot(time, uc) UC.set_xlabel('time (s)') UC.set_ylabel('Uc(t), (V)', color = 'b') IC = UC.twinx() IC.plot(time, ic, color = 'r') IC.set_ylabel('Ic(t), (A)', color = 'r') plt.grid(True) plt.show() 



Simulation results:


It is better to use other methods for solving difur for real calculations, and for obtaining graphs of voltages and currents of elements for self-learning, the simplest Euler method is sufficient. In electronics, the most common Newton-Raphson method is in most CAD systems it is this method that is used.

From the literature I advise books of PN Matkhanov. and Zebeke G.V.
And the manuals of all polytechnics (MSTU, St. Petersburg, Tomsk, etc.) and similar material are easier to throw out than to understand what is written there. Here is a catch phrase: β€œsimplify is difficult, complicate is easy”.

Source: https://habr.com/ru/post/323590/


All Articles