restart: # POWER METHOD ALGORITHM 9.1 # # To approximate the dominant eigenvalue and an associated # eigenvector of the n by n matrix A given a nonzero vector x: # # INPUT: Dimension n: matrix A: vector x: tolerance TOL: maximum # number of iterations N. # # OUTPUT: Approximate eigenvalue MU: approximate eigenvector x # or a message that the maximum number of iterations was # exceeded. print(`This is the Power Method.\134n`): OK := FALSE: print(`Choice of input method`): print(`1. input from keyboard - not recommended for large matrices`): 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), A(2,1), A(2,2), ..., A(2,N)`): print(`..., A(N,1), A(N,2), ..., A(N,N)\134n`): print(`Place as many entries as desired on each line, but separate `): print(`entries with`): print(`at least one blank.\134n`): print(`The initial approximation should follow in same format.\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 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): OK := FALSE: while OK = FALSE do print(`Input the dimension n - 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 do A[I1-1,J-1] := fscanf(INP, `%f`)[1]: od: od: for I1 from 1 to N do X[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: else OK := FALSE: while OK = FALSE do print(`Input the dimension n - an integer.`): N := scanf(`%d`)[1]: print(`N= `): print(N): if N > 0 then print(`Input Matrix `): for I1 from 1 to N do for J from 1 to N 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: print(`Input initial approximation vector `): for I1 from 1 to N do print(`input entry in position `,I1): X[I1-1] := scanf(`%f`)[1]:print(`Data is `):print(X[I1-1]): od: OK := TRUE: else print(`The number must be a positive integer.\134n`): fi: od: 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 positive number.\134n`): fi: od: OK := FALSE: while OK = FALSE do print(`Input maximum number of iterations `): print(`- integer.\134n`): NN := scanf(`%d`)[1]:print(`Maximum number of iterations = `):print(NN): # Use NN in place of N if NN > 0 then OK := TRUE: else print(`Number must be positive integer.\134n`): fi: od: fi: if OK = TRUE then OUP := default: fprintf(OUP, `The original matrix - output by rows:\134n`): for I1 from 1 to N do for J from 1 to N do fprintf(OUP, ` %11.8f`, A[I1-1,J-1]): od: fprintf(OUP, `\134n`): od: fprintf(OUP, `The initial approximation vector is :\134n`): for I1 from 1 to N do fprintf(OUP, ` %11.8f`, X[I1-1]): od: fprintf(OUP, `\134n`): fi: if OK = TRUE then print(`Choice of output method:`): print(`1. Output to screen`): print(`2. Output to text file`): print(`Please enter 1 or 2.`): FLAG := scanf(`%d`)[1]: print(`Input = `):print(FLAG): if FLAG = 2 then print(`Input the file name in the form - drive:\134\134name.ext`): print(`for example A:\134\134OUTPUT.DTA`): NAME := scanf(`%s`)[1]:print(`Output = `):print(NAME): OUP := fopen(NAME,WRITE,TEXT): else OUP := default: fi: fprint(OUP, `POWER METHOD\134n\134n`): fprintf(OUP, `Iter Aapprox Approx eigenvector\134n`): fprintf(OUP, ` eigenvalue\134n`): # Step 1 K := 1: # Step 2 LP := 1: AMAX := abs(X[0]): for I1 from 2 to N do if abs(X[I1-1]) > AMAX then AMAX := abs(X[I1-1]): LP := I1: fi: od: # Step 3 for I1 from 1 to N do X[I1-1] := X[I1-1]/AMAX: od: # Step 4 while K <= NN and OK = TRUE do # Step 5 for I1 from 1 to N do Y[I1-1] := 0: for J from 1 to N do Y[I1-1] := Y[I1-1] + A[I1-1,J-1] * X[J-1]: od: od: # Step 6 YMU := Y[LP-1]: # Step 7 LP := 1: AMAX := abs(Y[0]): for I1 from 2 to N do if abs(Y[I1-1]) > AMAX then AMAX := abs(Y[I1-1]): LP := I1: fi: od: # Step 8 if AMAX <= 0 then print(`0 eigenvalue - select another `): print(`initial vector and begin again\134n`): OK := FALSE: else # Step 9 ERR := 0: for I1 from 1 to N do T := Y[I1-1]/Y[LP-1]: if abs(X[I1-1]-T) > ERR then ERR := abs(X[I1-1]-T): fi: X[I1-1] := T: od: fprintf(OUP, `%d %12.8f`, K, YMU): # Step 10 for I1 from 1 to N do fprintf(OUP, ` %11.8f`, X[I1-1]): od: fprintf(OUP, `\134n`): if ERR <= TOL then fprintf(OUP, `\134n\134nThe eigenvalue = %12.8f`,YMU): fprintf(OUP, ` to tolerance = %.10e\134n`, TOL): fprintf(OUP, `obtained on iteration number = %d\134n\134n`, K): fprintf(OUP, `Unit eigenvector is :\134n\134n`): for I1 from 1 to N do fprintf(OUP, ` %11.8f`, X[I1-1]): od: fprintf(OUP, `\134n`): OK := FALSE: fi: # Step 11 K := K+1: fi: # Step 12 od: if K > NN then print(`Method did not converge within `,NN,` iterations`): fi: if OUP <> default then fclose(OUP): print(`Output file `,NAME,` created successfully`): fi: fi: STtUaGlzfmlzfnRoZX5Qb3dlcn5NZXRob2QufCtHNiI= STdDaG9pY2V+b2Z+aW5wdXR+bWV0aG9kRzYi SWZuMS5+aW5wdXR+ZnJvbX5rZXlib2FyZH4tfm5vdH5yZWNvbW1lbmRlZH5mb3J+bGFyZ2V+bWF0cmljZXNHNiI= SToyLn5pbnB1dH5mcm9tfmF+dGV4dH5maWxlRzYi STVQbGVhc2V+ZW50ZXJ+MX5vcn4yLkc2Ig== SS5Zb3VyfmlucHV0fmlzRzYi IiIj SVZUaGV+YXJyYXl+d2lsbH5iZX5pbnB1dH5mcm9tfmF+dGV4dH5maWxlfmlufnRoZX5vcmRlckc2Ig== SWduQSgxLDEpLH5BKDEsMiksfi4uLix+QSgxLE4pLH5BKDIsMSksfkEoMiwyKSx+Li4uLH58K35+fkEoMixOKUc2Ig== SUIuLi4sfkEoTiwxKSx+QShOLDIpLH4uLi4sfkEoTixOKXwrRzYi SWduUGxhY2V+YXN+bWFueX5lbnRyaWVzfmFzfmRlc2lyZWR+b25+ZWFjaH5saW5lLH5idXR+c2VwYXJhdGV+RzYi SS1lbnRyaWVzfndpdGhHNiI= STVhdH5sZWFzdH5vbmV+YmxhbmsufCtHNiI= SVlUaGV+aW5pdGlhbH5hcHByb3hpbWF0aW9ufnNob3VsZH5mb2xsb3d+aW5+c2FtZX5mb3JtYXQufCtHNiI= SWpuSGFzfnRoZX5pbnB1dH5maWxlfmJlZW5+Y3JlYXRlZD9+LX5lbnRlcn4xfmZvcn55ZXN+b3J+Mn5mb3J+bm8uRzYi STFZb3VyfnJlc3BvbnNlfmlzRzYi IiIi SVJJbnB1dH50aGV+ZmlsZX5uYW1lfmlufnRoZX5mb3Jtfi1+ZHJpdmU6XG5hbWUuZXh0RzYi STtmb3J+ZXhhbXBsZTp+fn5BOlxEQVRBLkRUQUc2Ig== STFUaGV+ZmlsZX5uYW1lfmlzRzYi US5GOlxBTEcwOTEuRFRBNiI= SURJbnB1dH50aGV+ZGltZW5zaW9ufm5+LX5hbn5pbnRlZ2VyLkc2Ig== SSVOfmlzRzYi IiIk STZJbnB1dH50aGV+dG9sZXJhbmNlLnwrRzYi SS1Ub2xlcmFuY2V+PX5HNiI= JCIiIiEiJg== SURJbnB1dH5tYXhpbXVtfm51bWJlcn5vZn5pdGVyYXRpb25zfkc2Ig== SSwtfmludGVnZXIufCtHNiI= SUBNYXhpbXVtfm51bWJlcn5vZn5pdGVyYXRpb25zfj1+RzYi IiNE The original matrix - output by rows: -4.00000000 14.00000000 0.00000000 -5.00000000 13.00000000 0.00000000 -1.00000000 0.00000000 2.00000000 The initial approximation vector is : 1.00000000 1.00000000 1.00000000 STlDaG9pY2V+b2Z+b3V0cHV0fm1ldGhvZDpHNiI= STQxLn5PdXRwdXR+dG9+c2NyZWVuRzYi STcyLn5PdXRwdXR+dG9+dGV4dH5maWxlRzYi STVQbGVhc2V+ZW50ZXJ+MX5vcn4yLkc2Ig== SSlJbnB1dH49fkc2Ig== IiIi Iter Aapprox Approx eigenvector eigenvalue 1 10.00000000 1.00000000 0.80000000 0.10000000 2 7.20000000 1.00000000 0.75000000 -0.11111111 3 6.50000000 1.00000000 0.73076923 -0.18803419 4 6.23076923 1.00000000 0.72222222 -0.22085048 5 6.11111111 1.00000000 0.71818182 -0.23591470 6 6.05454546 1.00000000 0.71621622 -0.24309495 7 6.02702703 1.00000000 0.71524664 -0.24658756 8 6.01345291 1.00000000 0.71476510 -0.24830578 9 6.00671141 1.00000000 0.71452514 -0.24915656 10 6.00335195 1.00000000 0.71440536 -0.24957942 11 6.00167503 1.00000000 0.71434552 -0.24979007 12 6.00083728 1.00000000 0.71431561 -0.24989515 13 6.00041858 1.00000000 0.71430066 -0.24994761 14 6.00020928 1.00000000 0.71429319 -0.24997382 15 6.00010464 1.00000000 0.71428945 -0.24998691 16 6.00005232 1.00000000 0.71428758 -0.24999346 The eigenvalue = 6.00005232 to tolerance = 1.0000000000e-05 obtained on iteration number = 16 Unit eigenvector is : 1.00000000 0.71428758 -0.24999346 JSFH