
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


  N=10
  NN=N*N
  DIM A(NN,NN+1)
 
  NODE=0
  FOR ROW=1 TO N
    FOR COL=1 TO N
        NODE=NODE+1
        IF ROW>1 THEN
            A(NODE,NODE)=A(NODE,NODE)+1
            A(NODE,NODE-N)=-1
        END IF
        IF ROW<N THEN
            A(NODE,NODE)=A(NODE,NODE)+1
            A(NODE,NODE+N)=-1
        END IF
        IF COL>1 THEN
            A(NODE,NODE)=A(NODE,NODE)+1
            A(NODE,NODE-1)=-1
        END IF
        IF COL<N THEN
            A(NODE,NODE)=A(NODE,NODE)+1
            A(NODE,NODE+1)=-1
        END IF
    NEXT
  NEXT
 
  AR=2 : AC=2 : A=AC+N*(AR-1)
  BR=7 : BC=8 : B=BC+N*(BR-1)
  A(A,NN+1)=-1
  A(B,NN+1)=1
 
  PRINT "Nodes ",A,B
 
  // solve linear system
  // using Gauss-Seidel method 
  // with pivoting
  R=NN
 
  FOR J=1 TO R
    FOR I=J TO R
      IF A(I,J)<>0 BREAK
    NEXT
    IF I=R+1 THEN
      PRINT "No solution!"
      END
    END IF
    FOR K=1 TO R+1
      T = A(J,K)
      A(J,K) = A(I,K)
      A(I,K) = T
    NEXT
    Y=1/A(J,J)
    FOR K=1 TO R+1
      A(J,K)=Y*A(J,K)
    NEXT
    FOR I=1 TO R
      IF I<>J THEN
         Y=-A(I,J)
         FOR K=1 TO R+1
            A(I,K)=A(I,K)+Y*A(J,K)
         NEXT
      END IF
    NEXT
  NEXT
  PRINT "Resistence = "; : PRINT ABS(A(A,NN+1)-A(B,NN+1)) USING "%1.13f"