FORTRAN Help: Bisection Method & Roots of Functions

In summary, the program calculates 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.
  • #1
mattmac.nuke
22
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
  • #2
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]).
 
  • #3
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!
 
  • #4
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'.
 
  • #5


Hello, it seems like you are on the right track with your code. However, there are a few things that could be improved.

Firstly, it seems like you are using the bisection method to find the roots of the Sine function. In that case, you should be checking for the sign of f_xm (not m) in your conditional statements.

Secondly, you are using the variable m to represent the midpoint of the interval, which is fine. However, in order to check if you have reached a root, you should be checking the value of the function at that midpoint (f_xm), not the value of the midpoint itself.

Lastly, in your do loop, you are using the condition (ABS(m) < 1E-5) as your exit condition. This means that your do loop will continue until the value of m is less than 1E-5, which may or may not be close enough to the actual root. Instead, you could use a different condition, such as (ABS(f_xm) < 1E-5), which would check if the value of the function at the midpoint is close enough to 0.

I hope this helps in improving your code. Keep up the good work!
 

Related to FORTRAN Help: Bisection Method & Roots of Functions

What is the Bisection Method?

The Bisection Method is a numerical method used to find the roots of a function. It involves dividing the interval in which the root is located into smaller intervals, and then narrowing down the interval until the root is found.

How does the Bisection Method work?

The Bisection Method works by evaluating the function at two points within an interval, and then determining which subinterval the root lies in. This process is repeated until the interval containing the root is narrowed down to a desired level of accuracy.

What are the advantages of using the Bisection Method?

The Bisection Method is relatively simple to implement and understand, making it a good choice for those new to numerical methods. It also guarantees convergence to a root, as long as the function is continuous and changes sign within the interval.

What are the limitations of the Bisection Method?

One limitation of the Bisection Method is that it can be slow to converge, especially for functions with multiple roots. It also requires the initial interval to be chosen carefully in order to ensure convergence to the desired root.

How can FORTRAN help with implementing the Bisection Method?

FORTAN has built-in functions and subroutines that can assist with implementing the Bisection Method, such as the BISCTN subroutine. These can save time and effort in coding the method from scratch, and can also handle special cases like when the function has multiple roots.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Back
Top