- #1

- 3,921

- 260

I'm having trouble with a very simple program. I want it to approximate a square root of a number R. So here is my program :

Program biseccion

implicit none

Real(8) :: R,a,b,Tol,d

Integer :: i

Write(*,*)'Enter R, a, b and Tol'

Read(*,*)R,a,b,Tol

i=0

d=(a+b)/2.

Do while (abs(f(d))>Tol)

d=(a+b)/2.

If (f(a)*f(d)>0) Then

a=d

Else

b=d

end if

i=i+1

Write(*,*)i,d,f(d)

End Do

Contains

Real Function f(x)

implicit none

Real(8) :: x, R

f=x**2-R

end function

end program

For example, if you enter R as 3, a as 1, b as 2 and Tol as 0.001, it should return " d " as the square root of we were looking for, "i" as the number of steps to reach it, and "f(d)" as the real value of the function f (obviously close to Tol). But it doesn't work. I've tried to find an error, but I don't see any. Maybe there is a problem in the function f... but I'm not sure.

I'd be grateful if you could run the program to see what happens, or if you any error. Thanks a lot.