module numerical
implicit none
real,parameter :: zero=0.00001
contains
real function bisect(A,B,func)
implicit none
real A,B !输入的值
real C !用来算(A+B)/2
real FA !记录F(A)
real FB !记录F(B)
real FC !记录F(C)
real,external :: func !求解的函数
C=(A+B)/2.0
FC=func(C)
do while(abs(fc)>zero)
FA=func(A)
FB=func(B)
if(FA*FC<0) then
B=C
C=(A+B)/2.0
else
A=C
C=(A+B)/2.0
end if
FC=func(c)
end do
bisect = C
return
end function
real function f(x)
implicit none
real x
f=(x+3)*(x-3)
return
end function
end module numerical
program main
use numerical
implicit none
real A,B !两个猜测值
real ANS !算出
do while(.true.)
write(*,*) '输入两个猜测值'
read(*,*) A,B
if(f(a)*f(b)<0)exit
write(*,*) "不正确的猜测值"
end do
ANS=bisect(A,B,f)
write(*,"('x=',F6.3)")ans
stop
end program main