restart: # BISECTION ALGORITHM 2.1 # # To find a solution to f(x) = 0 given the continuous function # f on the interval [a,b], where f(a) and f(b) have # opposite signs: # # INPUT: endpoints a,b: tolerance TOL: # maximum number of iterations NO. # # OUTPUT: approximate solution p or # a message that the algorithm fails. print(`This is the Bisection Method.`): print(`Input the function F(x) in terms of x`): print(`For example: cos(x)`): F := scanf(`%a`)[1]: print(F): F := unapply(F,x): OK := FALSE: while OK = FALSE do print(`Input endpoints A < B separated by blank`): A := scanf(`%f`)[1]: print(`A= `): print(A): B := scanf(`%f`)[1]: print(`B= `): print(B): if A > B then X := A: A := B: B := X: fi: if A = B then print(`A cannot equal B`): else FA := F(A): FB := F(B): if FA*FB > 0 then print(`F(A) and F(B) have same sign`): else OK := TRUE: fi: fi: od: OK := FALSE: while OK = FALSE do print(`Input tolerance`): TOL := scanf(`%f`)[1]: print(`Tolerance = `): print(TOL): if TOL <= 0 then print(`Tolerance must be positive`): else OK := TRUE: fi: od: OK := FALSE: while OK = FALSE do print(`Input maximum number of iterations - no decimal point`): NO := scanf(`%d`)[1]: print(`Maximum number of iterations = `): print(NO): if NO <= 0 then print(`Must be positive integer`): else OK := TRUE: fi: od: if OK = TRUE then print(`Select output destination`): print(`1. Screen`): print(`2. Text file`): print(`Enter 1 or 2`): FLAG := scanf(`%d`)[1]:print(`Entry = `):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]: OUP := fopen(NAME,WRITE,TEXT): else OUP := default: fi: print(`Select amount of output`): print(`1. Answer only`): print(`2. All intermediate approximations`): print(`Enter 1 or 2`): FLAG := scanf(`%d`)[1]:print(`Entry = `):print(FLAG): fprintf(OUP,`Bisection Method\134n`): if FLAG = 2 then fprintf(OUP, ` I P F(P)\134n`): fi: # Step 1 K := 1: # Step 2 OK := TRUE: while K <= NO and OK = TRUE do # Step 3 # Compute P(I) C := (B - A) / 2.0: P := A + C: # Step 4 FP := F(P): if FLAG = 2 then fprintf(OUP,`%3d %15.8e %15.7e \134n`,K,P,FP): fi: if abs(FP) < 1.0e-20 or C < TOL then # Procedure completed successfully fprintf(OUP,`\134nApproximate solution P = %11.8f \134n`,P): fprintf(OUP,`with F(P) = %12.8f\134n`,FP): fprintf(OUP,`Number of iterations = %3d`,K): fprintf(OUP,` Tolerance = %15.8e\134n`,TOL): OK := FALSE: else # Step 5 K := K+1: # Step 6 # Compute A(I) and B(I) if FA*FP > 0 then A := P: FA := FP: else B := P: FB := FP: fi: fi: od: if OK = TRUE then # Step 7 # Procedure completed unsuccessfully fprintf(OUP,`\134nIteration number %3d`,NO): fprintf(OUP,` gave approximation %12.8f\134n`,P): fprintf(OUP,`F(P) = %12.8f not within tolerance : %15.8e\134n`,FP,TOL): fi: if OUP <> default then fclose(OUP): printf(`Output file %s created successfully`,NAME): fi: fi: ST5UaGlzfmlzfnRoZX5CaXNlY3Rpb25+TWV0aG9kLkc2Ig== SUZJbnB1dH50aGV+ZnVuY3Rpb25+Rih4KX5pbn50ZXJtc35vZn54RzYi STRGb3J+ZXhhbXBsZTp+Y29zKHgpRzYi LCgqJEkieEc2IiIiJCIiIiokRiQiIiMiIiUhIzVGJw== SUlJbnB1dH5lbmRwb2ludHN+QX48fkJ+c2VwYXJhdGVkfmJ5fmJsYW5rRzYi SSRBPX5HNiI= JCIiIiIiIQ== SSRCPX5HNiI= JCIiIyIiIQ== STBJbnB1dH50b2xlcmFuY2VHNiI= SS1Ub2xlcmFuY2V+PX5HNiI= JCIiJiEiJQ== SVZJbnB1dH5tYXhpbXVtfm51bWJlcn5vZn5pdGVyYXRpb25zfi1+bm9+ZGVjaW1hbH5wb2ludEc2Ig== SUBNYXhpbXVtfm51bWJlcn5vZn5pdGVyYXRpb25zfj1+RzYi IiNE STpTZWxlY3R+b3V0cHV0fmRlc3RpbmF0aW9uRzYi SSoxLn5TY3JlZW5HNiI= SS0yLn5UZXh0fmZpbGVHNiI= SS1FbnRlcn4xfm9yfjJHNiI= SSlFbnRyeX49fkc2Ig== IiIi SThTZWxlY3R+YW1vdW50fm9mfm91dHB1dEc2Ig== SS8xLn5BbnN3ZXJ+b25seUc2Ig== SUMyLn5BbGx+aW50ZXJtZWRpYXRlfmFwcHJveGltYXRpb25zRzYi SS1FbnRlcn4xfm9yfjJHNiI= SSlFbnRyeX49fkc2Ig== IiIj Bisection Method I P F(P) 1 1.50000000e+00 2.3750000e+00 2 1.25000000e+00 -1.7968750e+00 3 1.37500000e+00 1.6210938e-01 4 1.31250000e+00 -8.4838867e-01 5 1.34375000e+00 -3.5098267e-01 6 1.35937500e+00 -9.6408842e-02 7 1.36718750e+00 3.2355780e-02 8 1.36328125e+00 -3.2149969e-02 9 1.36523438e+00 7.2030000e-05 10 1.36425781e+00 -1.6046697e-02 11 1.36474609e+00 -7.9892590e-03 Approximate solution P = 1.36474609 with F(P) = -0.00798926 Number of iterations = 11 Tolerance = 5.00000000e-04 JSFH