This Pascal program is usefull in solving large tridiagonal linear systems. An example is the systems emerging from numerically integrating parabolic PDE’s by Finite Differences method.

It was ported to Turbo Pascal from a Fortran 77 program.
Its usage is very simple. All you have to do is to enter dimension of the system and the data, ie the three diagonals and the constant vector. Have fun…

program trid;
USES CRT;
var
 
 B,D,X,Y,L : array [1..100] of real;
 C,G : ARRAY [1..99] OF REAL;
 A,K : ARRAY [2..100] OF REAL;
 N,J : INTEGER;
 
 
 
PROCEDURE  TRIDDE; {(VAR A,B,C,K,L,G)}
BEGIN
 L[1]:=B[1];
 G[1]:=C[1];
 FOR J:=1 TO N-1 DO
 BEGIN
  G[J]:=C[J];
  K[J+1]:=A[J+1]/L[J];
  L[J+1]:=B[J+1]-K[J+1]*G[J];
  END;
END;
 
PROCEDURE LOWERT; {(N,K,D,Y) }
BEGIN
  Y[1]:=D[1];
  FOR J:=2 TO N DO
   Y[J]:=D[J]-K[J]*Y[J-1];
END;
 
PROCEDURE UPPERT;{ (N,L,G,Y,X) }
BEGIN
  X[N]:=Y[N]/L[N];
  FOR J:=N-1 DOWNTO 1 DO
   X[J]:=(Y[J]-G[J]*X[J+1])/L[J];
END;
 
 
 
 
BEGIN
 CLRSCR;
 WRITE ('ENTER DIMENSION OF SYSTEM: ');
 READ ( N);
 WRITELN ( 'ENTER CONSTANT VECTOR: ');
 FOR J:=1 TO N DO
  READLN (D[J]);
 
 WRITELN ('ENTER THE DIAGONAL OF THE SYSTEM A :');
 FOR J:=2 TO N DO
  READLN  (A[J]);
 
 WRITELN ( 'ENTER THE DIAGONAL OF THE SYSTEM B :');
 FOR J:=1 TO N DO
  READLN  (B[J]);
 
 WRITELN ('ENTER THE DIAGONAL OF THE SYSTEM C :');
 FOR J:=1 TO N-1 DO
  READLN  (C[J]);
 
 TRIDDE; {(N,A,B,C,K,L,G);}
 LOWERT; {(N,K,D,Y); }
 UPPERT; {(N,L,G,Y,X);  }
 
 CLRSCR;
 WRITELN ( 'THE DIMENSION OF THE TRID SYSTEM IS: ',N);
 WRITELN ('THE CONST. VECTOR IS AS FOLLOWS :' );
 FOR J:=1  TO N DO
  WRITELN (D[J]);
 
 WRITELN ('PRESS ENTER'); READLN;
 CLRSCR;
 WRITE ('THE DIAGONALS OF THE TRID. MATRIX ARE :');
 FOR J:=1 TO N DO
  WRITELN (A[J+1],B[J],C[J]);
 
 WRITELN ('PRESS '); READLN;
 
 WRITELN ('THE DIAGONAL OF THE LOWER TRID.MATRIX IS :' );
 
 FOR J:=2 TO N DO
  WRITELN (K[J]);
 
 WRITELN ( 'PRESS ' ); READLN;
 
 WRITELN ( 'DIAGONALS OF UPPER TR.MATRIX AND CONST.VECTOR -Y-: ');
 
 FOR J:=1 TO N DO
  WRITELN (L[J],G[J],Y[J]);
 
 WRITELN ( 'PRESS ' ); READLN;
 
 WRITELN  ( 'THE SOLUTION VECTOR OF THE TRID. SYSTEM IS: ');
 FOR J:=1 TO N DO
  WRITELN (X[J]);
 
 READLN;READLN;
END.
Share it!