10  Initial Conditions

In electric circuit analysis, initial conditions refer to the stored energy states of reactive components at a designated at time, typically \(t = 0\). The starting energy state provides the numerical values needed to solve for circuit’s voltage or current at any \(t \ge 0\).

Circuits containing resistors alone respond instantaneously. However, when capacitors and inductors are present, the circuit’s behavior is described by differential equations.

10.1 Initial Capacitor Voltage

In the analysis presented in this chapter, capacitors are assumed to have an initial condition described in terms of an initial voltage across its terminals. This is modeled with a series DC step-voltage source of value \(\frac{v_C(0^-)}{s}\). The added series voltage source is the s-domain abstraction of the initial energy stored in the capacitor expressed in terms of voltage. The schematic of this is shown in Figure 10.1, where the capacitor is replaced by:

\(\underbrace{\frac{1}{Cs}}_{\text{Impedance}} + \underbrace{\frac{v_C(0^-)}{s}}_{\text{Series voltage source}}\)

The voltage across a capacitor cannot change instantaneously because the current equation is \(i = C \frac{dv}{dt}\). A step change in voltage would require infinite current, which is physically impossible. Therefore: \(v_C(0^+) = v_C(0^-)\)

A capacitor’s initial condition is expressed as a voltage (\(v_C\)) rather than a current because voltage is the direct measure of the energy stored in a capacitor’s electric field, and its governing differential equation dictates that voltage must remain continuous across switching events.

The fundamental constitutive equation relating a capacitor’s current to its voltage is: \(i(t) = C \frac{dv_C(t)}{dt}\). If we integrate both sides of this equation over an infinitesimally small time interval spanning a switching event from just before the switch flips (\(t = 0^-\)) to just after (\(t = 0^+\)), we get:

\(v_C(0^+) - v_C(0^-) = \frac{1}{C} \int_{0^-}^{0^+} i(t) \, dt\)

In any physical circuit, the current \(i(t)\) is finite. An infinite current would require infinite power, which is physically impossible. Therefore, the integral of a finite current over a zero-width time interval evaluates to zero:

\(\int_{0^-}^{0^+} i(t) \, dt = 0 \implies v_C(0^+) = v_C(0^-)\)

This proves that capacitor voltage cannot change instantaneously. Because its value immediately after a transition (\(0^+\)) is locked to its value immediately before (\(0^-\)), that pre-existing voltage serves as the mandatory initial condition for solving the post-switching differential equation.

A capacitor’s initial condition must always be the variable tied directly to its stored energy, because energy cannot be transfered infinitely fast. A Capacitor stores energy in an electric field via the formula \(W = \frac{1}{2} C v_C^2\). Since energy is a function of voltage, voltage is the state variable that must be continuous.

10.2 Initial Inductor Current

Inductors in the network that have initial conditions, have initial conditions described in terms of an initial current flowing in the inductor at \(t=0\). This is modeled by a DC step-current source of value \(\frac{i_L(0^-)}{s}\). The parallel current source is the s-domain abstraction of the inductor’s internal magnetic field. The schematic of this is shown in Figure 10.1, where the inductor is replaced by:

\(\underbrace{{Ls}}_{\text{Impedance}} \parallel \underbrace{\frac{i_L(0^-)}{s}}_{\text{Parallel current source}}\)

An inductor’s initial condition is expressed as a current (\(i_L\)) rather than a voltage because current is the direct measure of the energy stored in an inductor’s magnetic field, and its governing differential equation dictates that current must remain continuous across switching events.

The fundamental constitutive equation relating an inductor’s voltage to its current is: \(v(t) = L \frac{di_L(t)}{dt}\)

If we integrate both sides of this equation over an infinitesimally small time interval spanning a switching event from just before the switch flips (\(t = 0^-\)) to just after (\(t = 0^+\)), we get:

\(i_L(0^+) - i_L(0^-) = \frac{1}{L} \int_{0^-}^{0^+} v(t) \, dt\)

In any physical circuit, the voltage \(v(t)\) across an inductor is finite (an infinite voltage would require infinite power or instantly break down component insulation). Therefore, the integral of a finite voltage over a zero-width time interval evaluates to zero:

\(\int_{0^-}^{0^+} v(t) \, dt = 0 \implies i_L(0^+) = i_L(0^-)\)

This proves that inductor current cannot change instantaneously. Because its value immediately after a transition (\(0^+\)) is locked to its value immediately before (\(0^-\)), that pre-existing current serves as the mandatory initial condition for solving the post-switching differential equation.

The derivative term \(\frac{di_L}{dt}\) reveals why voltage can change instantly. If a circuit forces an abrupt change in current (such as snapping open a switch connected to an energized inductor), the rate of change \(\frac{di_L}{dt}\) becomes extreamly large.

According to \(v = L \frac{di}{dt}\), a large rate of current change produces a large voltage spike.

This phenomenon is known as inductive kickback or flyback. Because voltage can jump discontinuously from zero to thousands of volts in a microsecond, voltage has no memory of its pre-switch value and cannot act as a stable initial condition.

An inductor stores energy in a magnetic field via \(W = \frac{1}{2} L i_L^2\). Because its governing equation is \(v(t) = L \frac{di_L(t)}{dt}\), it is current that is continuous, making current the initial condition for an inductor.

The current through an inductor cannot change instantaneously because the voltage equation is \(v = L \frac{di}{dt}\). A step change in current would require infinite voltage.

10.3 S-domain Analysis

In s-domain analysis, the initial conditions are included in the analysis by using auxiliary sources in the transformed models of the capacitors and inductors.

Mathematically, this is expressed as \(v_C(0^-) = 0\) then \(v_C(0) = V_{ic}\) and \(i_L(0^-) = 0\) then \(i_L(0)=I_{ic}\), where \(0^-\) is the first instant of time to the left of \(t=0\). Some engineering text books use \(v_C(0^-) = v_C(0)\) and \(i_L(0^-) = i_L(0)\), others use \(v_C(0) = v_C(0^+)\) and \(i_L(0) = i_L(0^+)\). So depending on which text book is consulted, there can be different ways of describing the value of the initial conditions at the time origin. Some of these inconsistencies are explored in Lundberg, Miller, and Trumper (2007).

The figure below contains the model for initial conditions used in this book for capacitors and inductors. Although it is possible to describe initial conditions for capacitors in terms of current, I think it is more intuitive to think about a capacitor as having an initial voltage as the initial condition at \(t=0\). The polarity of \(V\), can be changed either by changing the orientation of the symbol in the schematic before the netlist is generated or by changing the sign of the value of the initial voltage. The initial condition for inductors can also be described in terms of an initial voltage, I think it is more natural to describe the initial condition in terms of current present in the inductor at \(t=0\). The direction of the arrow in the current symbol in the LTSpice schematic can be confusing since the convention used is that the positive direction is out of node 1 in the figure below, which is opposite to the direction of the arrow on the current symbol.

Figure 10.1: On the right is the initial conditions model for the capacitor and for the inductor, on the left.

It should be noted that circuit, when redrawn to include initial conditions, by inserting the models shown above, alters the netlist to now include an additional voltage or current source. From the perspective of the overall circuit, the initial conditions associated with the capacitor or inductor are now contained in these additional sources. However, the capacitor or inductor, themselves remain in a zero state.

A capacitor in the zero state has no voltage across its terminals, so the series voltage source, that represents the initial condition does present an initial voltage across nodes 1 and 2 in the figure above. When a solution is obtained for the node voltages, the node voltages are correct and the current through the capacitor is the same current in \(V\). In the steady state, the capacitor will block the voltage from \(V\).

The inductor, shown above, is also in a zero state. The initial current condition is not present in the inductor itself, but to the overall circuit, the additional current source supplies this initial current to the circuit at \(t=0\). When a solution is obtained for the MNA network equations, one of the unknow currents is \(i_L\). To the MNA equations, \(L\) itself was in the zero state, so \(i_L\) is missing the initial condition. When evaluating \(i_L\), \(i(s)\) needs to be manually included, as shown in the examples below. In the steady state, the inductor will short the current from \(I\).

The following Python libraries are used in this notebook.

from sympy import *
import numpy as np
from scipy import signal
import pandas as pd
import matplotlib.pyplot as plt
import SymMNA
from IPython.display import display, Markdown, Math, Latex
init_printing()

10.4 Capacitor Initial Conditions

Fix plot to match LTSpice…

In the circuit shown below, there are three circuit elements, the voltage source \(V_1\), \(C_1\) and \(R_1\), all elements are arranged in series. The value of \(V_1\) is considered the capacitor’s initial condition of having a voltage equal to \(V_1=1\) at time \(t=0\).

Figure 10.2: Capacitor initial conditions example circuit.

The circuit was drawn using LTSpice and the netlist was exported and copied below.

Example_1_net_list = '''
* Initial_conditions_example_1_v1.asc
L1 1 2 1e-3
C1 1 3 1e-6
R1 2 0 10
V1 3 0 1
'''

The MNA equations are generated from the function SymMNA.smna.

report, network_df, i_unk_df, A, X, Z = SymMNA.smna(Example_1_net_list)

The code below assembles the network equations from the MNA matrices and displays the equations.

# Put matrices into SymPy 
X = Matrix(X)
Z = Matrix(Z)

# put the matrices into equation form 
NE_sym = Eq(A*X,Z)

# free symbols are entered as SymPy variables
var(str(NE_sym.free_symbols).replace('{','').replace('}',''))

# the element values are put into a dictionary
element_values = SymMNA.get_part_values(network_df) # get element values from netlist

# display the equations
temp = ''
for i in range(shape(NE_sym.lhs)[0]):
    temp += '${:s} = {:s}$<br>'.format(latex(NE_sym.rhs[i]),latex(NE_sym.lhs[i]))
Markdown(temp)

\(0 = C_{1} s v_{1} - C_{1} s v_{3} + I_{L1}\)
\(0 = - I_{L1} + \frac{v_{2}}{R_{1}}\)
\(0 = - C_{1} s v_{1} + C_{1} s v_{3} + I_{V1}\)
\(V_{1} = v_{3}\)
\(0 = - I_{L1} L_{1} s + v_{1} - v_{2}\)

Initial conditions for the capacitor \(C_1\) is set by making \(V_1\) a step voltage equal to the value of the initial voltage condition. \(1/s\) is substituted for the value of \(V_1\).

element_values[V1] = element_values[V1]/s
NE_ic = NE_sym.subs(element_values)

# display the equations
temp = ''
for i in range(shape(NE_ic.lhs)[0]):
    temp += '${:s} = {:s}$<br>'.format(latex(NE_ic.rhs[i]),latex(NE_ic.lhs[i]))
Markdown(temp)

\(0 = I_{L1} + 1.0 \cdot 10^{-6} s v_{1} - 1.0 \cdot 10^{-6} s v_{3}\)
\(0 = - I_{L1} + 0.1 v_{2}\)
\(0 = I_{V1} - 1.0 \cdot 10^{-6} s v_{1} + 1.0 \cdot 10^{-6} s v_{3}\)
\(\frac{1.0}{s} = v_{3}\)
\(0 = - 0.001 I_{L1} s + v_{1} - v_{2}\)

Solve the network equations and display the results.

U_ic = solve(NE_ic,X)

temp = ''
for i in U_ic.keys():
    temp += '${:s} = {:s}$<br>'.format(latex(i),latex(U_ic[i]))

Markdown(temp)

\(I_{L1} = \frac{1000.0}{s^{2} + 10000.0 s + 1000000000.0}\)
\(I_{V1} = - \frac{1000.0}{s^{2} + 10000.0 s + 1000000000.0}\)
\(v_{1} = \frac{s + 10000.0}{s^{2} + 10000.0 s + 1000000000.0}\)
\(v_{2} = \frac{10000.0}{s^{2} + 10000.0 s + 1000000000.0}\)
\(v_{3} = \frac{1}{s}\)

The expression for the voltage at node 1 is obtained by referencing the \(v_1\) as shown below. nsimplify() is used to replace floats by whole numbers if possible. This makes it easier for Sympy to find the inverse Laplace transform.

node_1_voltage_s = U_ic[v1].nsimplify()
node_1_voltage_s

\(\displaystyle \frac{s + 10000}{s^{2} + 10000 s + 1000000000}\)

Declare the symbol \(t\) to be used as the time variable.

t = symbols('t',positive=True)  # t > 0

Find the inverse Laplace transform using the SymPy function, inverse_laplace_transform.

node_1_voltage_t = inverse_laplace_transform(node_1_voltage_s, s, t)
node_1_voltage_t

\(\displaystyle \frac{\sqrt{39} e^{- 5000 t} \sin{\left(5000 \sqrt{39} t \right)}}{39} + e^{- 5000 t} \cos{\left(5000 \sqrt{39} t \right)}\)

Use lambdify to generate a function of time.

func_node_1_voltage_t = lambdify(t, node_1_voltage_t) 

The inverse Laplace is obtained for node voltage 2 with the following code.

node_2_voltage_s = U_ic[v2].nsimplify()
node_2_voltage_s

\(\displaystyle \frac{10000}{s^{2} + 10000 s + 1000000000}\)

node_2_voltage_t = inverse_laplace_transform(node_2_voltage_s, s, t)
node_2_voltage_t

\(\displaystyle \frac{2 \sqrt{39} e^{- 5000 t} \sin{\left(5000 \sqrt{39} t \right)}}{39}\)

Use lambdify to generate a function of time.

func_node_2_voltage_t = lambdify(t, node_2_voltage_t) 

The results are verified by LTSpice as shown below.

The current in the capacitor can be obtained by looking at the current from \(V_1\) which is the same as \(I_{V}\).

V1_current_s = U_ic[I_V1].nsimplify()
V1_current_s

\(\displaystyle - \frac{1000}{s^{2} + 10000 s + 1000000000}\)

V1_current_t = inverse_laplace_transform(V1_current_s, s, t)
V1_current_t

\(\displaystyle - \frac{\sqrt{39} e^{- 5000 t} \sin{\left(5000 \sqrt{39} t \right)}}{195}\)

func_V1_current_t = lambdify(t, V1_current_t) 

The current in \(L_1\) can also be obtained as follows:

L1_current_s = U_ic[I_L1].nsimplify()
L1_current_s

\(\displaystyle \frac{1000}{s^{2} + 10000 s + 1000000000}\)

L1_current_t = inverse_laplace_transform(L1_current_s, s, t)
L1_current_t

\(\displaystyle \frac{\sqrt{39} e^{- 5000 t} \sin{\left(5000 \sqrt{39} t \right)}}{195}\)

func_L1_current_t = lambdify(t, L1_current_t) 

The plot below shows the current flowing in \(V_1\) and \(L_1\) versus time.

x_axis = np.linspace(0, 1e-3, 2000, endpoint=True)
fig, ax1 = plt.subplots()
ax1.set_ylabel('v(t), volts')
ax1.set_xlabel('time, ms')

# plot voltages
plt.plot(x_axis*1000, func_node_1_voltage_t(x_axis),'-g',label='$v_1(t)$')
plt.plot(x_axis*1000, func_node_2_voltage_t(x_axis),'-b',label='$V_2(t)$')
plt.ylim((-0.8,1.6))
ax1.tick_params(axis='y')
plt.legend(loc='upper right')
plt.grid()

# instantiate a second y-axes that shares the same x-axis
ax2 = ax1.twinx()
color = 'k'

# plot currents
plt.plot(x_axis*1000, func_V1_current_t(x_axis),'-.r',label='$i_{V_1}(t)$')
plt.plot(x_axis*1000, func_L1_current_t(x_axis),'-.c',label='$i_{L_1}(t)$')
plt.ylim((-0.03,0.03))

ax2.set_ylabel('i(t), amps',color=color)
ax2.tick_params(axis='y', labelcolor=color)

plt.legend(loc='lower right')
plt.title('Voltage and Current')
plt.show()

The results obtained above can be verified by comparing the LTSpice solution plotted below. The above matches LTSpice when:

Figure 10.3: LTSpice simulation results for capacitor initial conditions.

10.5 Inductor Initial Conditions

The initial conditions for an inductor are explored using the circuit shown below. The circuit elements are arranged in parallel. \(I_1\) is used to set the initial current present in \(L_1\) at \(t=0\).

Figure 10.4: Inductor initial conditions example circuit.

The netlist for the circuit was exported from LTSpice and copied below.

Example_2_net_list = '''
* Initial_conditions_example_2_v1.asc
L1 1 0 1e-3
C1 1 0 1e-6
R1 1 0 100
I1 0 1 1
'''

The MNA equations are generated from the function SymMNA.smna.

report, network_df, i_unk_df, A, X, Z = SymMNA.smna(Example_2_net_list)

The code below assembles the network equations from the MNA matrices and displays the equations.

# Put matrices into SymPy 
X = Matrix(X)
Z = Matrix(Z)

# put the matrices into equation form 
NE_sym = Eq(A*X,Z)

# free symbols are entered as SymPy variables
var(str(NE_sym.free_symbols).replace('{','').replace('}',''))

# the element values are put into a dictionary
element_values = SymMNA.get_part_values(network_df) # get element values from netlist

# display the equations
temp = ''
for i in range(shape(NE_sym.lhs)[0]):
    temp += '${:s} = {:s}$<br>'.format(latex(NE_sym.rhs[i]),latex(NE_sym.lhs[i]))
Markdown(temp)

\(I_{1} = I_{L1} + v_{1} \left(C_{1} s + \frac{1}{R_{1}}\right)\)
\(0 = - I_{L1} L_{1} s + v_{1}\)

Initial conditions for \(L_1\) is set by making \(I_1\) a step current equal to the value of the initial current condition. \(1/s\) is substituted for the value of \(I_1\).

element_values[I1] = element_values[I1]/s
NE_ic = NE_sym.subs(element_values)

# display the equations
temp = ''
for i in range(shape(NE_ic.lhs)[0]):
    temp += '${:s} = {:s}$<br>'.format(latex(NE_ic.rhs[i]),latex(NE_ic.lhs[i]))
Markdown(temp)

\(\frac{1.0}{s} = I_{L1} + v_{1} \left(1.0 \cdot 10^{-6} s + 0.01\right)\)
\(0 = - 0.001 I_{L1} s + v_{1}\)

Solve the network equations and display the results.

U_ic = solve(NE_ic,X)

temp = ''
for i in U_ic.keys():
    temp += '${:s} = {:s}$<br>'.format(latex(i),latex(U_ic[i]))

Markdown(temp)

\(I_{L1} = \frac{1000000000.0}{s^{3} + 10000.0 s^{2} + 1000000000.0 s}\)
\(v_{1} = \frac{1000000.0}{s^{2} + 10000.0 s + 1000000000.0}\)

The voltage at node 1 is obtained as follows:

node_1_voltage_s = U_ic[v1].nsimplify()
node_1_voltage_s

\(\displaystyle \frac{1000000}{s^{2} + 10000 s + 1000000000}\)

node_1_voltage_t = inverse_laplace_transform(node_1_voltage_s, s, t)
node_1_voltage_t

\(\displaystyle \frac{200 \sqrt{39} e^{- 5000 t} \sin{\left(5000 \sqrt{39} t \right)}}{39}\)

Use lambdify to generate a function of time.

func_node_1_voltage_t = lambdify(t, node_1_voltage_t) 

As described above, the MNA solution for \(L_1\) is the zero state solution for the inductor, therefore, the initial condition of \(1/s\) needs to be manually included. The current through \(L_1\) can be obtained by:

L1_current_s = U_ic[I_L1].nsimplify() - 1/s
L1_current_s

\(\displaystyle \frac{1000000000}{s^{3} + 10000 s^{2} + 1000000000 s} - \frac{1}{s}\)

The IPython magic function is used to track the time it takes for SymPy to obtain the inverse Laplace transform, in this case for my computer is a bit over 1 minute.

%%time
L1_current_t = inverse_laplace_transform(L1_current_s, s, t)
CPU times: user 17.5 ms, sys: 110 μs, total: 17.6 ms
Wall time: 17.5 ms
L1_current_t

\(\displaystyle - \frac{\sqrt{39} e^{- 5000 t} \sin{\left(5000 \sqrt{39} t \right)}}{39} - e^{- 5000 t} \cos{\left(5000 \sqrt{39} t \right)}\)

func_L1_current_t = lambdify(t, L1_current_t) 

\(C_1\) current is the same as the current in \(V=I(1/sC)\).

U_ic[v1]

\(\displaystyle \frac{1000000.0}{s^{2} + 10000.0 s + 1000000000.0}\)

U_ic[v1]/(1/(s*C1))

\(\displaystyle \frac{1000000.0 C_{1} s}{s^{2} + 10000.0 s + 1000000000.0}\)

(U_ic[v1]/(1/(s*C1))).subs({C1:1e-6})

\(\displaystyle \frac{1.0 s}{s^{2} + 10000.0 s + 1000000000.0}\)

C1_current_s = (U_ic[v1]/(1/(s*C1))).subs({C1:1e-6}).nsimplify()
C1_current_s

\(\displaystyle \frac{s}{s^{2} + 10000 s + 1000000000}\)

The IPython magic function is used to track the time it takes for SymPy to obtain the inverse Laplace transform, in this case for my computer the time is shown below.

%%time
C1_current_t = inverse_laplace_transform(C1_current_s, s, t)
CPU times: user 11.5 ms, sys: 0 ns, total: 11.5 ms
Wall time: 11.4 ms
C1_current_t

\(\displaystyle - \frac{\sqrt{39} e^{- 5000 t} \sin{\left(5000 \sqrt{39} t \right)}}{39} + e^{- 5000 t} \cos{\left(5000 \sqrt{39} t \right)}\)

func_C1_current_t = lambdify(t, C1_current_t) 

The plot below shows the inductor current versus time.

x_axis = np.linspace(0, 1e-3, 2000, endpoint=True)
fig, ax1 = plt.subplots()
ax1.set_ylabel('v(t), volts')
ax1.set_xlabel('time, ms')

# plot voltages
plt.plot(x_axis*1000, func_node_1_voltage_t(x_axis),'-g',label='$v_1(t)$')
plt.ylim((-16,28))
ax1.tick_params(axis='y')
plt.legend(loc='upper right')
plt.grid()

# instantiate a second y-axes that shares the same x-axis
ax2 = ax1.twinx()
color = 'k'

# plot currents
plt.plot(x_axis*1000, func_C1_current_t(x_axis),'-.b',label='$i_{C_1}(t)$')
plt.plot(x_axis*1000, func_L1_current_t(x_axis),'-.r',label='$i_{L_1}(t)$')
plt.ylim((-1.0,1.2))

ax2.set_ylabel('i(t), amps',color=color)
ax2.tick_params(axis='y', labelcolor=color)

plt.legend(loc='lower right')
plt.title('Voltage and Current')
plt.show()

The capacitor current and node voltage agree with the LTSpice solution.

Figure 10.5: LTSpice simulation results for inductor initial conditions.

10.6 Initial Conditions Test Circuit

In this example, a circuit with initial conditions consisting of a capacitor with an initial voltage and an inductor with initial current is analysed. The circuit in Figure 10.6 has seven branches and four nodes. \(V_1\) represents the initial voltage on \(C_2\) and \(I_1\) represents the initial current in \(L_2\).

Figure 10.6: Initial conditions test circuit.

The circuit was drawn using LTSpice and the netlist was pasted into the code.

Example_3_net_list = '''
* Initial_conditions_example_3_v1.asc
L1 1 2 2e-3
C2 2 4 0.5e-6
R1 3 0 10
C1 1 0 1e-6
L2 1 0 1e-3
V1 4 3 5
I1 1 0 1
'''

The MNA equations are generated from the function SymMNA.smna.

report, network_df, i_unk_df, A, X, Z = SymMNA.smna(Example_3_net_list)

The code below assembles the network equations from the MNA matrices and displays the equations.

# Put matrices into SymPy 
X = Matrix(X)
Z = Matrix(Z)

# put the matrices into equation form 
NE_sym = Eq(A*X,Z)

# free symbols are entered as SymPy variables
var(str(NE_sym.free_symbols).replace('{','').replace('}',''))

# the element values are put into a dictionary
element_values = SymMNA.get_part_values(network_df) # get element values from netlist

# display the equations
temp = ''
for i in range(shape(NE_sym.lhs)[0]):
    temp += '${:s} = {:s}$<br>'.format(latex(NE_sym.rhs[i]),latex(NE_sym.lhs[i]))
Markdown(temp)

\(- I_{1} = C_{1} s v_{1} + I_{L1} + I_{L2}\)
\(0 = C_{2} s v_{2} - C_{2} s v_{4} - I_{L1}\)
\(0 = - I_{V1} + \frac{v_{3}}{R_{1}}\)
\(0 = - C_{2} s v_{2} + C_{2} s v_{4} + I_{V1}\)
\(V_{1} = - v_{3} + v_{4}\)
\(0 = - I_{L1} L_{1} s + v_{1} - v_{2}\)
\(0 = - I_{L2} L_{2} s + v_{1}\)

The initial conditions for \(C_2\) and \(L_2\) are set by \(V_1\) and \(I_1\). A step function with a value of 5 is used for \(V_1\) and 1 for \(I_1\).

element_values[V1] = element_values[V1]/s
element_values[I1] = element_values[I1]/s
NE_ic = NE_sym.subs(element_values)

# display the equations
temp = ''
for i in range(shape(NE_ic.lhs)[0]):
    temp += '${:s} = {:s}$<br>'.format(latex(NE_ic.rhs[i]),latex(NE_ic.lhs[i]))
Markdown(temp)

\(- \frac{1.0}{s} = I_{L1} + I_{L2} + 1.0 \cdot 10^{-6} s v_{1}\)
\(0 = - I_{L1} + 5.0 \cdot 10^{-7} s v_{2} - 5.0 \cdot 10^{-7} s v_{4}\)
\(0 = - I_{V1} + 0.1 v_{3}\)
\(0 = I_{V1} - 5.0 \cdot 10^{-7} s v_{2} + 5.0 \cdot 10^{-7} s v_{4}\)
\(\frac{5.0}{s} = - v_{3} + v_{4}\)
\(0 = - 0.002 I_{L1} s + v_{1} - v_{2}\)
\(0 = - 0.001 I_{L2} s + v_{1}\)

Solve the network equations and display the results.

U_ic = solve(NE_ic,X)

temp = ''
for i in U_ic.keys():
    temp += '${:s} = {:s}$<br>'.format(latex(i),latex(U_ic[i]))

Markdown(temp)

\(I_{L1} = \frac{- 2500.0 s^{2} - 500000000.0 s - 2500000000000.0}{s^{4} + 5000.0 s^{3} + 2500000000.0 s^{2} + 5000000000000.0 s + 1.0 \cdot 10^{18}}\)
\(I_{L2} = \frac{- 1000000000.0 s^{2} - 2500000000000.0 s - 1.0 \cdot 10^{18}}{s^{5} + 5000.0 s^{4} + 2500000000.0 s^{3} + 5000000000000.0 s^{2} + 1.0 \cdot 10^{18} s}\)
\(I_{V1} = \frac{- 2500.0 s^{2} - 500000000.0 s - 2500000000000.0}{s^{4} + 5000.0 s^{3} + 2500000000.0 s^{2} + 5000000000000.0 s + 1.0 \cdot 10^{18}}\)
\(v_{1} = \frac{- 1000000.0 s^{2} - 2500000000.0 s - 1.0 \cdot 10^{15}}{s^{4} + 5000.0 s^{3} + 2500000000.0 s^{2} + 5000000000000.0 s + 1.0 \cdot 10^{18}}\)
\(v_{2} = \frac{5.0 s^{3} + 2500000000.0 s - 1.0 \cdot 10^{15}}{s^{4} + 5000.0 s^{3} + 2500000000.0 s^{2} + 5000000000000.0 s + 1.0 \cdot 10^{18}}\)
\(v_{3} = \frac{- 25000.0 s^{2} - 5000000000.0 s - 25000000000000.0}{s^{4} + 5000.0 s^{3} + 2500000000.0 s^{2} + 5000000000000.0 s + 1.0 \cdot 10^{18}}\)
\(v_{4} = \frac{5.0 s^{4} + 7500000000.0 s^{2} + 5.0 \cdot 10^{18}}{s^{5} + 5000.0 s^{4} + 2500000000.0 s^{3} + 5000000000000.0 s^{2} + 1.0 \cdot 10^{18} s}\)

The voltage at node 1 is:

node_1_voltage_s = U_ic[v1].nsimplify()
node_1_voltage_s

\(\displaystyle \frac{- 1000000 s^{2} - 2500000000 s - 1000000000000000}{s^{4} + 5000 s^{3} + 2500000000 s^{2} + 5000000000000 s + 1000000000000000000}\)

The function listed below,Inv_Laplace_By_PFE, performs an inverse Laplace transform on functions that are too large for the standard SymPy built in function, inverse_laplace_transform. My code below follows the answer given by Davide_sd in zorka5 (2024).

def Inv_Laplace_By_PFE(func_s, start, stop, num_points):
    """Inverse Laplace Transform By Partial Fraction Expansion (Inv_Laplace_By_PFE).
    This code was inspired by the answer given by Davide_sd for the question at
    https://stackoverflow.com/questions/77876224/calculating-inverse-laplace-transform-using-python-or-matlab 

    Args:
        func_s: a rational function of 's' with real valued coefficients
        The following parameters are used in np.linspace, which is called at the end of this procedure.
        start: start time
        stop: stop time
        num_points: number of points

    Returns:
        Array of the time response. 
    """
    n, d = fraction(func_s)

    n = n.expand() # expand the numerator to get the individual additive terms

    # Each of the numerator terms can be put over the common denominator.
    if len(Add.make_args(n)) > 1: # check for the number of additive terms in the numerator
        terms = [a / d for a in n.args]
    else:
        terms = [n/d]
    
    '''
    The following code processes each of the terms obtained above. 
    - the SciPy function [residue](https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.residue.html) is used to get the residues and poles of the partial-fraction expansion
    - r: Residues corresponding to the poles. For repeated poles, the residues are ordered to correspond to ascending by power fractions.
    - p: Poles ordered by magnitude in ascending order.
    - k: Coefficients of the direct polynomial term.
    When computing the inverse Laplace transform, the Coefficients (k) are ignored since these transform to a Dirac delta function, $\delta (t)$ and don't need to be plotted.
    '''
    
    N = []
    for p1 in terms:

        # use the SciPy residue function to get the partial-fraction expansion residues and poles
        n, d = fraction(p1)
        cn = Poly(n, s).all_coeffs()
        cd = Poly(d, s).all_coeffs()
        r, p, k = signal.residue(cn, cd, tol=0.001, rtype='avg')

        # build a symbolic expression for each of the residues and find the inverse Laplace of each one
        z = 0
        for i in range(len(r)):
            m = (r[i]/(s-p[i]))
            z += inverse_laplace_transform(m, s, t)

        N.append(z)

    # Each of these terms can be converted to a function using SymPy's [lambdify](https://docs.sympy.org/latest/modules/utilities/lambdify.html) function.
    # Define the values for the x-axis of the plot and put into an array for plotting.
    x = np.linspace(start, stop, num_points, endpoint=True)
    array_x = np.zeros(len(x),dtype = complex)
    for p in N:
        array_x += lambdify(t, p)(x)

    return array_x

Call the function Inv_Laplace_By_PFE and get an array of the transient response for node 1.

node_1_x_values = Inv_Laplace_By_PFE(node_1_voltage_s, 0, 1e-3, 2000)

Get the expression for node 2, display and do the inverse Laplace transform.

node_2_voltage_s = U_ic[v2].nsimplify()
node_2_voltage_s

\(\displaystyle \frac{5 s^{3} + 2500000000 s - 1000000000000000}{s^{4} + 5000 s^{3} + 2500000000 s^{2} + 5000000000000 s + 1000000000000000000}\)

node_2_x_values = Inv_Laplace_By_PFE(node_2_voltage_s, 0, 1e-3, 2000)

Get the expression for node 3, display and do the inverse Laplace transform.

node_3_voltage_s = U_ic[v3].nsimplify()
node_3_voltage_s

\(\displaystyle \frac{- 25000 s^{2} - 5000000000 s - 25000000000000}{s^{4} + 5000 s^{3} + 2500000000 s^{2} + 5000000000000 s + 1000000000000000000}\)

node_3_x_values = Inv_Laplace_By_PFE(node_3_voltage_s, 0, 1e-3, 2000)

The plot below shows the node voltages versus time.

x = np.linspace(0, 1e-3, 2000, endpoint=True)

plt.title('Node voltage vs time')

plt.plot(x*1000, np.real(node_1_x_values),'-g',label='v1(t)')
plt.plot(x*1000, np.real(node_2_x_values),'-b',label='v2(t)')
plt.plot(x*1000, np.real(node_3_x_values),'-r',label='v3(t)')
plt.ylim((-42,35))

plt.ylabel('v(t), volts')
plt.xlabel('time, ms')

plt.legend()
plt.grid()
plt.show()

The results obtained from the Python code agree with LTSpice as shown below.

LTSpice simulation results for initial conditions example circuit.

10.7 LRC Circuit with Switch

This example comes from a question on EESE, Solving for current in RLC circuit with Laplace and steady-state solution. The circuit in the question is from Alexander and Sadiku (2021), example 8.8. The schematic for the question has been drawn in LTSpice and is shown in Figure 10.7.

This circuit has a switch, \(S_1\) that closes at \(t=0\) and a voltage source, \(V_1\) that steps from 30 to 0 volts at \(t=0\). Prior to \(t=0\), the voltage at node 3 is 15 volts, since \(R_1\) and \(R_2\) act as a voltage divider. This puts the voltage across \(C_1\) to be 15 volts. The current source \(I_1\) is an independednt current source and is only connected to \(L_1\) prior to \(S_1\) closing. This causes a 4 amp current to flow in \(L_1\) prior to \(t=0\).

Figure 10.7: LRC circuit with switch schematic.

To solve for the voltages and currents after \(S_1\) closes, first we need to find the DC steady state for \(t < 0\). We assume the circuit has been connected in its pre-switch state long enough to reach steady state. By inspection we determine the capacitor voltage \(v_C(0^-)=15\) and inductor current \(i_L(0^-)=4\).

The post switched circuit (\(t = 0^+\)) is shown in Figure 10.8. In this schematic, \(I_2\) represents the initial current flowing in the inductor at \(t=0\) and \(V_2\) represents the voltage on \(C_1\) at \(t=0\). Notice that the value of \(V_1\) has been set to zero.

Figure 10.8: Schematic including the circuit’s initial conditions.

The netlist for the schematic was exported from LTSpice and copied into the code cell below.

Example_5_net_list = '''
* Initial_conditions_example_5_v2.asc
V1 2 0 0
I1 0 1 4
L1 1 0 20
R1 1 0 20
R2 2 1 20
C1 1 3 8e-3
I2 1 0 4
V2 3 0 15
'''

The MNA equations are generated from the function SymMNA.smna.

report, network_df, i_unk_df, A, X, Z = SymMNA.smna(Example_5_net_list)

The code below assembles the network equations from the MNA matrices and displays the equations.

# Put matrices into SymPy 
X = Matrix(X)
Z = Matrix(Z)

# put the matrices into equation form 
NE_sym = Eq(A*X,Z)

# free symbols are entered as SymPy variables
var(str(NE_sym.free_symbols).replace('{','').replace('}',''))

# the element values are put into a dictionary
element_values = SymMNA.get_part_values(network_df) # get element values from netlist

# display the equations
temp = ''
for i in range(shape(NE_sym.lhs)[0]):
    temp += '${:s} = {:s}$<br>'.format(latex(NE_sym.rhs[i]),latex(NE_sym.lhs[i]))
Markdown(temp)

\(I_{1} - I_{2} = - C_{1} s v_{3} + I_{L1} + v_{1} \left(C_{1} s + \frac{1}{R_{2}} + \frac{1}{R_{1}}\right) - \frac{v_{2}}{R_{2}}\)
\(0 = I_{V1} - \frac{v_{1}}{R_{2}} + \frac{v_{2}}{R_{2}}\)
\(0 = - C_{1} s v_{1} + C_{1} s v_{3} + I_{V2}\)
\(V_{1} = v_{2}\)
\(V_{2} = v_{3}\)
\(0 = - I_{L1} L_{1} s + v_{1}\)

The initial conditions for \(C_1\) and \(L_1\) are set by \(V_2\) and \(I_2\). All the independent sources are converted to step functions by dividing by the Laplace variable as shown in the code below.

element_values[V1] = element_values[V1]/s
element_values[V2] = element_values[V2]/s
element_values[I1] = element_values[I1]/s
element_values[I2] = element_values[I2]/s
NE_ic = NE_sym.subs(element_values)

# display the equations
temp = ''
for i in range(shape(NE_ic.lhs)[0]):
    temp += '${:s} = {:s}$<br>'.format(latex(NE_ic.rhs[i]),latex(NE_ic.lhs[i]))
Markdown(temp)

\(0 = I_{L1} - 0.008 s v_{3} + v_{1} \left(0.008 s + 0.1\right) - 0.05 v_{2}\)
\(0 = I_{V1} - 0.05 v_{1} + 0.05 v_{2}\)
\(0 = I_{V2} - 0.008 s v_{1} + 0.008 s v_{3}\)
\(0 = v_{2}\)
\(\frac{15.0}{s} = v_{3}\)
\(0 = - 20.0 I_{L1} s + v_{1}\)

Solve the network equations and display the results.

U_ic = solve(NE_ic,X)

temp = ''
for i in U_ic.keys():
    temp += '${:s} = {:s}$<br>'.format(latex(i),latex(U_ic[i]))

Markdown(temp)

\(I_{L1} = \frac{3.0}{4.0 s^{2} + 50.0 s + 25.0}\)
\(I_{V1} = \frac{3.0 s}{4.0 s^{2} + 50.0 s + 25.0}\)
\(I_{V2} = \frac{- 6.0 s - 3.0}{4.0 s^{2} + 50.0 s + 25.0}\)
\(v_{1} = \frac{60.0 s}{4.0 s^{2} + 50.0 s + 25.0}\)
\(v_{2} = 0.0\)
\(v_{3} = \frac{15.0}{s}\)

The voltage at node 1 is:

node_1_voltage_s = U_ic[v1].nsimplify()
node_1_voltage_s

\(\displaystyle \frac{60 s}{4 s^{2} + 50 s + 25}\)

The SymPy symbol t is defined to be positive for \(t \gt 0\)

t = symbols('t',positive=True)  # t > 0

The symbolic inverse Laplace transform can be found by using the SymPy function inverse_laplace_transform.

node_1_voltage_t = inverse_laplace_transform(node_1_voltage_s, s, t)
node_1_voltage_t

\(\displaystyle - \frac{25 \sqrt{21} e^{- \frac{25 t}{4}} \sinh{\left(\frac{5 \sqrt{21} t}{4} \right)}}{7} + 15 e^{- \frac{25 t}{4}} \cosh{\left(\frac{5 \sqrt{21} t}{4} \right)}\)

Use lambdify to generate a function of time.

func_node_1_voltage_t = lambdify(t, node_1_voltage_t) 

The current through \(L_1\) can be obtained by including the contribition from the parallel current source included manually in the expression below:

L1_current_s = U_ic[I_L1].nsimplify() + 4/s
L1_current_s

\(\displaystyle \frac{3}{4 s^{2} + 50 s + 25} + \frac{4}{s}\)

The IPython magic function is used to track the time it takes for SymPy to obtain the inverse Laplace transform, in this case for my computer is a bit over 10 ms.

%%time
L1_current_t = inverse_laplace_transform(L1_current_s, s, t)
CPU times: user 15.4 ms, sys: 0 ns, total: 15.4 ms
Wall time: 15.2 ms
L1_current_t

\(\displaystyle 4 + \frac{\sqrt{21} e^{- \frac{25 t}{4}} \sinh{\left(\frac{5 \sqrt{21} t}{4} \right)}}{35}\)

Use lambdify to generate a function of time.

func_L1_current_t = lambdify(t, L1_current_t) 

The plot below shows the the voltage for node 1 and inductor current versus time.

x_axis = np.linspace(0, 10, 2000, endpoint=True)
fig, ax1 = plt.subplots()
ax1.set_ylabel('v(t), volts')
ax1.set_xlabel('time, sec')

# plot voltages
plt.plot(x_axis, func_node_1_voltage_t(x_axis),'-r',label='$v_1(t)$')
plt.ylim((-2,22))
ax1.tick_params(axis='y')
plt.legend(loc='upper right')
plt.grid()

# instantiate a second y-axes that shares the same x-axis
ax2 = ax1.twinx()
color = 'k'

# plot currents
plt.plot(x_axis, func_L1_current_t(x_axis),'-.b',label='$i_{L_1}(t)$')
plt.ylim((3.995,4.055))

ax2.set_ylabel('i(t), amps',color=color)
ax2.tick_params(axis='y', labelcolor=color)

plt.legend(loc='lower right')
plt.title('Voltage and Current')
plt.show()

To vefify the Python results, the LTSpice simulation results are shown below. The simulation starts at \(t=0\), but \(S_1\) is controlled by \(V_1\) and closes at \(t=1\). As shown in the plot below, the simulation matches the results from Python.

10.8 Summary

In this chapter, MNA was used to analyze circuits which have initial conditions defined for the capacitors and inductors.

  • A capacitor’s initial voltage condition was represented by step function voltage source
  • An inductor’s initial current condition was represented by a step function current source