
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


 
 PROGRAM RESISTENCE_MESH
 
 !$BASE=1
 
 !$DYNAMIC
 DIM A[0,0]
 
 BEGIN
 
  N=10
  NN=N*N
  !$DIM A[NN,NN+1]
 
  PRINT(CHR$(12);) !CLS
  ! generate matrix data
  NODE=0
  FOR ROW=1 TO N DO
    FOR COL=1 TO N DO
        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
    END FOR
  END FOR
 
  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 DO
    FOR I=J TO R DO
      EXIT IF A[I,J]<>0
    END FOR
    IF I=R+1 THEN
      PRINT("No solution!")
      !$STOP
    END IF
    FOR K=1 TO R+1 DO
      SWAP(A[J,K],A[I,K])
    END FOR
    Y=1/A[J,J]
    FOR K=1 TO R+1 DO
      A[J,K]=Y*A[J,K]
    END FOR
    FOR I=1 TO R DO
      IF I<>J THEN
         Y=-A[I,J]
         FOR K=1 TO R+1 DO
            A[I,K]=A[I,K]+Y*A[J,K]
         END FOR
      END IF
    END FOR
  END FOR
  PRINT("Resistence=";ABS(A[A,NN+1]-A[B,NN+1]))
 END PROGRAM
 