> restart; > # JACOBI ITERATIVE ALGORITHM 7.1 > # > # 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. > alg071 := proc() local AA, OK, NAME, INP, N, I, J, A, X1, TOL, NN, K, ERR, S, X2, FLAG, OUP; > printf(`This is the Jacobi Method for Linear Systems.\n`); > printf(`The array will be input from a text file in the order\n`); > printf(`A(1,1), A(1,2), ..., A(1,n+1), A(2,1), A(2,2), ..., > A(2,n+1),..., A(n,1), A(n,2), ..., A(n,n+1)\n`); > printf(`Place as many entries as desired on each line, but separate\n`); > printf(`entries with `); > printf(`at least one blank.\n\n\n`); > printf(`The initial approximation should follow in the same format\n`); > printf(`Has the input file been created? - enter Y or N.\n`); > AA := scanf(`%c`)[1]; > OK := FALSE; > if AA = "Y" or AA = "y" then > printf(`Input the file name in the form - drive:\\name.ext\n`); > printf(`for example: A:\\DATA.DTA\n`); > NAME := scanf(`%s`)[1]; > INP := fopen(NAME,READ,TEXT); > OK := FALSE; > while OK = FALSE do > printf(`Input the number of equations - an integer.\n`); > N := scanf(`%d`)[1]; > if N > 0 then > for I from 1 to N do > for J from 1 to N+1 do > A[I-1,J-1] := fscanf(INP, `%f`)[1]; > od; > od; > for I from 1 to N do > X1[I-1] := fscanf(INP, `%f`)[1]; > od; > OK := TRUE; > fclose(INP); > else > printf(`The number must be a positive integer\n`); > fi; > od; > OK := FALSE; > while OK = FALSE do > printf(`Input the tolerance.\n`); > TOL := scanf(`%f`)[1]; > if TOL > 0 then > OK := TRUE; > else > printf(`Tolerance must be a positive.\n`); > fi; > od; > OK := FALSE; > while OK = FALSE do > printf(`Input maximum number of iterations.\n`); > NN := scanf(`%d`)[1]; > if NN > 0 then > OK := TRUE; > else > printf(`Number must be a positive integer.\n`); > fi; > od; > else > printf(`The program will end so the input file can be created.\n`); > fi; > if OK = TRUE then > # 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 I from 1 to N do > S := 0; > for J from 1 to N do > S := S-A[I-1,J-1]*X1[J-1]; > od; > S := (S+A[I-1,N])/A[I-1,I-1]; > if abs(S) > ERR then > ERR := abs(S); > fi; > # X2 is used in place of X > X2[I-1] := X1[I-1]+S; > od; > # Step 4 > if ERR <= TOL then > # Process is completed successfully. > OK := TRUE; > else > # Step 5 > K := K+1; > # Step 6 > for I from 1 to N do > X1[I-1] := X2[I-1]; > od; > fi; > od; > # Step 7 > # Process is completed unsuccessfully. > if OK = FALSE then > printf(`Maximum Number of Iterations Exceeded.\n`); > else > printf(`Choice of output method:\n`); > printf(`1. Output to screen\n`); > printf(`2. Output to text file\n`); > printf(`Please enter 1 or 2.\n`); > FLAG := scanf(`%d`)[1]; > if FLAG = 2 then > printf(`Input the file name in the form - drive:\\name.ext\n`); > printf(`for example: A:\\OUTPUT.DTA\n`); > NAME := scanf(`%s`)[1]; > OUP := fopen(NAME,WRITE,TEXT); > else > OUP := default; > fi; > fprintf(OUP, `JACOBI ITERATIVE METHOD FOR LINEAR SYSTEMS\n\n`); > fprintf(OUP, `The solution vector is :\n`); > for I from 1 to N do > fprintf(OUP, ` %11.8f`, X2[I-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): > printf(`Output file %s created successfully`,NAME); > fi; > fi; > fi; > RETURN(0); > end; > alg071();