-
Fortran Program For Secant Method Vs Newton카테고리 없음 2020. 2. 21. 07:28
$UWHPSC/codes/fortran/newton/newton.f90 module newton! Module parameters: implicit none integer, parameter:: maxiter = 20 real ( kind = 8 ), parameter:: tol = 1. D - 14 contains subroutine solve ( f, fp, x0, x, iters, debug )! Estimate the zero of f(x) using Newton's method.! F: the function to find a root of! Fp: function returning the derivative f'! X0: the initial guess!
Debug: logical, prints iterations if debug=.true.! The estimate x satisfying f(x)=0 (assumes Newton converged!)! The number of iterations iters implicit none real ( kind = 8 ), intent ( in ):: x0 real ( kind = 8 ), external:: f, fp logical, intent ( in ):: debug real ( kind = 8 ), intent ( out ):: x integer, intent ( out ):: iters! Declare any local variables: real ( kind = 8 ):: deltax, fx, fxprime integer:: k!
Initial guess x = x0 if ( debug ) then print 11, x 11 format ( 'Initial guess: x = ', e22.15 ) endif! Newton iteration to find a zero of f(x) do k = 1, maxiter! Evaluate function and its derivative: fx = f ( x ) fxprime = fp ( x ) if ( abs ( fx ). $UWHPSC/codes/fortran/newton/test1.f90 program test1 use newton, only: solve use functions, only: fsqrt, fprimesqrt implicit none real ( kind = 8 ):: x, x0, fx real ( kind = 8 ):: x0vals ( 3 ) integer:: iters, itest logical:: debug! Print., 'Test routine for computing zero of f' debug =.true.! Values to test as x0: x0vals = ( / 1. D0 / ) do itest = 1, 3 x0 = x0vals ( itest ) print., ' '!
Secant Method Geeks For Geeks
Blank line call solve ( fsqrt, fprimesqrt, x0, x, iters, debug ) print 11, x, iters 11 format ( 'solver returns x = ', e22.15, ' after', i3, ' iterations' ) fx = fsqrt ( x ) print 12, fx 12 format ( 'the value of f(x) is ', e22.15 ) if ( abs ( x - 2. D0 ) 1 d - 14 ) then print 13, x 13 format ( '. Unexpected result: x = ', e22.15 ) endif enddo end program test1This makes use of a module functions.f90 that includes the definition ofthe functions used here. Since (f(x) = x^2 - 4), we are attempting tocompute the square root of 4.
X Exclude words from your searchPut - in front of a word you want to leave out. For example, jaguar speed -carSearch for an exact matchPut a word or phrase inside quotes.
For example, 'tallest building'.Search for wildcards or unknown wordsPut a. in your word or phrase where you want to leave a placeholder. For example, 'largest. in the world'.Search within a range of numbersPut. Between two numbers.
For example, camera $50.$100.Combine searchesPut 'OR' between each search query. For example, marathon OR race. Welcome!This is one of over 2,200 courses on OCW. Find materials for this course in the pages linked along the left.MIT OpenCourseWare is a free & open publication of material from thousands of MIT courses, covering the entire MIT curriculum.No enrollment or registration. Freely browse and use OCW materials at your own pace.
There's no signup, and no start or end dates.Knowledge is your reward. Use OCW to guide your own life-long learning, or to teach others. We don't offer credit or certification for using OCW.Made for sharing. Download files for later. Send to friends and colleagues.
Modify, remix, and reuse (just remember to cite OCW as the source.)Learn more at.