Fortra 90 : Error in my program of bisection method

AI Thread Summary
The discussion revolves around troubleshooting a simple program designed to approximate the square root of a number using the bisection method. The user initially encounters issues with the program not functioning as expected, particularly with the output values for the square root approximation, the number of iterations, and the function's evaluation. The problem is identified as being related to the declaration of the variable R within the function f(x). By declaring R inside the function, it created a local variable that prevented access to the R defined in the main program. Once this issue was addressed by removing the declaration of R in the function, the program began to work correctly, providing the expected results. The discussion highlights the importance of variable scope in programming and its impact on function behavior.
fluidistic
Gold Member
Messages
3,928
Reaction score
272
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'
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.
 
Technology news on Phys.org
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).

And please don't call f(x) each time. Think about efficiency.
 
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
1
Views
2K
Replies
12
Views
1K
Replies
11
Views
2K
Replies
25
Views
3K
Replies
11
Views
2K
Replies
8
Views
2K
Back
Top