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 doprint(`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 thenX := P0:P0 := P1:P1 := X:fi:if P0 = P1 then:print(`P0 cannot equal P1\134n`):elseQ0 := F(P0):Q1 := F(P1):if Q0*Q1 > 0 thenprint(`F(P0) and F(P1) have the same sign.\134n`):elseOK := TRUE:fi:fi:od:OK := FALSE:while OK = FALSE doprint(`Input tolerance\134n`):TOL := scanf(`%f`)[1]:print(`Tolerance = `):print(TOL): if TOL <= 0 thenprint(`Tolerance must be positive\134n`):else OK := TRUE:fi:od:OK := FALSE:while OK = FALSE doprint(`Input maximum number of iterations - no decimal point\134n`):N0:= scanf(`%d`)[1]:print(`Maximum number of iterations = `):print(N0): if N0 <= 0 thenprint(`Must be positive integer\134n`):else OK := TRUE:fi:od:if OK = TRUE thenprint(`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 thenprint(`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):elseOUP := 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 thenfprintf(OUP, ` I P F(P)\134n`):fi:# Step 1I1 := 2:OK := TRUE:Q0 := F(P0):Q1 := F(P1):# Step 2while I1 <= N0 and OK = TRUE do# Step 3# Compute P(I)P := P1-Q1*(P1-P0)/(Q1-Q0):Q := F(P):if FLAG = 2 thenfprintf(OUP,`%3d%15.8e%15.8e\134n`,I1,P,Q):fi:# Step 4if abs(P-P1) < TOL then# Procedure completed successfullyfprintf(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 5I1 := I1+1:# Step 6# Compute P0(I) and P1(I) if Q*Q1 < 0 thenP0 := P1:Q0 := Q1:fi:# Step 7# Procedure completed unsuccessfullyP1 := P:Q1 := Q:fi:od:if OK = TRUE thenfprintf(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 thenfclose(OUP):print(`Output file `,NAME,` created successfully`):fi:fi:JSFH