program main
call findbracket(x0, a, b)
end program
function f(x)
double precision x,f
f = x
end function
subroutine findbracket(x0,a,b)
double precision x0, a, b
double precision fa, fb
double precision dx
dx = 0.001d0
x0 = 1.0d0
a = x0
b = x0
print*, a, b
print*, f(a)
print*, f(b)
do
fa = f(a)
fb = f(b)
print*, "what is fa", fa
print*, "what is fb", fb
a = a - dx
if (fa*fb < 0) then
exit
end if
print*, b, dx
b = b + dx
if (fa*fb < 0) then
exit
end if
dx = dx*2
end do
end subroutine
I'm writing a program that solves the root of a given function f(x). At this point, I just want to check if I'm getting right value for each step and I noticed some errors. To make it simple, I have f(x) = x as the function and I expected the program to print f(a) = 1.0 and f(b) = 1.0 but the output is f(a) = 1.8750000000000000 and f(b) = 0.0000000000000000. Plus although I set both a and b equal to x0, it seems to be a = 1.0000002381857485 and b = 1.0000000000000000. Could anyone explain why this is happening? I might be missing something stupid, but I cannot find it. I appreciate your help.