Created
September 11, 2017 13:36
-
-
Save sidvishnoi/0c2390962971627ea1f3b856f014294d to your computer and use it in GitHub Desktop.
Bisection method with while loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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