Program For Bisection Method In Fortran
I'm trying to implement Bisection Method with Fortran 90 to get solution, accurate to within 10^-5 for 3x - e^x = 0 for 1 <= x <= 2
This is the code that I came up with, but when I run the code it just list .1.5000000000000000 100 times.
How should I fix this code so that I can keep applying bisection method correctly until I get to a number around 10^-5?
Dec 28, 2017 - Program bisection implicit none real:: a,b,c,error,f error = 1.0e-06 write( *, *) 'Enter two numbers a and b simultaneously between which the root. I'm trying to implement Bisection Method with Fortran 90 to get solution, accurate to within 10^-5 for 3x - e^x = 0 for 1. C++ Programming - Program for Bisection Method - Mathematical Algorithms - The method is also called the interval halving method, the binary search method.
1 Answer
The problem is actually not with your algorithm, but rather in how you calculate f
. Because you have not specified implicit none
in the function, the compiler allowed e**x
to slip through, even though Fortran doesn't define e
as you would have liked.
When you correct the function as follows, the program works fine:
This is a good lesson to use implicit none everywhere.
Not the answer you're looking for? Browse other questions tagged fortranbisection or ask your own question.
Bisection Method Algorithm
The purpose of this program is to calculate the approximate roots of the Sine function on given intervals. The intervals are input by the user, and then the do loop continues until the condition (m becomes very close to 0 or equals 0) is met.
3. The attempt at a solution
program bisec
IMPLICIT NONE
REAL :: a, b, m, f_xa, f_xb, f_xm
WRITE (*,*) 'Please enter the interval [A,B]:'
READ (*,*) a,b
DO !WHILE (ABS(m) > 1E-7)
m = (a + b)/2.
f_xa = SIN(a)
f_xb = SIN(b)
f_xm = SIN(m)
IF (ABS(m) < 1E-5) THEN
EXIT
END IF
IF (f_xa*f_xm > 0) THEN
a= m
ELSE IF (f_xa*f_xm < 0) THEN
b= m
ELSE IF (f_xa*f_xm 0) THEN
EXIT
END IF
END DO
WRITE (*,*) 'Solution is:',m
end program bisec
This is what I have so far, I've changed around my conditional statement to see if it would help, but it did not. The solutions it gives me are either 0 (on an interval that does not include 0) or radically large numbers, or it runs indefinitely. I know I am doing something wrong within the do loop. I've tried to follow the math correctly and translate it into code, but this is still a challenge for me as I am still in the early learning stages of programming. Thank you!