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 failure print(`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 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(`Mximum 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): else OUP := 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 then fprintf(OUP, ` I P F(P)\134n`): fi: F0 := F(P0): # Step 1 I1 := 1: OK := TRUE: # Step 2 while I1 <= N0 and OK = TRUE do # Step 3 # Compute P(I) FP0 := FP(P0): D1 := F0/FP0: # Step 6 P0 := P0 - D1: F0 := F(P0): if FLAG = 2 then fprintf(OUP,`%3d %14.8e %14.7e\134n`,I1,P0,F0): fi: # Step 4 if abs(D1) < TOL then # Procedure completed successfully fprintf(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 5 else I1 := I1+1: fi: od: if OK = TRUE then # Step 7 # Procedure completed unsuccessfully fprintf(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 then fclose(OUP): print(`Output file `,NAME,` created successfully`): fi: fi: STlUaGlzfmlzfk5ld3Rvbidzfk1ldGhvZHwrRzYi SUdJbnB1dH50aGV+ZnVuY3Rpb25+Rih4KX5pbn50ZXJtc35vZn54fCtHNiI= STVGb3J+ZXhhbXBsZTp+Y29zKHgpfCtHNiI= LCYtSSRjb3NHNiQlKnByb3RlY3RlZEdJKF9zeXNsaWJHNiI2I0kieEdGKCIiIkYqISIi ST1JbnB1dH5pbml0aWFsfmFwcHJveGltYXRpb258K0c2Ig== STlJbml0aWFsfmFwcHJveGltYXRpb25+PX5HNiI= JCIrTjspUiZ5ISM1 STFJbnB1dH50b2xlcmFuY2V8K0c2Ig== SS1Ub2xlcmFuY2V+PX5HNiI= JCIiJiEiJQ== SVdJbnB1dH5tYXhpbXVtfm51bWJlcn5vZn5pdGVyYXRpb25zfi1+bm9+ZGVjaW1hbH5wb2ludHwrRzYi ST9NeGltdW1+bnVtYmVyfm9mfml0ZXJhdGlvbnN+PX5HNiI= IiNE STtTZWxlY3R+b3V0cHV0fmRlc3RpbmF0aW9ufCtHNiI= SSsxLn5TY3JlZW58K0c2Ig== SS4yLn5UZXh0fmZpbGV8K0c2Ig== SS5FbnRlcn4xfm9yfjJ8K0c2Ig== SSlFbnRyeX49fkc2Ig== IiIi STlTZWxlY3R+YW1vdW50fm9mfm91dHB1dHwrRzYi STAxLn5BbnN3ZXJ+b25seXwrRzYi SUQyLn5BbGx+aW50ZXJtZWRpYXRlfmFwcHJveGltYXRpb25zfCtHNiI= SS5FbnRlcn4xfm9yfjJ8K0c2Ig== SSlFbnRyeX49fkc2Ig== IiIj Newton's Method I P F(P) 1 7.39536134e-01 -7.5487470e-04 2 7.39085178e-01 -7.5100000e-08 Approximate solution = 0.73908518 with F(P) = -7.5100000000e-08 Number of iterations = 2 Tolerance = 5.0000000000e-04 JSFH