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. print(`This is the Jacobi Method for 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 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)\134n`): 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:\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): 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 fi: od: else print(`The program will end so the input file can be created.\134n`): 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 fi: od: fi: if OK = TRUE then OUP := default: fprintf(OUP, `The original system - output by rows:\134n`): 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, `\134n`): od: fprintf(OUP, `The initial approximation:\134n`): for I1 from 1 to N do fprintf(OUP, ` %11.8f`, X1[I1-1]): od: fprintf(OUP, `\134n`): fi: if OK = TRUE then OK := FALSE: while OK = FALSE do print(`Input the tolerance.\134n`): TOL := scanf(`%f`)[1]:print(`Tolerance = `):print(TOL): if TOL > 0 then OK := TRUE: else print(`Tolerance must be a positive number.\134n`): fi: od: fi: if OK = TRUE then OK := FALSE: while OK = FALSE do print(`Input maximum number of iterations.\134n`): 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.\134n`): fi: od: fi: if OK = TRUE then fprintf(OUP,`Iter# Vector\134n`): # 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: # X2 is used in place of X X2[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 for I1 from 1 to N do X1[I1-1] := X2[I1-1]: fprintf(OUP, ` %11.8f`, X2[I1-1]): od: fprintf(OUP, `\134n`): fi: od: # Step 7 # Process is completed unsuccessfully. if OK = FALSE then print(`Maximum Number of Iterations Exceeded.\134n`): else print(`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 then print(`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): else OUP := default: fi: fprintf(OUP, `JACOBI ITERATIVE METHOD FOR LINEAR SYSTEMS\134n\134n`): fprintf(OUP, `The solution vector is :\134n`): for I1 from 1 to N do fprintf(OUP, ` %11.8f`, X2[I1-1]): od: fprintf(OUP, `\134nusing %d iterations\134n`, K): fprintf(OUP, `with Tolerance %.10e in infinity-norm\134n`, TOL): if OUP <> default then fclose(OUP): print(`Output file `,NAME,` created successfully`): fi: fi: fi: SU9UaGlzfmlzfnRoZX5KYWNvYml+TWV0aG9kfmZvcn5MaW5lYXJ+U3lzdGVtcy58K0c2Ig== STdDaG9pY2V+b2Z+aW5wdXR+bWV0aG9kRzYi SWVuMS5+aW5wdXR+ZnJvbX5rZXlib2FyZH4tfm5vdH5yZWNvbW1lbmRlZH5mb3J+bGFyZ2V+c3lzdGVtc0c2Ig== SToyLn5pbnB1dH5mcm9tfmF+dGV4dH5maWxlRzYi STVQbGVhc2V+ZW50ZXJ+MX5vcn4yLkc2Ig== SS5Zb3VyfmlucHV0fmlzRzYi IiIj SVZUaGV+YXJyYXl+d2lsbH5iZX5pbnB1dH5mcm9tfmF+dGV4dH5maWxlfmlufnRoZX5vcmRlckc2Ig== SVtvQSgxLDEpLH5BKDEsMiksfi4uLix+QSgxLE4rMSksfkEoMiwxKSx+QSgyLDIpLH4uLi4sfnwrfn5+QSgyLE4rMSlHNiI= SUQuLi4sfkEoTiwxKSx+QShOLDIpLH4uLi4sfkEoTixOKzEpfCtHNiI= SWduUGxhY2V+YXN+bWFueX5lbnRyaWVzfmFzfmRlc2lyZWR+b25+ZWFjaH5saW5lLH5idXR+c2VwYXJhdGV+RzYi SS1lbnRyaWVzfndpdGhHNiI= STRhdH5sZWFzdH5vbmV+YmxhbmsuRzYi SVdUaGV+aW5pdGlhbH5hcHByb3hpbWF0aW9ufnNob3VsZH5mb2xsb3d+aW5+c2FtZX5mb3JtYXRHNiI= SWpuSGFzfnRoZX5pbnB1dH5maWxlfmJlZW5+Y3JlYXRlZD9+LX5lbnRlcn4xfmZvcn55ZXN+b3J+Mn5mb3J+bm8uRzYi STFZb3VyfnJlc3BvbnNlfmlzRzYi IiIi SVJJbnB1dH50aGV+ZmlsZX5uYW1lfmlufnRoZX5mb3Jtfi1+ZHJpdmU6XG5hbWUuZXh0RzYi STtmb3J+ZXhhbXBsZTp+fn5BOlxEQVRBLkRUQUc2Ig== STFUaGV+ZmlsZX5uYW1lfmlzRzYi US5FOlxBTEcwNzEuRFRBNiI= SUxJbnB1dH50aGV+bnVtYmVyfm9mfmVxdWF0aW9uc34tfmFufmludGVnZXIuRzYi SSVOfmlzRzYi IiIl The original system - output by rows: 10.00000000 -1.00000000 2.00000000 0.00000000 6.00000000 -1.00000000 11.00000000 -1.00000000 3.00000000 25.00000000 2.00000000 -1.00000000 10.00000000 -1.00000000 -11.00000000 0.00000000 3.00000000 -1.00000000 8.00000000 15.00000000 The initial approximation: 0.00000000 0.00000000 0.00000000 0.00000000 STZJbnB1dH50aGV+dG9sZXJhbmNlLnwrRzYi SS1Ub2xlcmFuY2V+PX5HNiI= JCIiIiEiJQ== SUVJbnB1dH5tYXhpbXVtfm51bWJlcn5vZn5pdGVyYXRpb25zLnwrRzYi SUBNYXhpbXVtfm51bWJlcn5vZn5pdGVyYXRpb25zfj1+RzYi IiNE Iter# Vector 1 0.60000000 2.27272727 -1.10000000 1.87500000 2 1.04727273 1.71590909 -0.80522727 0.88522727 3 0.93263636 2.05330579 -1.04934091 1.13088068 4 1.01519876 1.95369576 -0.96810863 0.97384272 5 0.98899130 2.01141473 -1.01028590 1.02135051 6 1.00319865 1.99224126 -0.99452174 0.99443374 7 0.99812847 2.00230688 -1.00197223 1.00359431 8 1.00062513 1.99867030 -0.99903558 0.99888839 9 0.99967415 2.00044767 -1.00036916 1.00061919 10 1.00011860 1.99976795 -0.99982814 0.99978598 11 0.99994242 2.00008477 -1.00006833 1.00010850 12 1.00002214 1.99995896 -0.99996916 0.99995967 STpDaG9pY2V+b2Z+b3V0cHV0fm1ldGhvZDp8K0c2Ig== STUxLn5PdXRwdXR+dG9+c2NyZWVufCtHNiI= STgyLn5PdXRwdXR+dG9+dGV4dH5maWxlfCtHNiI= STZQbGVhc2V+ZW50ZXJ+MX5vcn4yLnwrRzYi SS9Zb3VyfmlucHV0fmlzfkc2Ig== IiIi JACOBI ITERATIVE METHOD FOR LINEAR SYSTEMS The solution vector is : 0.99998973 2.00001582 -1.00001256 1.00001924 using 13 iterations with Tolerance 1.0000000000e-04 in infinity-norm JSFH