RosettaCodeData/Task/Resistor-mesh/FutureBasic/resistor-mesh.basic

95 lines
1.6 KiB
Plaintext

// Rosetta Code Resistor Mesh task
//https://rosettacode.org/wiki/Resistor_mesh
// Translation of Yabasic to FutureBasic
window 1,@"Resistor Mesh Task"
#build ShowMoreWarnings NO
short N=10
short NN=100
double A(100,101)
short NODE=0
short ROW,COL
FOR ROW=1 TO N
FOR COL=1 TO N
NODE=NODE+1
IF ROW>1
A(NODE,NODE)=A(NODE,NODE)+1
A(NODE,NODE-N)=-1
END IF
IF ROW<N
A(NODE,NODE)=A(NODE,NODE)+1
A(NODE,NODE+N)=-1
END IF
IF COL>1
A(NODE,NODE)=A(NODE,NODE)+1
A(NODE,NODE-1)=-1
END IF
IF COL<N
A(NODE,NODE)=A(NODE,NODE)+1
A(NODE,NODE+1)=-1
END IF
NEXT
NEXT
short AR,AC,ANode,BNode,BR,BC
// These are columns and rows in the matrix to calculate the position of A and B
//A is row 2 column 2
//B is row 7 column 8
AR=2 : AC=2 : ANode=AC+N*(AR-1)
BR=7 : BC=8 : BNode=BC+N*(BR-1)
A(ANode,NN+1)=-1
A(BNode,NN+1)=1
// solve linear system
// using Gauss-Seidel method
// with pivoting
short R
R=NN
short J,I,K
double T
double Y
FOR J=1 TO R
FOR I=J TO R
IF A(I,J)<>0 then break
NEXT
IF I=R+1
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
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
double Resistance
Resistance = A(ANode,101)-A(BNode,101)
PRINT
PRINT "Resistance between Nodes" str$(ANode) + " and" + str$(BNode) + " is" + str$(abs(Resistance)) + " Ohms"
handleevents