Originally posted on here
The source code of bisection method.Here the equation x3 - 2x - 5 = 0 is solved by the method of bisection in FORTRAN language with CODE::BLOCKS.The equation x3 - 2x - 5 = 0 is solved here by the method of bisection in FORTRAN language with CODE::BLOCKS. We know the method of bisection can be expressed as the equal dividing system. Here we equally divide the length of the function from where the opposite points are in the opposite of the curve. The algorithm may be as bellow:
1. Take two points as the opposite sign of functional value..
2. Find the average of the points.
3. Get functional value on the average.
4. If functional value is equal to zero then the solution will be this average.
5. If the difference between the recent two points is less then the tolerance, The approximate solution is the average.
6. Take the average as the new point as condition.
7. Do from the step 2.
The programming source code is given bellow:
program bisection
implicit none
integer::itrtn,i
real::a,b,x,c,tol,f
print*, "Enter the initial values (a,b):"
read(*,*)a,b
if( (f(a)*f(b)) .gt.0) then
write(*,*)"Can not be done", f(a), f(b);
stop
end if
print*, "Enter the number of iteration:"
read(*,*)itrtn
print*, "Enter the tolerance:"
read(*,*)tol
do 10 i=1,itrtn
c = (a+b)/2
if(f(c).eq.0) then
write(*,*)"The soln is:",c
stop
end if
if(f(a)*f(c).lt.0) then
b = c
else
a = c
end if
if(abs(a-b).le.tol) then
write(*,*)"The soln is:",c
stop
end if
10 continue;
print*, "Maximum iteration failed"
end
real function f(x)
real::x
f = x**3 - 2*x -5
return
end function