Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortra 90 : Error in my program of bisection method

  1. Jun 4, 2008 #1

    fluidistic

    User Avatar
    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 :
    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.
     
  2. jcsd
  3. Jun 4, 2008 #2

    Borek

    User Avatar

    Staff: Mentor

    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.
     
  4. Jun 4, 2008 #3

    fluidistic

    User Avatar
    Gold Member

    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.
     
  5. Jun 4, 2008 #4

    alphysicist

    User Avatar
    Homework Helper

    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.
     
  6. Jun 4, 2008 #5

    fluidistic

    User Avatar
    Gold Member

    Thanks once again alphysicist. It now works as it should. The error wasn't obvious for me.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook