restart:# CROUT FACTORIZATION FOR TRIDIAGONAL LINEAR SYSTEMS ALGORITHM 6.7## To solve the n x n linear system## E1: A[1,1] X[1] + A[1,2] X[2] = A[1,n+1]# E2: A[2,1] X[1] + A[2,2] X[2] + A[2,3] X[3] = A[2,n+1]# :# .# E(n): A[n,n-1] X[n-1] + A[n,n] X[n] = A[n,n+1]## INPUT: the dimension n: the entries of A.## OUTPUT: the solution X(1), ..., X(N).print(`This is Crout Method for tridiagonal linear systems.\134n`):print(`Choice of input method`):print(`1. input from keyboard - not recommended for large systems`):print(`2. input from a text file`):print(`Please enter 1 or 2.`):FLAG := scanf(`%d`)[1]: print(`Your input is`): print(FLAG):if FLAG = 2 thenprint(`The array will be input from a text file in the order:\134n`):print(`diagonal entries, lower sub-diagonal entries,\134n`):print(`upper sub-diagonal entries, inhomogeneous term.\134n\134n`):print(`Place as many entries as desired on each line, but separate `):print(`entries \134n`):print(`with at least one blank.\134n\134n\134n`):print(`Has the input file been created? - enter 1 for yes or 2 for no.`):AA := scanf(`%d`)[1]: print(`Your response is`): print(AA):if AA = 1 thenprint(`Input the file name in the form - drive:\134\134name.ext`):print(`for example: A:\134\134DATA.DTA`):NAME := scanf(`%s`)[1]: print(`The file name is`): print(NAME):INP := fopen(NAME,READ,TEXT):OK := FALSE:while OK = FALSE doprint(`Input the number of equations - an integer.`):N := scanf(`%d`)[1]: print(`N is`): print(N):if N > 0 then# A(I,I) is stored in A(I), 1 <= I <= n for I1 from 1 to N doA[I1-1] := fscanf(INP, `%f`)[1]:od:# the lower sub-diagonal A(I,I-1) is stored#in B(I), 2 <= I <= n for I1 from 2 to N doB[I1-1] := fscanf(INP, `%f`)[1]:od:# the upper sub-diagonal A(I,I+1) is stored#in C(I), 1 <= I <= n-1 NN := N-1:for I1 from 1 to NN doC[I1-1] := fscanf(INP, `%f`)[1]:od:# A(I,N+1) is stored in BB(I), 1 <= I <= n for I1 from 1 to N doBB[I1-1] := fscanf(INP, `%f`)[1]:od:OK := TRUE:fclose(INP):else print(`The number must be a positive integer.\134n`):fi:od:else print(`The program will end so the input file can be created.\134n`):fi:elseOK := FALSE:while OK = FALSE doprint(`Input the number of equations - an integer.`):N := scanf(`%d`)[1]: print(`N= `): print(N):if N > 0 then# A(I,I) is stored in A(I), 1 <= I <= n for I1 from 1 to N doprint(`Input the diagonal entry in row = `):print(I1):A[I1-1] := scanf( `%f`)[1]:print(`Entry = `,A[I1-1]):od:# the lower sub-diagonal A(I,I-1) is stored#in B(I), 2 <= I <= n for I1 from 2 to N doprint(`Input the lower sub-diagonal diagonal entry in row = `):print(I1):print(` and column = `):print(I1-1):B[I1-1] := scanf(`%f`)[1]:print(`Entry = `,B[I1-1]):od:# the upper sub-diagonal A(I,I+1) is stored#in C(I), 1 <= I <= n-1 NN := N-1:for I1 from 1 to NN doprint(`Input the upper sub-diagonal diagonal entry in row = `):print(I1):print(` and column = `):print(I1+1):C[I1-1] := scanf(`%f`)[1]:print(`Entry = `,C[I1-1]):od:# A(I,N+1) is stored in BB(I), 1 <= I <= n for I1 from 1 to N doprint(`Input the entry on the right-hand side of equation = `):print(I1):BB[I1-1] := scanf(`%f`)[1]:print(`Entry = `,BB[I1-1]):od:OK:=TRUE:else print(`The number must be a positive integer.\134n`):fi:od:fi:if OK = TRUE thenOUP := default:fprintf(OUP, `The original system \134n`):fprintf(OUP,`Diagonal Entries`):for I1 from 1 to N dofprintf(OUP, ` %11.8f`, A[I1-1]):od:fprintf(OUP, `\134n`):fprintf(OUP,`Lower Sub-Diagonal Entries`):for I1 from 2 to N dofprintf(OUP, ` %11.8f`, B[I1-1]):od:fprintf(OUP, `\134n`):fprintf(OUP,`Upper Sub-Diagonal Entries`):for I1 from 1 to NN dofprintf(OUP, ` %11.8f`, C[I1-1]):od:fprintf(OUP, `\134n`):fprintf(OUP,`Inhomogeneous Entries`):for I1 from 1 to N dofprintf(OUP, ` %11.8f`, BB[I1-1]):od:fprintf(OUP, `\134n`):if OK = TRUE then# Steps 1 - 3 set up and solve Lz = 0# Step 1# The entries of U overwrite C and the entries of L overwrite AC[0] := C[0]/A[0]:Z[0] := BB[0]/A[0]:# Step 2for I1 from 2 to NN doA[I1-1] := A[I1-1]-B[I1-1]*C[I1-2]:C[I1-1] := C[I1-1]/A[I1-1]:Z[I1-1] := (BB[I1-1]-B[I1-1]*Z[I1-2])/A[I1-1]:od:# Step 3A[N-1] := A[N-1]-B[N-1]*C[N-2]:Z[N-1] := (BB[N-1]-B[N-1]*Z[N-2])/A[N-1]:# Step 4# Steps 4 and 5 solve Ux = zX[N-1] := Z[N-1]:# Step 5for I2 from 1 to NN doI1 := NN-I2+1:X[I1-1] := Z[I1-1]-C[I1-1]*X[I1]:od:# Step 6print(`Choice of output method:\134n`):print(`1. Output to screen\134n`):print(`2. Output to text file\134n`):print(`Please enter 1 or 2.\134n`):FLAG := scanf(`%d`)[1]:print(`Your entry is `):print(FLAG):if FLAG = 2 thenprint(`Input the file name in the form - drive:\134\134name.ext\134n`):print(`for example: A:\134\134OUTPUT.DTA\134n`):NAME := scanf(`%s`)[1]:print(`Output file is `):print(NAME):OUP := fopen(NAME,WRITE,TEXT):elseOUP := default:fi:fprintf(OUP, `CROUT METHOD FOR TRIDIAGONAL LINEAR SYSTEMS\134n\134n`):fprintf(OUP, `The solution is\134n`):for I1 from 1 to N dofprintf(OUP, ` %12.8f`, X[I1-1]):od:fprintf(OUP, `\134n`):if OUP <> default thenfclose(OUP):print(`Output file `,NAME,` created successfully`):fi:fi:fi:JSFH