Solving system of linear equations across network in Python

271 Views Asked by At

I have a network and I would like to calculate the voltage drop across the nodes (2-5). I know V1=10 and V6=0. The equations at each node are written as follows. There are 4 equations and 4 unknowns.

2: V1/R12 - V2*(1/R12 + 1/R24) + V4/R24 = 0
3: V1/R13 - V3*(1/R13 + 1/R34 + 1/R35) + V4/R34 + V5/R35 = 0
4: V2/R24 + V3/R34 - V4*(1/R24 + 1/R34 + 1/R46) + V6/R46 = 0
5: V3/R35 - V5*(1/R35 + 1/R56) + V6/R56 = 0

Is there a way to implement it in Python? Manually generating the matrices is tedious for a very large network, say with 100 nodes.

Network

1

There are 1 best solutions below

0
On BEST ANSWER

Here's the code in Visual Basic to reduce an $m \times n$ matrix $(a)$ to RREF (reduce row echelon form).

Public Sub reduce_matrix(a, ByVal m As Integer, ByVal n As Integer, ByRef det As Double)

Dim i, j, k, k1, krow As Integer
Dim amax As Double, kmax As Integer

eps = 1.E-8


krow = 1
det = 1

For i = 1 To n
    
    k1 = 0

    amax = Abs(a(krow, i))
    kmax = 0
    For k = krow To m

    If Abs(a(k, i)) > eps Then
       If Abs(a(k, i)) >= amax Then
          kmax = k
          amax = Abs(a(k, i))
       End If
      
    End If

Next k

If kmax <> 0 Then
   k1 = kmax
End If


If k1 = 0 Then
   det = 0
   GoTo lnext_i
End If

If k1 <> krow Then

' interchange rows k1 and krow

   For j = 1 To n

      t = a(k1, j)
      a(k1, j) = a(krow, j)
      a(krow, j) = t

   Next j
   det = -det
       
End If


'  normalize krow-th row

   t = a(krow, i)

   If Abs(t - 1) > eps Then
   
   For j = i To n
   
   a(krow, j) = a(krow, j) / t
   
   Next j
   
   det = det * t
    
   End If
   
' eleminate elements in column i
    
For i1 = 1 To m

If i1 <> krow Then

   factor = -a(i1, i)
   If factor = 0 Then GoTo lnext_i1
          
   For j = i To n
   
   a(i1, j) = a(i1, j) + factor * a(krow, j)
   
   Next j
   
End If

lnext_i1:
Next i1
    
krow = krow + 1

If krow > m Then Exit For

lnext_i:

Next i



End Sub