restart: # SECANT ALGORITHM 2.4 # # To find a solution to the equation f(x) = 0 # given initial approximations p0 and p1: # # INPUT: initial approximation p0, p1: tolerance TOL: # maximum number of iterations N0. # # OUTPUT: approximate solution p or # a message that the algorithm fails. :print(`This is the Secant Method\134n`): :print(`Input the function F(x) in terms of x\134n`): :print(`For example: cos(x)\134n`): F := scanf(`%a`)[1]:print(F): F := unapply(F,x): OK := FALSE: while OK = FALSE do :print(`Input initial approximations P0 and P1 separated by a blank\134n`): P0 := scanf(`%f`)[1]:print(`P0 = `):print(P0): P1 := scanf(`%f`)[1]:print(`P1 = `):print(P1): if P0 = P1 then :print(`P0 cannot equal P1\134n`): else OK := TRUE: fi: od: OK := FALSE: 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, `Secant Method\134n`): if FLAG = 2 then fprintf(OUP, ` I P F(P)\134n`): fi: # Step 1 I1 := 2: F0 := F(P0): F1 := F(P1): OK := TRUE: # Step 2 while I1 <= N0 and OK = TRUE do # Step 3 # Compute P(I) P := P1-F1*(P1-P0)/(F1-F0): FP := F(P): if FLAG = 2 then fprintf(OUP,`%3d %15.8e %15.8e\134n`,I1,P,FP): fi: # Step 4 if abs(P-P1) < TOL then # Procedure completed successfully fprintf(OUP,`\134nApproximate solution P = %12.8f\134n`,P): fprintf(OUP,`with F(P) = %12.8f\134n`,FP): fprintf(OUP,`Number of iterations = %d\134n`,I1): fprintf(OUP,`Tolerance = %14.8e\134n`,TOL): OK := FALSE: # Step 5 else I1 := I1+1: # Step 6 # Update P0, F0, P1, F1 P0 := P1: F0 := F1: P1 := P: F1 := FP: fi: od: if OK = TRUE then # Step 7 # Procedure completed unsuccessfully fprintf(OUP,`\134nIteration number %d`,N0): fprintf(OUP,` gave approximation %12.8f\134n`,P): fprintf(OUP,`with F(P) = %12.8f not within tolerance %15.8e\134n`,FP,TOL): fi: if OUP <> default then fclose(OUP): :print(`Output file `,NAME,` created successfully`): fi: fi: STtUaGlzfmlzfnRoZX5TZWNhbnR+TWV0aG9kfCtHNiI= SUdJbnB1dH50aGV+ZnVuY3Rpb25+Rih4KX5pbn50ZXJtc35vZn54fCtHNiI= STVGb3J+ZXhhbXBsZTp+Y29zKHgpfCtHNiI= LCYtSSRjb3NHNiQlKnByb3RlY3RlZEdJKF9zeXNsaWJHNiI2I0kieEdGKCIiIkYqISIi SWduSW5wdXR+aW5pdGlhbH5hcHByb3hpbWF0aW9uc35QMH5hbmR+UDF+c2VwYXJhdGVkfmJ5fmF+Ymxhbmt8K0c2Ig== SSZQMH49fkc2Ig== JCIiJiEiIg== SSZQMX49fkc2Ig== JCIrTjspUiZ5ISM1 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 Secant Method I P F(P) 2 7.36384139e-01 4.51771860e-03 3 7.39058139e-01 4.51772000e-05 4 7.39085149e-01 -2.69000000e-08 Approximate solution P = 0.73908515 with F(P) = -0.00000003 Number of iterations = 4 Tolerance = 5.00000000e-04 JSFH