'********************************************************* '* SOLVE A LINEAR SYSTEM BY TRIANGULARIZATION METHOD * '* ----------------------------------------------------- * '* SAMPLE RUN: * '* * '* Linear system AX = B * '* * '* Matrix A: * '* 2 1 -2 * '* 3 -1 1 * '* 7 5 -3 * '* * '* Right side: * '* B( 1 ) = 1 * '* B( 2 ) = 0 * '* B( 3 ) = 0 * '* * '* Solution of AX=B: * '* X( 1 ) = .0625 * '* X( 2 ) = -.5000 * '* X( 3 ) = -.6875 * '* * '* ----------------------------------------------------- * '* Reference: "Methodes de calcul numerique - Tome 1 By * '* Claude Nowakowski, PS1 1981" [BIBLI 07]. * '* * '* Basic Release By J-P Moreau, Paris. * '* (www.jpmoreau.fr) * '********************************************************* 'Program Tlinear DEFINT I-N DEFDBL A-H, O-Z READ N DIM A(N, N), X(N), B(N) FOR I = 1 TO N FOR J = 1 TO N READ A(I, J) NEXT J NEXT I FOR I = 1 TO N READ B(I) NEXT I CLS PRINT " Linear system AX = B" PRINT PRINT " Matrix A:" FOR I = 1 TO N FOR J = 1 TO N PRINT " "; A(I, J); NEXT J PRINT NEXT I PRINT PRINT " Right Side:" FOR I = 1 TO N PRINT " B("; I; ") = "; B(I) NEXT I 'Transform A into triangular matrix FOR K = 1 TO N - 1 FOR I = K + 1 TO N B(I) = B(I) - A(I, K) / A(K, K) * B(K) FOR J = K + 1 TO N A(I, J) = A(I, J) - A(I, K) / A(K, K) * A(K, J) NEXT J NEXT I NEXT K 'Solve triangular system X(N) = B(N) / A(N, N) FOR I = N - 1 TO 1 STEP -1 S = 0# FOR K = I + 1 TO N S = S + A(I, K) * X(K) NEXT K X(I) = (B(I) - S) / A(I, I) NEXT I 'Print results: PRINT PRINT " Solution of AX=B" FOR I = 1 TO N PRINT " X("; I; ") = "; X(I) NEXT I END 'of main program DATA 3 DATA 2,1,-2,3,-1,1,7,5,-3 DATA 1,0,0 'end of file Tlinear.bas