What's causing my Fortran bisection method to give incorrect results?

In summary, the conversation was about a programming problem where the person was trying to solve for a temperature in thermodynamics using a transcendental equation. The code they wrote was not working and they were unsure of what the error could be. After some troubleshooting, it was discovered that the function in the code was not correctly using the values for t_1 and t_2. Once the function was updated to include these values as arguments, the problem was solved.
  • #1
fluidistic
Gold Member
3,923
261
I'm rusty with fortran and programming in general. I can't see my "error" in a code that I wrote from scratch.
Basically I wanted to get some fun and solve for a temperature in thermodynamics where I must get "T_f" which appear in a transcendental equation: ##A\ln \left ( \frac{T_f^2}{T_1T_2} \right )+2B[2T_f-(T_1T_2)]=0##.
Where T_1 is worth 100, T_2 is worth 200, A=2 and B=0.005.
Here is my code:
Code:
program rootfinderbisection
implicit none

real(4)::a,b,xn,fa,fb,fn,f,e,x_n,t_1,t_2


write(*,*)"Enter the temperatures t_1 and t_2"
read(*,*)t_1, t_2
write(*,*)"Enter your interval guess, [a,b]"
read(*,*)a,b
write(*,*)"Enter the precision you want"
read(*,*)e
fa=f(a)
fb=f(b)

if (abs(fa)<e) then
write(*,*) "The solution is",a
stop
else if (abs(fb)<e) then
write(*,*)"The solution is",b
stop
end if

if (f(a)*f(b)>0) then
write(*,*)"There's an even number of root(s) in that interval"
stop
endif

xn=0.5*(a+b)
fn=f(xn)

do while (abs(fn)>e) 
if (fa*fn<0) then
b=xn
fb=fn
else
a=xn
fa=fn
endif

xn=0.5*(a+b)
fn=f(xn)
end do
write(*,*)"The solution is",x_n


end program
that I compile along with
Code:
function f(x)
implicit none

real(4)::f,x, t_1,t_2
f=2.*log((x**2.)/(t_1*t_2))+2.*0.005*(2.*x-(t_1+t_2))

end function
The problem is that for the values I said above, I get the message "There's an even number of root(s) in that interval". Thus apparently the condition f(a)*f(b)>0 is satisfied. However when I enter the numbers in my pocket calculator I get f(a)=-2.3862... and f(b)=2.3862... where I truncated both values. Thus the condition is not satisfied.
I really don't see what's wrong. Any idea?

Edit: I tried with many different values for a and b. I even changed t_1 and t_2, same message.

Edit2: I've just tested the values of fa and fb. It thinks they are worth infinity. Going to check this out.
Edit3: No idea why it thinks they are worth infinity. o_0.
Edit4: ok it does not plug in t_1 and t_2 inside my function.
 
Last edited:
Technology news on Phys.org
  • #2
Try including t_1 and t_2 as arguments in the function f and see if this resolves the calculation errors.
 
  • #3
Your function "f(x)" does not agree with your formula at the top of your post. Is the last term in the bracket term of your equation "T_1 * T_2" or "T_1 + T_2".
 
  • #4
fluidistic said:
Edit4: ok it does not plug in t_1 and t_2 inside my function.

t_1 and t_2 in the function are completely separate variables from t_1 and t_2 in the main program. If you want t_1 and t_2 in the function to have the same values as in main, you need to make them arguments to the function along with x.
 
  • #5
Thanks all for the tips. To TheoMcCloskey, you're right, I made a latex typo here for T_f. The one in my program is ok.
So it got rid of the problem! However I now get "The solution is 3.02481484E-39" which is basically 0. I'll investigate and post if stuck.

Edit: Worked, problem solved. I had to change the line "write(*,*)"The solution is",x_n" to "write(*,*)"The solution is",xn".
Thanks a lot guys!
 
Last edited:

Related to What's causing my Fortran bisection method to give incorrect results?

1. What is the Fortran bisection method?

The Fortran bisection method is a numerical algorithm used for finding the roots of a nonlinear equation. It works by repeatedly bisecting an interval in which the root is known to exist, until the root is found to within a desired level of accuracy.

2. How does the Fortran bisection method work?

The Fortran bisection method works by first defining an interval [a,b] in which the root is known to exist. It then repeatedly bisects this interval by finding the midpoint c = (a+b)/2 and checking if the sign of the function at c is different from the sign at either a or b. If it is, then the root must lie in the subinterval [a,c] or [c,b], and the process is repeated until the root is found to within a desired level of accuracy.

3. What are the advantages of using the Fortran bisection method?

One advantage of using the Fortran bisection method is that it is a very simple and robust algorithm that can be easily implemented in code. It also guarantees convergence to a root as long as the function is continuous and the initial interval is chosen carefully. Additionally, it can handle a wide range of functions, including those that are not differentiable or have multiple roots.

4. What are the limitations of the Fortran bisection method?

One limitation of the Fortran bisection method is that it can be relatively slow compared to other root-finding algorithms, especially for functions with multiple roots or roots that are close together. It also requires an initial interval in which the root is known to exist, which may not always be easy to determine. Additionally, it may not work well for functions that are highly oscillatory or have steep changes in slope.

5. How does the accuracy of the Fortran bisection method compare to other root-finding methods?

The Fortran bisection method is generally less accurate than some other root-finding methods, such as Newton's method or the secant method. This is because it requires a larger number of iterations to converge to a root. However, it is more robust and can handle a wider range of functions, so it may be a better choice in certain situations where accuracy is not the primary concern.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
12
Views
972
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
13
Views
2K
Replies
1
Views
9K
  • Differential Equations
Replies
1
Views
790
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top