Fortran Program For Secant Method Iteration

Posted on  by admin

Contents. with Ada. TextIo; use Ada. TextIo; procedure RootsOfFunction is package RealIo is new Ada. Translation of: printroots = (f, begin, end, step ) - # Print approximate roots of f between x=begin and x=end, # using sign changes as an indicator that a root has been # encountered. X = begin y = f (x ) lasty = y crossxaxis = - (lasty 0 ) or (lasty 0 and y # Smaller steps produce more accurate/precise results in general, # but for many functions we'll never get exact roots, either due # to imperfect binary representation or irrational roots.

Fortran Program For Secant Method Iteration

Step = 1 / 256 f1 = (x ) - x.x.x - 3.x.x + 2.x printroots f1, - 1, 5, step f2 = (x ) - x.x - 4.x + 3 printroots f2, - 1, 5, step f3 = (x ) - x - 1.5 printroots f3, 0, 4, step f4 = (x ) - x.x - 2 printroots f4, - 2, 2, step output coffee roots.coffee - Root found at 0 Root found at 1 Root found at 2 - Root found at 1 Root found at 3 - Root found at 1.5 - Root found near -1.4140625 Root found near 1.41796875. Output: Root found at 8.6218e-16 Root found at 1.000016 Root found at 1.998914 % Implemented by Arjun Sunel - module (roots ). export ( main / 0 ). Main ( ) - F = fun ( X ) - X. X. X - 3. X.

X + 2. X end, Step = 0.001,% Using smaller steps will provide more accurate results Start = - 1, Stop = 3, Sign = F ( Start ) 0, X = Start, while ( X, Step, Start, Stop, Sign, F ).

9.2 Secant Method, False Position Method, and Ridders’ Method 347 Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X).

Secant

While ( X, Step, Start, Stop, Sign, F ) - Value = F ( X ), if Value 0 -% We hit a root: format ( 'Root found at pn'X ), while ( X + Step, Step, Start, Stop, Value 0, F ); ( Value% We passed a root: format ( 'Root found near pn'X ), while ( X + Step, Step, Start, Stop, Value 0, F ); X Stop -: format ( ' ); true - while ( X + Step, Step, Start, Stop, Value 0, F ) end. Output: Root found near 8.6218e-16 Root found near 1.000016 Root found near 2.998915 ok PROGRAM ROOTSFUNCTION!VAR E,X,STP,VALUE,S%,I%,LIMIT%,X1,X2,D FUNCTION F(X) F=X.X.X-3.X.X+2.X END FUNCTION BEGIN X=-1 STP=1.0E-6 E=1.0E-9 S%=(F(X)0) PRINT('VERSION 1: SIMPLY STEPPING X') WHILE XLIMIT% THEN PRINT('ERROR: FUNCTION NOT CONVERGING') EXIT END IF D=(X2-X1)/(F(X2)-F(X1)).F(X2) IF ABS(D). Output: Root (to 12DP) Max. Error 0.08 1e-06 1.16 1e-06 2.34 1e-06 Note that the roots found are all near misses because fractional numbers that seem nice and 'round' in decimal (such as 10^-6) often have some rounding error when represented in binary. To increase the chances of finding exact integer roots, try using an integer start value with a step value that is a power of two. Main procedure print ( 'Root (to 12DP) tMax.

Error n' ) for , r in pairs (root (f, - 1, 3, 2 ^- 10 ) ) do print ( string.format ( '%0.12f', r.val ), r.err ) end. Output: findRoots(#f, -1, 3, 0.0001) Root found at 0 Root found at 1 Root found at 2 findRoots(#f, -1.000001, 3, 0.0001) Root near 9.0713e-005 Root near 1.000099 Root near 2.000099 If the equation is a polynomial, we can put the coefficients in a vector and use roots: a = 1, - 3, 2, 0 ; r = (a );% let's print it for = 1: 3 n = (a, r ( ) ); ( 'x%d =%f (%f', r ( ), n ); if (n!= 0.0 ) ( ' not' ); endif ( ' exact) n' ); endfor Otherwise we can program our (simple) method. Translation of: Procedure.d f (x.d ) ProcedureReturn x.x.x - 3.x.x + 2.x EndProcedure Procedure main ( ) OpenConsole ( ) Define.d StepSize = 0.001 Define.d Start =- 1, stop = 3 Define.d value =f (start ), x =start Define.i oldsign =Sign (value ) If value = 0 PrintN ( 'Root found at ' + StrF (start ) ) EndIf While x oldsign PrintN ( 'Root found near ' + StrF (x ) ) ElseIf value = 0 PrintN ( 'Root found at ' + StrF (x ) ) EndIf oldsign =Sign (value ) x +StepSize Wend EndProcedure main ( ).

Secant Method In Excel

Newton's Method FORTRAN 77 Code PROGRAM NEWTON C - C Alg2'5.for FORTRAN program for implementing Algorithm 2.5 C C NUMERICAL METHODS: FORTRAN Programs, (c) John H. Mathews 1995 C To accompany the text: C NUMERICAL METHODS for Math., Science & Engineering, 2nd Ed, 1992 C Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A. C Prentice Hall, Inc.; USA, Canada, Mexico ISBN 0-13-624990-6 C Prentice Hall, International Editions: ISBN 0-13-625047-5 C This free software is compliments of the author. C E-mail address: in%'mathews@fullerton.edu' C C Algorithm 2.5 (Newton-Raphson Iteration). C Section 2.4, Newton-Raphson and Secant Methods, Page 84 C - PARAMETER(Delta=5E-6,Epsilon=5E-6,Max=100) INTEGER Cond,K REAL Dp,P,P0,P1,Y1,RelErr CHARACTER ANS.1 EXTERNAL F,F1 10 CALL INPUT(P0) P=P0 CALL NEWRAP(F,F1,P,Delta,Epsilon,Max,P1,Dp,Y1,Cond,K) CALL RESULT(P0,P1,Dp,Y1,Cond,K) WRITE(9,.)' ' WRITE(9,.) 'WANT TO USE A DIFFERENT STARTING VALUE? ' READ(9,'(A)') ANS IF (ANS.EQ.'