I am trying to solve a certain question;
Question: There is a 2-d square grid of nodes, with resistors placed randomly to connect those nodes. And eventually, those connections may extend from one end to the other end. Say for now, I am only considering left and right side. I want to know what is the total equivalent resistance of the network, which leads the current from one end to the other.
(For simplicity, consider only one main cluster to extend end to end)
Now, I saw this code for uniform grid case: Resistor mesh - Rosetta Code
using SparseArrays
N = 3
D1 = sparse(I,N-1,N) - spdiagm(N-1,N, 1=>ones(N-1))
D = [ kron(D1, sparse(I,N,N)); kron(sparse(I,N,N), D1) ]
i, j = 1 , 9
b = zeros(N^2); b[i], b[j] = 1, -1
v = (D' * D) \ b
v[i] - v[j]
And made me think I can maybe use this for random network too.
I tried to make an Incidence matrix from a given random lattice (from my previous attempt at percolation problem). And, formed the Laplacian matrix using Incidence matrix.
For sanity check, I tried to make 3x3 grid with all connections. This is what I got for Incidence matrix.
12Ă—9 Matrix{Float64}:
-1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0
-1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 -1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0
0.0 -1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 -1.0 0.0 0.0 1.0 0.0 0.0 0.0
0.0 0.0 0.0 -1.0 0.0 0.0 1.0 0.0 0.0
0.0 0.0 0.0 -1.0 1.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 -1.0 0.0 0.0 1.0 0.0
0.0 0.0 0.0 0.0 -1.0 1.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 -1.0 0.0 0.0 1.0
0.0 0.0 0.0 0.0 0.0 0.0 -1.0 1.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 -1.0 1.0
And the following is the Laplacian matrix
9Ă—9 Matrix{Float64}:
2.0 -1.0 0.0 -1.0 0.0 0.0 0.0 0.0 0.0
-1.0 3.0 -1.0 0.0 -1.0 0.0 0.0 0.0 0.0
0.0 -1.0 2.0 0.0 0.0 -1.0 0.0 0.0 0.0
-1.0 0.0 0.0 3.0 -1.0 0.0 -1.0 0.0 0.0
0.0 -1.0 0.0 -1.0 4.0 -1.0 0.0 -1.0 0.0
0.0 0.0 -1.0 0.0 -1.0 3.0 0.0 0.0 -1.0
0.0 0.0 0.0 -1.0 0.0 0.0 2.0 -1.0 0.0
0.0 0.0 0.0 0.0 -1.0 0.0 -1.0 3.0 -1.0
0.0 0.0 0.0 0.0 0.0 -1.0 0.0 -1.0 2.0
Which is same output as the rosseta code;
9Ă—9 SparseMatrixCSC{Float64, Int64} with 33 stored entries:
2.0 -1.0 â‹… -1.0 â‹… â‹… â‹… â‹… â‹…
-1.0 3.0 -1.0 â‹… -1.0 â‹… â‹… â‹… â‹…
â‹… -1.0 2.0 â‹… â‹… -1.0 â‹… â‹… â‹…
-1.0 â‹… â‹… 3.0 -1.0 â‹… -1.0 â‹… â‹…
â‹… -1.0 â‹… -1.0 4.0 -1.0 â‹… -1.0 â‹…
â‹… â‹… -1.0 â‹… -1.0 3.0 â‹… â‹… -1.0
â‹… â‹… â‹… -1.0 â‹… â‹… 2.0 -1.0 â‹…
â‹… â‹… â‹… â‹… -1.0 â‹… -1.0 3.0 -1.0
â‹… â‹… â‹… â‹… â‹… -1.0 â‹… -1.0 2.0
But, the problem comes when I try to solve the above linear equation;
M = A' * A
b = zeros(9)
b[1] = 1
b[6] = -1
x = M \ b
The result is somehow, always SingularException(some number) [numbers I remember on top of my head are 0, 1, 13, 9, 2 , etc]
Can someone please tell me whats going on? Because essentially, both the laplacian matrices are same, but this code doesn’t yield the result. Is it because of Sparse matrix that works in the rosetta code?
(Now, when I use the sparse(M), it does give me the result of the completely connected grid problem. But, then why does the normal matrix give error?)
Edit: Typo in last code block