restart:# ITERATIVE REFINEMENT ALGORITHM 7.4## To approximate the solution to the linear system Ax=b when A is# suspected to be ill-conditioned:## 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 maximum number# of iterations N.## OUTPUT: The approximation XX(1),...,XX(n) or a message that the# number of iterations was exceeded. CHIP := proc(RND,R,X) local x,e:if RND = 1 thenDigits := R:x := evalf(X):RETURN(x):elseif X = 0 then x := 0:elsee := trunc(evalf(log10(abs(X)))):if abs(X) > 1 thene := e+1:fi:x := evalf(trunc(X*10^(R-e))*10^(e-R)):fi:RETURN(x):fi:end:print(`This is the Iterative Refinement Method.\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(`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)\134n`):print(`Place as many entries as desired on each line, but separate\134n`):print(`entries with `):print(`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):OK := FALSE:if AA = 1 thenprint(`Input the file name in the form - drive:\134\134name.ext\134n`):print(`for example: A:\134\134DATA.DTA\134n`):NAME := scanf(`%s`)[1]:INP := fopen(NAME,READ,TEXT):OK := FALSE:while OK = FALSE doprint(`Input the number of equations - an integer.\134n`):N := scanf(`%d`)[1]:print(`N = `):print(N):if N > 0 thenfor I1 from 1 to N dofor J from 1 to N+1 doA[I1-1,J-1] := fscanf(INP, `%f`)[1]:od:od:OK := TRUE:fclose(INP):elseprint(`The number must be a positive integer\134n`):fi:od:elseprint(`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 thenfor I1 from 1 to N dofor J from 1 to N+1 doprint(`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:OK := TRUE:else print(`The number must be a positive integer.\134n`):fi:od:fi:if OK = TRUE thenOK := FALSE:while OK = FALSE doprint(`Input maximum number of iterations.\134n`):# NN is used for the maximum number of iterationsNN := scanf(`%d`)[1]:print(`Maximum number of iterations = `):print(NN):if NN > 0 thenOK := TRUE:elseprint(`Number must be a positive integer.\134n`):fi:od:OK := FALSE:print(`Choice of rounding or chopping:\134n`):print(`1. Rounding\134n`):print(`2. Chopping\134n`):print(`Enter 1 or 2.\134n`):RND := scanf(`%d`)[1]:print(`Your input is `):print(RND):while OK = FALSE doprint(`Input number of digits D <= 8 of rounding\134n`):DD := scanf(`%d`)[1]:print(`Number of digits = `):print(DD):if DD > 0 thenOK := TRUE:elseprint(`D must be a positive integer.\134n`):fi:od:OK := FALSE:while OK = FALSE doprint(`Input tolerance, which is usually 10^(-D).\134n`):TOL := scanf(`%f`)[1]:print(`Tolerance = `):print(TOL):if TOL > 0 thenOK := TRUE:elseprint(`Tolerance must be a positive.\134n`):fi:od:if OK = TRUE thenprint(`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 input 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, `ITERATIVE REFINEMENT METHOD\134n\134n`):M := N+1:fprintf(OUP, `Original system\134n`):for I1 from 1 to N dofor J from 1 to M dofprintf(OUP,` %.10e`,A[I1-1,J-1]):od:fprintf(OUP,`\134n`):od:if RND = 1 thenfprintf(OUP,`Rounding to %d Digits.\134n`,DD):else fprintf(OUP,`Chopping to %d Digits.\134n`,DD):fi:fprintf(OUP,`\134n Modified System \134n`):for I1 from 1 to N doNROW[I1-1] := I1:for J from 1 to M doA[I1-1,J-1] := CHIP(RND,DD,A[I1-1,J-1]):B[I1-1,J-1] := A[I1-1,J-1]:fprintf(OUP,` %.10e`, A[I1-1,J-1]):od:fprintf(OUP, `\134n`):od:# NROW and B have been initialized, Gauss elimination will begin.# Step 0I1 := 1:while I1 <= N-1 and OK = TRUE doKK := I1:while abs(A[KK-1,I1-1]) < 1.0e-20 and KK <= N doKK := KK+1:od:if KK > N thenOK := false:fprintf(OUP, `System does not have a unique solution.\134n`):else if KK <> I1 then# Row interchange is necessaryIS := NROW[I1-1]:NROW[I1-1] := NROW[KK-1]:NROW[KK-1] := IS:for J from 1 to M doC := A[I1-1,J-1]:A[I1-1,J-1] := A[KK-1,J-1]:A[KK-1,J-1] := C:od:fi:for J from I1+1 to N doA[J-1,I1-1] := CHIP(RND,DD,A[J-1,I1-1]/A[I1-1,I1-1]):for L from I1+1 to M doA[J-1,L-1] := CHIP(RND,DD,A[J-1,L-1]-CHIP(RND,DD,A[J-1,I1-1]*A[I1-1,L-1])):od:od:fi:I1 := I1+1:od:if abs(A[N-1,N-1]) < 1.0e-20 and OK = TRUE thenOK := FALSE:fprintf(OUP, `System has singular matrix\134n`):fi:if OK = TRUE thenfprintf(OUP, `Reduced system\134n`):for I1 from 1 to N dofor J from 1 to M dofprintf(OUP, ` %.10e`, A[I1-1,J-1]):od:fprintf(OUP, `\134n`):od:X[N-1] := CHIP(RND,DD,A[N-1,M-1]/A[N-1,N-1]):for I1 from 1 to N-1 doJ := N-I1:S := 0.0:for L from J+1 to N doS := CHIP(RND,DD,S-CHIP(RND,DD,A[J-1,L-1]*X[L-1])):od:S := CHIP(RND,DD,A[J-1,M-1]+S):X[J-1] := CHIP(RND,DD,S/A[J-1,J-1]):od:fi:fprintf(OUP, `Initial solution\134n`):for I1 from 1 to N dofprintf(OUP,` %.10e`, X[I1-1]):od:fprintf(OUP, `\134n`):# Refinement begins# Step 1if OK = TRUE thenK := 1:for I1 from 1 to N doXX[I1-1] := X[I1-1]:od:fi:# Step 2while OK = TRUE and K <= NN do# LL is set to 1 if the desired accuracy in any component is not# achieved.LL := 0:# Step 3for I1 from 1 to N doR[I1-1] := 0:for J from 1 to N doR[I1-1] := CHIP(RND,2*DD,R[I1-1]-CHIP(RND,2*DD,B[I1-1,J-1]*XX[J-1])):od:R[I1-1] := CHIP(RND,2*DD,B[I1-1,M-1]+R[I1-1]):od:fprintf(OUP, `Residual number %d\134n`, K):for I1 from 1 to N doR[I1-1] := CHIP(RND,DD,R[I1-1]):fprintf(OUP, `%18.10e `, R[I1-1]):od:fprintf(OUP, `\134n`):# Step 4# Solve the linear system in the same order as in Step 0.for I1 from 1 to N-1 doI2 := NROW[I1-1]:for J from I1+1 to N doJ1 := NROW[J-1]:R[J1-1] := CHIP(RND,DD,R[J1-1]-CHIP(RND,DD,A[J-1,I1-1]*R[I2-1])):od:od:X[N-1] := CHIP(RND,DD,R[NROW[N-1]-1]/A[N-1,N-1]):for I1 from 1 to N-1 doJ := N-I1:S := 0:for L from J+1 to N doS := CHIP(RND,DD,S-CHIP(RND,DD,A[J-1,L-1]*X[L-1])):od:S := CHIP(RND,DD,S+R[NROW[J-1]-1]):X[J-1] := CHIP(RND,DD,S/A[J-1,J-1]):od:fprintf(OUP, `Vector Y\134n`):for I1 from 1 to N dofprintf(OUP,`%18.10e `, X[I1-1]):od:fprintf(OUP, `\134n`):# Steps 5 and 6XXMAX := 0:YMAX := 0:ERR1 := 0:for I1 from 1 to N do# If not sufficiently accurate, set LL to 1.if abs(X[I1-1]) > TOL thenLL := 1:fi:if K = 1 thenif abs(X[I1-1]) > YMAX thenYMAX := abs(X[I1-1]):fi:if abs(XX[I1-1]) > XXMAX thenXXMAX := abs(XX[I1-1]):fi:fi:TEMP := XX[I1-1]:XX[I1-1] := CHIP(RND,DD,XX[I1-1]+X[I1-1]):TEMP := abs(TEMP-XX[I1-1]):if TEMP > ERR1 thenERR1 := TEMP:fi:od:if ERR1 <= TOL thenLL := 2:fi:if K = 1 thenCOND := YMAX/XXMAX*10^DD:fi:fprintf(OUP, `New approximation\134n`):for I1 from 1 to N dofprintf(OUP, `%18.10e `, XX[I1-1]):od:fprintf(OUP, `\134n`):# Step 7if LL = 0 thenfprintf(OUP, `The above vector is the solution.\134n`):OK := FALSE:elseif LL = 2 thenfprintf(OUP,`The above vector is the best possible\134n`):fprintf(OUP,`with TOL := %18.10e \134n`,TOL):OK := FALSE:elseK := K+1:fifi:# Step 8 is not used in this implementation.od:if K > NN thenprint( `Maximum Number of Iterations Exceeded.`):fi:fprintf(OUP, `Condition number is %.10e\134n`, COND):if OUP <> default thenfclose(OUP):print(`Output file `,NAME,` created successfully`):fi:fi:fi:JSFH