restart: # METHOD OF FALSE POSITION ALGORITHM 2.5 # # To find a solution to f(x) = 0 given the continuous function # f on the interval [p0,p1], where f(p0) and f(p1) have # opposite signs: # # INPUT: endpoints p0, p1: tolerance TOL: # maximum number of iterations N0. # # OUTPUT: approximate solution p or # a message that the algorithm fails. print(`This is the Method of False Position\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 endpoints P0 < P1 separated by a blank space\134n`): P0 := scanf(`%f`)[1]:print(`P0 = `):print(P0): P1 := scanf(`%f`)[1]:print(`P1 = `):print(P1): if P0 > P1 then X := P0: P0 := P1: P1 := X: fi: if P0 = P1 then :print(`P0 cannot equal P1\134n`): else Q0 := F(P0): Q1 := F(P1): if Q0*Q1 > 0 then print(`F(P0) and F(P1) have the same sign.\134n`): else OK := TRUE: fi: 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, `METHOD OF FALSE POSITION OR REGULA FALSII\134n\134n`): if FLAG = 2 then fprintf(OUP, ` I P F(P)\134n`): fi: # Step 1 I1 := 2: OK := TRUE: Q0 := F(P0): Q1 := F(P1): # Step 2 while I1 <= N0 and OK = TRUE do # Step 3 # Compute P(I) P := P1-Q1*(P1-P0)/(Q1-Q0): Q := F(P): if FLAG = 2 then fprintf(OUP,`%3d%15.8e%15.8e\134n`,I1,P,Q): 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`,Q): fprintf(OUP,`Number of iterations = %3d`,I1): fprintf(OUP,` Tolerance = %15.8e\134n`,TOL): OK := FALSE: else # Step 5 I1 := I1+1: # Step 6 # Compute P0(I) and P1(I) if Q*Q1 < 0 then P0 := P1: Q0 := Q1: fi: # Step 7 # Procedure completed unsuccessfully P1 := P: Q1 := Q: fi: od: if OK = TRUE then fprintf(OUP,`\134nIteration number %3d`,N0): fprintf(OUP,` gave approximation %12.8f\134n`,P): fprintf(OUP,`F(P) = %12.8f not within tolerance: %15.8e\134n`,Q,TOL): fi: if OUP <> default then fclose(OUP): print(`Output file `,NAME,` created successfully`): fi: fi: SUZUaGlzfmlzfnRoZX5NZXRob2R+b2Z+RmFsc2V+UG9zaXRpb258K0c2Ig== SUdJbnB1dH50aGV+ZnVuY3Rpb25+Rih4KX5pbn50ZXJtc35vZn54fCtHNiI= STVGb3J+ZXhhbXBsZTp+Y29zKHgpfCtHNiI= LCYtSSRjb3NHNiQlKnByb3RlY3RlZEdJKF9zeXNsaWJHNiI2I0kieEdGKCIiIkYqISIi SVRJbnB1dH5lbmRwb2ludHN+UDB+PH5QMX5zZXBhcmF0ZWR+Ynl+YX5ibGFua35zcGFjZXwrRzYi 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 METHOD OF FALSE POSITION OR REGULA FALSII I P F(P) 2 7.36384139e-01 4.51771860e-03 3 7.39058139e-01 4.51772000e-05 4 7.39084864e-01 4.50900000e-07 Approximate solution P = 0.73908486 with F(P) = 0.00000045 Number of iterations = 4 Tolerance = 5.00000000e-04 JSFH