> restart: > # GAUSS-SEIDEL ITERATIVE TECHNIQUE ALGORITHM 7.2 > # > # To solve Ax = b given an initial approximation x(0). > # > # INPUT: the number of equations and unknowns n: the entries > # A(I,J), 1<=I, J<=n, of the matrix A: the entries > # B(I), 1<=I<=n, of the inhomogeneous term b: the > # entries XO(I), 1<=I<=n, of x(0): tolerance TOL: > # maximum number of iterations N. > # > # OUTPUT: the approximate solution X(1),...,X(n) or a message > # that the number of iterations was exceeded. > print(`This is the Gauss-Seidel Method for Linear Systems.`); > 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 then > print(`The array will be input from a text file in the order`): > print(`A(1,1), A(1,2), ..., A(1,N+1), A(2,1), A(2,2), ..., > A(2,N+1)`): > print(`..., A(N,1), A(N,2), ..., A(N,N+1)\n`): > print(`Place as many entries as desired on each line, but separate `): > print(`entries with`): > print(`at least one blank.`): > print(`The initial approximation should follow in same format`): > 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): OK:=FALSE: > if AA = 1 then > print(`Input the file name in the form - drive:\\name.ext`): > print(`for example: A:\\DATA.DTA`): > NAME := scanf(`%s`)[1]: print(`The file name is`): print(NAME): > INP := fopen(NAME,READ,TEXT): > while OK = FALSE do > print(`Input the number of equations - an integer.`): > N := scanf(`%d`)[1]: print(`N is`): print(N): > if N > 0 then > for I1 from 1 to N do > for J from 1 to N+1 do > A[I1-1,J-1] := fscanf(INP, `%f`)[1]: > od: > od: > for I1 from 1 to N do > X1[I1-1] := fscanf(INP, `%f`)[1]: > od: > OK := TRUE: > fclose(INP): > else print(`The number must be a positive integer.\n`): > fi: > od: > else > print(`The program will end so the input file can be created.\n`): > fi: > else > OK := FALSE: > while OK = FALSE do > print(`Input the number of equations - an integer.`): > N := scanf(`%d`)[1]: print(`N= `): print(N): > if N > 0 then > for I1 from 1 to N do > for J from 1 to N+1 do > print(`input entry in position `,I1,J): > A[I1-1,J-1] := scanf(`%f`)[1]:print(`Data is `):print(A[I1-1,J-1]): > od: > od: > for I1 from 1 to N do > print(`input initial approximation entry in position `,I1): > X1[I1-1] := scanf(`%f`)[1]:print(`Data is `):print(X1[I1-1]): > od: > OK := TRUE: > else print(`The number must be a positive integer.\n`): > fi: > od: > fi: > if OK = TRUE then > OUP := default: > fprintf(OUP, `The original system - output by rows:\n`): > for I1 from 1 to N do > for J from 1 to N+1 do > fprintf(OUP, ` %11.8f`, A[I1-1,J-1]): > od: > fprintf(OUP, `\n`): > od: > fprintf(OUP, `The initial approximation:\n`): > for I1 from 1 to N do > fprintf(OUP, ` %11.8f`, X1[I1-1]): > od: > fprintf(OUP, `\n`): > fi: > if OK = TRUE then > OK := FALSE: > while OK = FALSE do > print(`Input the tolerance.\n`): > TOL := scanf(`%f`)[1]:print(`Tolerance = `):print(TOL): > if TOL > 0 then > OK := TRUE: > else > print(`Tolerance must be a positive number.\n`): > fi: > od: > fi: > if OK = TRUE then > OK := FALSE: > while OK = FALSE do > print(`Input maximum number of iterations.\n`): > NN := scanf(`%d`)[1]:print(`Maximum number of iterations = `):print(NN): > if NN > 0 then > OK := TRUE: > else > print(`Number must be a positive integer.\n`): > fi: > od: > fi: > if OK = TRUE then > fprintf(OUP,`Iter# Vector\n`): > # Step 1 > K := 1: > OK := FALSE: > # Step 2 > while OK = FALSE and K <= NN do > # ERR is used to test accuracy - it measures the infinity norm > ERR := 0: > # Step 3 > for I1 from 1 to N do > S := 0: > for J from 1 to N do > S := S-A[I1-1,J-1]*X1[J-1]: > od: > S := (S+A[I1-1,N])/A[I1-1,I1-1]: > if abs(S) > ERR then > ERR := abs(S): > fi: > X1[I1-1] := X1[I1-1] + S; > od: > # Step 4 > if ERR <= TOL then > # Process is completed successfully. > OK := TRUE: > else > # Step 5 > fprintf(OUP, ` %d `, K): > K := K+1: > # Step 6 is not used since only one vector is needed. > for I1 from 1 to N do > fprintf(OUP, ` %11.8f`, X1[I1-1]): > od: > fprintf(OUP, `\n`): > fi: > od: > # Step 7 > # Process is completed unsuccessfully. > if OK = FALSE then > print(`Maximum Number of Iterations Exceeded.\n`): > else > print(`Choice of output method:\n`): > print(`1. Output to screen\n`): > print(`2. Output to text file\n`): > print(`Please enter 1 or 2.\n`): > FLAG := scanf(`%d`)[1]:print(`Your input is `):print(FLAG): > if FLAG = 2 then > print(`Input the file name in the form - drive:\\name.ext\n`): > print(`for example: A:\\OUTPUT.DTA\n`): > NAME := scanf(`%s`)[1]:print(`Output file is `):print(NAME): > OUP := fopen(NAME,WRITE,TEXT): > else > OUP := default: > fi: > fprintf(OUP, `Gauss-Seidel Method FOR LINEAR SYSTEMS\n\n`): > fprintf(OUP, `The solution vector is :\n`): > for I1 from 1 to N do > fprintf(OUP, ` %11.8f`, X1[I1-1]): > od: > fprintf(OUP, `\nusing %d iterations\n`, K): > fprintf(OUP, `with Tolerance %.10e in infinity-norm\n`, TOL): > if OUP <> default then > fclose(OUP): > print(`Output file `,NAME,` created successfully`): > fi: > fi: > fi: