FORTRAN Help: Bisection Method & Roots of Functions

Click For Summary

Discussion Overview

The discussion focuses on the implementation of the Bisection Method in FORTRAN for finding the approximate roots of the Sine function over user-defined intervals. Participants are addressing issues related to the code logic, particularly within the loop structure and the conditions for convergence.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes their initial approach to the Bisection Method and notes issues with the output, including incorrect roots and infinite loops.
  • Another participant points out that the condition for convergence should focus on the function value f(m) approaching zero rather than the midpoint m itself.
  • A participant acknowledges a mistake in their initial logic and mentions modifications made to the code, including initializing m within the loop and testing endpoints.
  • Concerns are raised about the behavior of the program with negative intervals, where the order of inputs seems to affect the output, leading to confusion about the results.
  • A suggestion is made to manually trace through the code for problematic cases to verify the logic of endpoint selection in the Bisection Method.

Areas of Agreement / Disagreement

Participants express differing views on the correct implementation of the Bisection Method, particularly regarding the conditions for convergence and the handling of negative intervals. The discussion remains unresolved as participants explore these issues.

Contextual Notes

There are limitations noted regarding the initial testing of intervals to ensure they contain a root, as well as the potential for infinite loops if this is not addressed. The discussion also highlights the need for careful handling of function evaluations and the implications of input order on results.

mattmac.nuke
Messages
22
Reaction score
0

Homework Statement


The purpose of this program is to calculate the approximate roots of the Sine function on given intervals. The intervals are input by the user, and then the do loop continues until the condition (m becomes very close to 0 or equals 0) is met.


The Attempt at a Solution



program bisec
IMPLICIT NONE
REAL :: a, b, m, f_xa, f_xb, f_xm

WRITE (*,*) 'Please enter the interval [A,B]:'
READ (*,*) a,b

DO !WHILE (ABS(m) > 1E-7)
m = (a + b)/2.
f_xa = SIN(a)
f_xb = SIN(b)
f_xm = SIN(m)

IF (ABS(m) < 1E-5) THEN
EXIT
END IF

IF (f_xa*f_xm > 0) THEN
a= m
ELSE IF (f_xa*f_xm < 0) THEN
b= m
ELSE IF (f_xa*f_xm == 0) THEN
EXIT
END IF

END DO
WRITE (*,*) 'Solution is:',m

end program bisec

This is what I have so far, I've changed around my conditional statement to see if it would help, but it did not. The solutions it gives me are either 0 (on an interval that does not include 0) or radically large numbers, or it runs indefinitely. I know I am doing something wrong within the do loop. I've tried to follow the math correctly and translate it into code, but this is still a challenge for me as I am still in the early learning stages of programming. Thank you!
 
Physics news on Phys.org
What is the value of m the first time the DO condition is tested (that is, what is its value just prior to entering the DO loop for the first time?

Surely if you're looking for a root you want the value of the function f(m) to approach zero, not the value of m; m is simply the midpoint x-value, which takes on values in your search interval [a,b].

I note that you don't do any preliminary testing to see that the interval actually contains a zero. This may cause you grief (like an infinite loop for some pairs [a,b]).
 
I made some modifications last night, after I posted actually. You're right, I'm not sure why on Earth I was test m instead of f_xm for the condition, but I've fixed that. I wound up initializing m inside the do loop, so it's initial value is the result of the operations on A and B, and it updates through each iteration. I thought about testing end points too, the program seems to work fine for positive and negative values where one of the limits (A or B) is actually the solution. The problem I seem to be having with negative intervals though is that when I punch in evaluate from [-8,-7] it will display -7 as a solution, however if I enter it as [-7,-8] it will display -8 as the correct solution. When evaluating for positive values however, no matter what the order of the numbers, it displays the correct solution. Why does it not do this for negative values as well?
Here is my updated code:

program bisec
IMPLICIT NONE
REAL(10) :: a, b, m, f_xa, f_xb, f_xm, pi
INTEGER :: i, n
WRITE (*,*) 'LAB 5- Bisection Method'
WRITE (*,*) '-----------------------'
WRITE (*,*) ' '
WRITE (*,*) 'Please enter the interval [A,B]:'
READ (*,*) a,b
pi = 3.1415926

DO i = 0, 1000
m = (a + b)/2.
f_xa = SIN(pi*a/2.)
f_xb = SIN(pi*b/2.)
f_xm = SIN(pi*m/2.)

IF (f_xa*f_xm > 0) THEN
a= m
ELSE IF (f_xa*f_xm < 0) THEN
b= m
ELSE IF (f_xa*f_xm == 0) THEN
EXIT
END IF

IF (ABS(f_xm) < 0.000000000000001) EXIT

END DO
WRITE (*,'(A, F8.3)') 'The solution on this interval is: x =',m
end program bisec


I really appreciate the assistance, Thank you!
 
Why don't you play computer and step, by hand and hand calculator, through one or two loops for one of the 'problematical' cases? Or work from a sketch of sin(x), since you really only need to verify the conditional logic for the choice of which end-point to move to 'm'.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
Replies
4
Views
2K
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
11
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K