FORTRAN 90+: PROGRAM EXAMPLE
It is recommended to look at the numerical methods codes where programming is demonstrated. An additional example is included here.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! Uses bisection method to find square root ! ! x*x - c = 0 ! over the interval [a,b] ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! PROGRAM sqrt IMPLICIT NONE INTEGER :: nmax, n REAL :: tol, d REAL :: a, b, c REAL :: fa, fc WRITE(*,*) ’ Input Constant d (x^2-d=0) ’ READ(*,*) d WRITE(*,*) ’ Input Lower and Upper Limits’ READ(*,*) a, b tol = 0.000001 nmax = 100 fa = f(a,d) c = (a + b ) /2. fc = f(c,d) n = 1 DO WHILE (n <= nmax) IF((b-a)/2.0 < tol) THEN WRITE(*,*) ’Root’,c,’gives’,fc,’after’,n,’iterations’ EXIT ENDIF IF(fa*fc > 0.0) THEN a = c ELSE b = c ENDIF c = (a + b ) /2. fc = f(c,d) n = n + 1 ENDDO CONTAINS REAL FUNCTION F(x,n) REAL :: x REAL :: n f = x*x - n END FUNCTION F END PROGRAM