Fortra 90 : Error in my program of bisection method

Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to the implementation of the bisection method for approximating the square root of a number in Fortran. Participants explore potential errors in the code, particularly focusing on the function definition and variable scope.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a Fortran program intended to approximate the square root of a number R using the bisection method and describes the issue of the program not functioning as expected.
  • Another participant suggests modifying the output to include more variables for debugging purposes and emphasizes the importance of efficiency by reducing function calls.
  • A different participant points out that declaring R within the function creates a new variable that prevents access to the R defined in the main program, which could be the source of the problem.
  • The original poster acknowledges the advice and confirms that the program works correctly after making the suggested changes.

Areas of Agreement / Disagreement

Participants generally agree on the identification of the variable scope issue as a source of the problem, and the original poster confirms that the suggested solution resolved the issue. However, the initial misunderstanding of the error indicates some uncertainty in the debugging process.

Contextual Notes

The discussion highlights the importance of variable scope in programming and the potential for confusion when similar variable names are used in different contexts. Specific details about the iterations and function behavior are noted but not fully resolved.

Who May Find This Useful

This discussion may be useful for programmers working with Fortran, particularly those implementing numerical methods like the bisection method and encountering similar variable scope issues.

fluidistic
Gold Member
Messages
3,934
Reaction score
286
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 25 ·
Replies
25
Views
4K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K