
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


function resistormesh(integer ni, nj, ai, aj, bi, bj)
    integer n = ni*nj, k, c
    sequence A = repeat(repeat(0,n),n),
             B = repeat({0},n)
    for i=1 to ni do
        for j=1 to nj do
            k = (i-1)*nj + j--1
            if i=ai and j=aj then
                A[k,k] = 1
            else
                c = 0
                if i<ni then c += 1; A[k,k+nj] = -1 end if
                if i>1 then  c += 1; A[k,k-nj] = -1 end if
                if j<nj then c += 1; A[k,k+1] = -1 end if
                if j>1 then  c += 1; A[k,k-1] = -1 end if
                A[k,k] = c
            end if
        end for
    end for
    k = (bi-1)*nj +bj
    B[k,1] = 1
    A = inverse(A)
    B = matrix_mul(A,B)
    return B[k,1]
end function
 
printf(1,"Resistance = %.13f ohms\n",resistormesh(10, 10, 2, 2, 8, 7))
