# Fortra 90 : Error in my program of bisection method

• fluidistic
In summary, the programmer was having trouble with a very simple program that involved approximating a square root of a number. They wrote a program that contained an implicit real function and an integer to keep track of how many steps it took to find the square root. They tried to run the program, but it wouldn't work because they had forgotten to declare a variable that was specific to the function. After declaring the variable, the program worked properly.

#### fluidistic

Gold Member
Hello,
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'

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.

I believe the reason is obvious, and should be pretty easy to identify just by looking at the output. Try Write(*,*)i,d,a,b,f(d).

I just tried "Write(*,*)i,d,a,b,f(d)", in one example a wasn't changing and b got b^2, b^4, etc. In another example I got about 3000 iterations were a and b didn't change, and so did d. And I always considered a small interval containing the root. I still don't see the error. Now I guess it's in the do while.

Hi fluidistic,

By declaring R in your function, you have set up a new variable R that is specific to that function, and therefore you have made the function unable to access the R in the main program.

If you don't declare it in the function I believe it will work properly.

Thanks once again alphysicist. It now works as it should. The error wasn't obvious for me.

## 1. What is Fortra 90?

Fortra 90 is a high-level programming language commonly used in scientific and engineering applications. It is an extension of the original Fortran language and offers many new features and improvements.

## 2. What is the bisection method?

The bisection method is a numerical algorithm used to find the root of a continuous function. It involves repeatedly dividing the interval in which the root lies in half and checking which half the root is in until it is found to a desired accuracy.

## 3. What does the error in my program of bisection method mean?

The error in your program of bisection method refers to any mistake or incorrectness in the code that is preventing it from running correctly. This could be due to syntax errors, logical errors, or incorrect algorithm implementation.

## 4. How can I fix the error in my program of bisection method?

To fix the error in your program of bisection method, you will need to carefully review your code and identify the source of the error. This may involve debugging and testing different parts of the code to determine where the mistake is occurring. Once the error is identified, you can make the necessary changes to fix it.

## 5. Are there any common mistakes made in implementing the bisection method algorithm?

Yes, there are some common mistakes that can occur when implementing the bisection method algorithm. These include incorrect use of loops, incorrect use of conditional statements, and errors in the mathematical calculations. It is important to thoroughly test and debug your code to ensure it is correctly implementing the algorithm.