
Task

Given    10×10    grid nodes   (as shown in the image)   interconnected by    1Ω    resistors as shown,

find the resistance between points   A   and   B.



See also

   (humor, nerd sniping)   xkcd.com cartoon


' version 01-07-2018
' compile with: fbc -s console
 
#Define n 10
 
Dim As UInteger nn = n * n
Dim As Double g(-nn To nn +1, -nn To nn +1)
Dim As UInteger node, row, col
 
For row = 1 To n
    For col = 1 To n
        node += 1
        If row > 1 Then
            g(node, node) += 1
            g(node, node - n) = -1
        End If
        If row < n Then
            g(node, node) += 1
            g(node, node + n) = -1
        End If
        If col > 1 Then
            g(node, node) += 1
            g(node, node -1) = -1
        End If
        If col < n Then
            g(node, node) += 1
            g(node, node +1) = -1
        End If
    Next
Next
 
Dim As UInteger ar = 2, ac = 2
Dim As UInteger br = 7, bc = 8
Dim As UInteger a = ac + n * (ar -1)
Dim As UInteger b = bc + n * (br -1)
 
g(a, nn +1) = -1
g(b, nn +1) = 1
 
Print : Print "Nodes a: "; a, " b: "; b
 
' solve linear system using Gauss-Seidel method with pivoting
Dim As UInteger i, j, k
Dim As Double y
 
Do
    For j = 1 To nn
        For i = j To nn
            If g(i, j) <> 0 Then Exit For
        Next
        If i = nn +1 Then
            Print : Print "No solution"
            Exit Do
        End If
        For k = 1 To nn +1
            Swap g(j, k), g(i, k)
        Next
        y = g(j, j)
        For k = 1 To nn +1
            g(j, k) = g(j, k) / y
        Next
        For i = 1 To nn
            If i <> j Then
                y = -g(i, j)
                For k = 1 To nn +1
                    g(i, k) = g(i, k) + y * g(j, k)
                Next
            End If
        Next
    Next
 
    Print
    Print "Resistance ="; Abs(g(a, nn +1) - g(b, nn +1)); " Ohm"
    Exit Do
Loop
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End