Wednesday 17 October 2012



CLS
'PROGRAM TO PRODUCE THE COFACTOR, ADJOINT AND INVERSE OF ANY 3 BY 3 MATRIX
'THIS VERSION USES THE READ/DATA STATEMENT

'INPUT THE DIMENSIONS OF THE MATRIX IN THE DATA STATEMENT BELOW
DATA 3,3
READ M, N

DIM A(M, N)
DIM C(M, N)
DIM INV(M, N)
'INPUT THE ELEMENTS OF THE ORIGINAL MATRIX
'IN THE ORDER OF TOP TO BOTTOM AND LEFT TO RIGHT
DATA 2,1,2,-4,3,1,2,3,5,1,2,3,4,5,6,7,8     'TESTING  ELEMENTS
 FOR I = 1 TO M
   FOR J = 1 TO N
  READ A(I, J)
  NEXT
 NEXT
  PRINT " THE ORIGINAL MATRIX A"
 FOR I = 1 TO M
   FOR J = 1 TO N
  PRINT USING "##"; A(I, J);
  PRINT " ";
  NEXT
  PRINT
 NEXT
  PRINT
 'THE COFACTOR MATRIX
 C(1, 1) = (A(2, 2) * A(3, 3) - A(2, 3) * A(3, 2))
 C(1, 2) = -(A(2, 1) * A(3, 3) - A(2, 3) * A(3, 1))
 C(1, 3) = (A(2, 1) * A(3, 2) - A(2, 2) * A(3, 1))
 C(2, 1) = -(A(1, 2) * A(3, 3) - A(1, 3) * A(3, 2))
 C(2, 2) = (A(1, 1) * A(3, 3) - A(1, 3) * A(3, 1))
 C(2, 3) = -(A(1, 1) * A(3, 2) - A(1, 2) * A(3, 1))
 C(3, 1) = (A(1, 2) * A(2, 3) - A(1, 3) * A(2, 2))
 C(3, 2) = -(A(1, 1) * A(2, 3) - A(1, 3) * A(2, 1))
 C(3, 3) = (A(1, 1) * A(2, 2) - A(1, 2) * A(2, 1))


  PRINT "THE COFACTOR MATRIX"
  FOR I = 1 TO M
   FOR J = 1 TO N
   PRINT USING "###"; C(I, J);
   PRINT " ";
   NEXT
   PRINT
  NEXT
   PRINT
  PRINT " THE TRANSPOSE OF THE COFACTOR (ADJOINT OF THE MATRIX, A)"
  FOR I = 1 TO N
   FOR J = 1 TO M
   PRINT USING "###"; C(J, I);
   PRINT " ";
   NEXT
   PRINT
  NEXT
  PRINT
PRINT "THE DETERMINANT OF A IS ";
D = A(1, 1) * A(2, 2) * A(3, 3) + A(1, 2) * A(2, 3) * A(3, 1) + A(1, 3) * A(2, 1) * A(3, 2) - A(3, 1) * A(2, 2) * A(1, 3) - A(3, 2) * A(2, 3) * A(1, 1) - A(3, 3) * A(2, 1) * A(1, 2)
PRINT D
 IF D <> 0 THEN
 GOTO 50
 ELSE GOTO 100
 END IF

50  PRINT "THE INVERSE OF A IS"
  FOR I = 1 TO N
   FOR J = 1 TO M
   INV(I, J) = C(J, I) * (1 / D)
   PRINT USING "###"; INV(I, J);
   PRINT " ";
   NEXT
   PRINT
  NEXT


100 END

SUB INVERSE

END SUB