# Gaussian Elimination ```Gaussian Elimination
N equations in N unknowns (x, y, z)
A,B,C,D are known constants:
A1x + B1y + C1z = D1
A2x + B2y + C2z = D2
A3x + B3y + C3z = D3
Put the constants into a matrix:
A1 B1 C1 D1
A2 B2 C2 D2
A3 B3 C3 D3
Use linear operators to transform the matrix to:
1 0 0 Dx
0 1 0 Dy
0 0 1 Dz
Dx, Dy Dz are the values for x y and z that make the first equations true – thus
solving the equations.
Gaussian Elimination
Two basic linear operations used on matrix:
• */ by constant
• add/sub rows from each other:
A1 B1 C1 D1
A2 B2 C2 D2
A3 B3 C3 D3
Divide first row by A1, becomes:
1 B1/A1 C1/A1 D1/A1
Subtract A2 times first row from second row, becomes:
0 B2-B1A2 C2-C1A2 D2-D1A2
Subtract A3 times first row from third row, becomes:
0 B3-B1A3 C3-C1A3 D3-D1A3
Now we have the first column in the right form, continue with second row, second
column, etc.
Gaussian Elimination
int gauss(double **M, int maxrow, int maxcol) {
int diag, row, col;
double pivot, pivot2;
for (diag = 0; diag &lt; maxrow; diag++) { /* for each row = variable = diagonal element */
pivot = M[diag][diag];
if (pivot == 0) { printf(&quot;Gauss: Singular Matrix!\n&quot;); return(1); }
for (col = diag; col &lt; maxcol; col++) {
/* from diagonal to right, divide out by the pivot diagonal element */
M[diag][col] /= pivot;
}
for(row = 0; row &lt; maxrow; row++)
/* for all other rows subtract scalled diag row, zeroing pivot column */
if (row != diag) {
{
/* scale all elements of diag row by value which will zero
diag element of this row, and subtract from this row */
pivot2 = M[row][diag];
for (col = diag; col &lt; maxcol; col++)
M[row][col] -= M[diag][col]*pivot2;
}
}
return(0);
}
Nodal Analysis: NA
For a linear circuit, solve Kirchhoff's current law (KCL) and
voltage law (KVL) using a matrix formulation, just Ohm’s
law in a matrix:
|G| x |V| = |I|
G is conduction matrix (1/R)
V is vector of unknown node voltages
I is vector of known node currents
Solve by inverting G and then multiplying I to get V
or concatenating |G||I| and performing Gaussian elimination
(or equivalent) to get V
Assumes G and I are constant
Modified Nodal Analysis: MNA
•
•
Matrices are a little more complex, solution is the same
AX = Z
• General form of linear equations:
• used for a lot of systems beyond circuits
• A matrix is conduction and voltage sources
• G is conduction, B is voltages C = BT, D = 0
• X is unknown voltages and currents
⎡G B ⎤
• Z is known voltages, currents
⎡i ⎤
⎡v ⎤
X =⎢ ⎥ Z =⎢ ⎥
A=⎢
⎥
⎣ j⎦
⎣e ⎦
⎣C D ⎦
•Solve by matrix inversion and multiplication:
−1
A Z=X
⎡G B ⎤
⎢C D ⎥
⎣
⎦
−1
⎡i ⎤ ⎡v ⎤
&times;⎢ ⎥ = ⎢ ⎥
⎣e ⎦ ⎣ j ⎦
Conduction Matrix
• G is matrix of conductions (1/R) = g
• For each impedance:
• if R is on node k, add value of 1/R on
diagonal (k,k).
– If other end of R is on ground, stop
– Else, if R is on node j add 1/R to other
diagonal (j,j) and add (-1/R) on elements (k,j)
and (j,k)
Voltage Source Matrices
• B has a column for each voltage source, V
• If V has a positive terminal on node j, put 1
in row j.
– If other end of V is on ground, stop
– Else, if V has a negative terminal on node k
put -1 on row k
• C is B transpose (row for each source…)
• D (corner) is zero if no dependent sources
Vector of Unknowns
• X is composed of the unknown node
voltages and voltage source currents.
• Vk for each unknown voltage at node k
• Ji for the current through each voltage
source i (in the direction of positive
terminal through the device to the
negative terminal).
Vector of Knowns
• Z is composed of the known node currents and
voltage source voltages.
• Ik for each known current at node k. This is zero
unless there is a current source at node k. if so,
the value is +current positive node. If other end
is not on ground, then value is –current at other
node.
• Vj for the value of voltage source j. If other end is
not on ground, then value of –voltage at other
node.
Example Circuit
AX = Z
1 100
−1 100
0
0
1
−1 100 1 100+1 200+1 400 −1 200
0
0
0
−1 200
1 200
0
0
0
1
0
0
0
0
1 300 0
0 0
v1
0
v2
0
&times; v3 = 100mA
v4 −100mA
ivs
5V
Example Circuit
⎡G B ⎤
A=⎢
⎥
⎣C D ⎦
n1
n2
n3
n4
v1
n1
0.01
-0.01
0
0
1
n2
-0.01
0.0175
-0.005
0
0
n3
n4
0
0
-0.005
0
0.005
0
0 0.003333
0
0
v1
1
0
0
0
0
Example Circuit
5V
32V
12V
-30V
70mA
⎡G B ⎤
⎥
⎢
⎣C D ⎦
0
0
0
0
1
0
80
80
0
0.8
0
80
280
0
0.8
−1
A Z=X
−1
⎡i ⎤
&times;⎢ ⎥
⎣e ⎦
0
0
0
300
0
1
0.8
0.8
0
-0.002
X
0
0
0.1
-0.1
5
⎡v ⎤
=⎢ ⎥
⎣ j⎦
5
12
=
32
-30
0.07
Energy Storage
• Capacitors and Inductors store energy and
thus have voltage (current) dependent
conductance:
g (t ) = i (t ) / v(t )
• Capacitor:
1
v(t ) =
C
t
∫
0
q (t )
i (t )dt =
C
dq (t )
dv(t )
i (t ) =
=C
dt
dt
• Inductor:
t
∫
1
i (t ) =
v(t )dt
L
0
di (t )
v(t ) = L
dt
Need to do numerical integration
x ’n
Two rectangular rules for integration
• xn+1 = xn + h*x’n ( forward Euler)
• xn+1 = xn + h*x’n+1 ( backward
Euler)
Many others: Trapezoid, Gear,
Simpson
Use for integration for charge storage:
vn+1 = vn+ h * v’n+1
vn+1 = vn+ h/c * in+1
Rearrange:
In+1 = (c/h)* vn+1 – (c/h) * vn
Define: Geq = c/h
Gives a Norton Equivalent circuit:
xn
xn+1
h
x’n+1
Capacitor companion model in
MNA
Geq − Geq V n +1 + − Ieq
=
&times;
− Geq Geq V n +1 − + Ieq
• Adds companion model (Norton equivalent
circuit) to G, X and Z matrices.
• Note signs of Ieq nodes
• Ieq must be re-calculated after each time
step:
Ieqn+1 = (V+n+1-V-n+1) * C/h
If h changes, then Geq = C/h must be re-calculated
too!
```