restart:# NEWTON-RAPHSON ALGORITHM 2.3## To find a solution to f(x) = 0 given an# initial approximation p0:## INPUT: initial approximation p0: tolerance TOL:# maximum number of iterations NO.## OUTPUT: approximate solution p or a message of failureprint(`This is Newton's 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):FP := unapply(diff(F,x),x):F := unapply(F,x):OK := FALSE:print(`Input initial approximation\134n`):P0 := scanf(`%f`)[1]:print(`Initial approximation = `):print(P0):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(`Mximum 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, `Newton's Method\134n`):if FLAG = 2 thenfprintf(OUP, ` I P F(P)\134n`):fi:F0 := F(P0):# Step 1I1 := 1:OK := TRUE:# Step 2while I1 <= N0 and OK = TRUE do # Step 3# Compute P(I)FP0 := FP(P0):D1 := F0/FP0:# Step 6P0 := P0 - D1: F0 := F(P0):if FLAG = 2 thenfprintf(OUP,`%3d %14.8e %14.7e\134n`,I1,P0,F0):fi:# Step 4if abs(D1) < TOL then# Procedure completed successfullyfprintf(OUP,`\134nApproximate solution = %12.8f\134n`,P0):fprintf(OUP,`with F(P) = %.10e\134n`,F0):fprintf(OUP,`Number of iterations = %d\134n`,I1):fprintf(OUP,`Tolerance = %.10e\134n`,TOL):OK := FALSE:# Step 5elseI1 := I1+1:fi:od:if OK = TRUE then# Step 7# Procedure completed unsuccessfullyfprintf(OUP,`\134nIteration number %d`,N0):fprintf(OUP,` gave approximation %.10e\134n`,P0):fprintf(OUP,`with F(P) = %.10e not within tolerance %.10e\134n`,F0,TOL):fi:if OUP <> default thenfclose(OUP):print(`Output file `,NAME,` created successfully`):fi:fi:JSFH