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`):elseOK := 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):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, `Secant Method\134n`):if FLAG = 2 thenfprintf(OUP, ` I P F(P)\134n`):fi:# Step 1I1 := 2:F0 := F(P0):F1 := F(P1):OK := TRUE:# Step 2while I1 <= N0 and OK = TRUE do # Step 3# Compute P(I)P := P1-F1*(P1-P0)/(F1-F0):FP := F(P):if FLAG = 2 thenfprintf(OUP,`%3d %15.8e %15.8e\134n`,I1,P,FP):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`,FP):fprintf(OUP,`Number of iterations = %d\134n`,I1):fprintf(OUP,`Tolerance = %14.8e\134n`,TOL):OK := FALSE:# Step 5elseI1 := I1+1:# Step 6# Update P0, F0, P1, F1P0 := P1:F0 := F1:P1 := P:F1 := FP:fi:od:if OK = TRUE then# Step 7# Procedure completed unsuccessfullyfprintf(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 thenfclose(OUP)::print(`Output file `,NAME,` created successfully`):fi:fi:JSFH