restart: # STEFFENSEN'S ALGORITHM 2.6 # # To find a solution to g(x) = x # given an initial approximation p0: # # INPUT: initial approximation p0: tolerance TOL: # maximum number of iterations N0. # # OUTPUT: approximate solution p or # a message that the method fails. print(`This is Steffensens Method.\134n`): print(`Input the function G(x) in terms of x\134n`): print(`For example: cos(x)\134n`): G := scanf(`%a`)[1]:print(G): G := unapply(G,x): OK := FALSE: print(`Input initial approximation\134n`): P0 := scanf(`%f`)[1]:print(`Initial approximation = `):print(P0): while OK = FALSE do print(`Input tolerance\134n`): TOL := scanf(`%f`)[1]:print(`Tolerance = `):print(TOL): if TOL <= 0 then print(`Tolerance must be positive\134n`): else OK := TRUE: fi: od: OK := FALSE: while OK = FALSE do print(`Input maximum number of iterations - no decimal point\134n`): N0 := scanf(`%d`)[1]:print(`Maximum number of iterations = `):print(N0): if N0 <= 0 then print(`Must be positive integer\134n`): else OK := TRUE: fi: od: if OK = TRUE then print(`Select output destination\134n`): print(`1. Screen\134n`): print(`2. Text file\134n`): print(`Enter 1 or 2\134n`): FLAG := scanf(`%d`)[1]:print(`Entry = `):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]: OUP := fopen(NAME,WRITE,TEXT): else OUP := default: fi: print(`Select amount of output\134n`): print(`1. Answer only\134n`): print(`2. All intermediate approximations\134n`): print(`Enter 1 or 2\134n`): FLAG := scanf(`%d`)[1]:print(`Entry = `):print(FLAG): fprintf(OUP, `STEFFENSEN'S METHOD\134n`): if FLAG = 2 then fprintf(OUP, ` I P\134n`): fi: # Step 1 I1 := 1: OK := TRUE: # Step 2 while I1 <= N0 and OK = TRUE do # Step 3 # Compute P(1) with superscript (I-1) P1 := G(P0): # Compute P(2) with superscript (I-1) P2 := G(P1): if abs(P2-2*P1+P0) < 1.0e-20 then FLAG := 1: D1 := 10: fprintf(OUP,`Denominator = 0, method fails\134n`): fprintf(OUP,`best possible is P2(%2d) = %15.8f\134n`,I1,P2): OK := FALSE: else D1 := (P1-P0)*(P1-P0)/(P2-2*P1+P0): fi: # Compute P(0) with superscript (I) P := P0-D1: if FLAG = 2 then fprintf(OUP, `%3d%15.8e\134n`, I1, P): fi: # Step 4 if abs(D1) < TOL then # Procedure completed successfully fprintf(OUP, `\134nApproximate solution := %12.8f\134n`, P): fprintf(OUP, `Number of iterations := %3d`, I1): fprintf(OUP, ` Tolerance := %15.8e\134n`,TOL): OK := FALSE: else # Step 5 I1 := I1+1: # Step 6 # Update P0 P0 := P: fi: od: if OK = TRUE then # Step 7 # Procedure completed unsuccessfully fprintf(OUP, `\134nIteration number %3d`, N0): fprintf(OUP, ` gave approximation %12.8f\134n`, P): fprintf(OUP, `not within tolerance %14e\134n`,TOL): fi: if OUP <> default then fclose(OUP): print(`Output file `,NAME,` created successfully`): fi: fi: ST1UaGlzfmlzflN0ZWZmZW5zZW5zfk1ldGhvZC58K0c2Ig== SUdJbnB1dH50aGV+ZnVuY3Rpb25+Ryh4KX5pbn50ZXJtc35vZn54fCtHNiI= STVGb3J+ZXhhbXBsZTp+Y29zKHgpfCtHNiI= LCQqJCokLCYkIiNTISIiIiIiSSJ4RzYiRilGKCNGKSIiIyQiK2d3RmlKISIq ST1JbnB1dH5pbml0aWFsfmFwcHJveGltYXRpb258K0c2Ig== STlJbml0aWFsfmFwcHJveGltYXRpb25+PX5HNiI= JCIjOiEiIg== STFJbnB1dH50b2xlcmFuY2V8K0c2Ig== SS1Ub2xlcmFuY2V+PX5HNiI= JCIiJiEiJQ== SVdJbnB1dH5tYXhpbXVtfm51bWJlcn5vZn5pdGVyYXRpb25zfi1+bm9+ZGVjaW1hbH5wb2ludHwrRzYi SUBNYXhpbXVtfm51bWJlcn5vZn5pdGVyYXRpb25zfj1+RzYi IiNE STtTZWxlY3R+b3V0cHV0fmRlc3RpbmF0aW9ufCtHNiI= SSsxLn5TY3JlZW58K0c2Ig== SS4yLn5UZXh0fmZpbGV8K0c2Ig== SS5FbnRlcn4xfm9yfjJ8K0c2Ig== SSlFbnRyeX49fkc2Ig== IiIi STlTZWxlY3R+YW1vdW50fm9mfm91dHB1dHwrRzYi STAxLn5BbnN3ZXJ+b25seXwrRzYi SUQyLn5BbGx+aW50ZXJtZWRpYXRlfmFwcHJveGltYXRpb25zfCtHNiI= SS5FbnRlcn4xfm9yfjJ8K0c2Ig== SSlFbnRyeX49fkc2Ig== IiIj STEFFENSEN'S METHOD I P 1 1.36526522e+00 2 1.36523001e+00 Approximate solution := 1.36523001 Number of iterations := 2 Tolerance := 5.00000000e-04 JSFH