Skip to content

Instantly share code, notes, and snippets.

@sidvishnoi
Created September 11, 2017 13:36
Show Gist options
  • Save sidvishnoi/0c2390962971627ea1f3b856f014294d to your computer and use it in GitHub Desktop.
Save sidvishnoi/0c2390962971627ea1f3b856f014294d to your computer and use it in GitHub Desktop.
Bisection method with while loop
program bisection
REAL a, b, c, TOL
INTEGER NMAX, N
WRITE(*, *) "Enter a and b"
READ(*, *) a, b
NMAX = 20
N = 1
TOL = 1.0e-04
do while ( N .lt. NMAX )
c = (a + b)/2
PRINT *, "N = ", N, " c = ", c
if ((b-a)/2 .lt. TOL) then
! ROOT FOUND = c
goto 2
endif
N = N + 1
if ((f(c) .lt. 0) .eqv. (f(a) .lt. 0)) then ! new interval
a = c
else
b = c
endif
enddo
1 PRINT *, "OUT OF ITERATION"
2 PRINT *, "RESULT = ", c
end program bisection
REAL function f(x)
REAL x
f = x*x*x - x - 2
end function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment