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 doprint(`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 thenX := A:A := B:B := X:fi:if A = B thenprint(`A cannot equal B`):elseFA := F(A):FB := F(B):if FA*FB > 0 thenprint(`F(A) and F(B) have same sign`):elseOK := TRUE:fi:fi:od:OK := FALSE:while OK = FALSE doprint(`Input tolerance`): TOL := scanf(`%f`)[1]: print(`Tolerance = `): print(TOL):if TOL <= 0 thenprint(`Tolerance must be positive`):else OK := TRUE:fi:od:OK := FALSE:while OK = FALSE doprint(`Input maximum number of iterations - no decimal point`):NO := scanf(`%d`)[1]: print(`Maximum number of iterations = `): print(NO):if NO <= 0 thenprint(`Must be positive integer`):else OK := TRUE:fi:od:if OK = TRUE thenprint(`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 thenprint(`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):elseOUP := 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 thenfprintf(OUP, ` I P F(P)\134n`):fi:# Step 1K := 1:# Step 2OK := TRUE:while K <= NO and OK = TRUE do# Step 3# Compute P(I)C := (B - A) / 2.0:P := A + C:# Step 4FP := F(P):if FLAG = 2 thenfprintf(OUP,`%3d %15.8e %15.7e \134n`,K,P,FP):fi:if abs(FP) < 1.0e-20 or C < TOL then# Procedure completed successfullyfprintf(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 5K := K+1:# Step 6# Compute A(I) and B(I)if FA*FP > 0 thenA := P:FA := FP:elseB := P:FB := FP:fi:fi:od:if OK = TRUE then# Step 7# Procedure completed unsuccessfullyfprintf(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 thenfclose(OUP):printf(`Output file %s created successfully`,NAME):fi:fi:JSFH