Bisection method in fortran 90

In summary, the equation x3−2x−2 = 0 which has a single root between x=−4 and x = 2 can be solved using bisection method. The code I have includes the formulas for xm, xL, xU, and fxb, fnew. If xnew<0 then xl=xl, xnew=xu, go to 1, if xnew>0 then xnew=xl, xu=xu, go to 1. For xL, if fxnew<0 then xl=xl, xnew=xu, go to 1, if fxnew>0 then xnew=xl, xu=xu
  • #1
jhosamelly
128
0
Bisection method for the equation x3−2x−2 = 0 which has a single root
between x=−4 and x = 2.

here's the code I have

Code:
program bisection2
implicit none
real :: fxa, xnew, xu, xl, fxb, fnew

 
xu=4
xl=2

1 xnew=(xu+xl)/2
fxa=(xnew**3-(2*xnew)-2)
fxb=(xl**3-(2*xl)-2)

fnew=fxa*fxb

if fxnew<0 then
xl=xl
xnew=xu
go to 1

if fxnew>0 then
xnew=xl
xu=xu
go to 1


print *, "fx(a) = ", fxa
print *, "fx(b) = ", fxb


end program bisection2

I know there's something wrong with my code. Can somebody help me please?

Here are the Bisection Method formulas

xm = (xl+xu)/2

n2xrmu.jpg
 
Technology news on Phys.org
  • #2
The code at the line starting: "if fxnew>0 then" can never execute. Take a close look and see if you can tell me why?

A couple of other points.
1. Even if you fix the above problem, your code may never terminate. Requiring exact (FP) equality with zero is a bad idea.
2. There's no real reason to use to goto in this code. You should look at alternative methods of program flow control.
3. It will really help you keep track of your code if you start making use of indentation.
 
Last edited:
  • #3
I'm guessing coz there is no "end if"?

I don't know. sorry.. How can I do the iteration here?

Thanks for the help
 
  • #4
jhosamelly said:
I'm guessing coz there is no "end if"?

Yes, unless you use "end if" then the statement "if fxnew>0 then" is still under the control if the previous "if".

I'm surprised that the program even compiled without the "end if" statements. (BTW. does it actually compile?)
 
  • #5
Can you please please help me? How can I put the conditions above and do the iteration? Please? thanks.

Yes it did compile.
 
  • #6
jhosamelly said:
Can you please please help me? How can I put the conditions above and do the iteration? Please? thanks.

Yes there's still quite a few thing wrong with code, but you know we can't write your program for you. :)

I'll look at one section:
Code:
if fxnew<0 then
xl=xl
xnew=xu

You're kind of got the right idea. This section is executed if xnew is opposite sign to xlow, so you now know the zero crossing is between xlow and xnew. Now you want to let xnew become the new xhigh, and keep xlow as is. Can you see that of your two assignment statements, one is redundant and the other is the wrong way around.
 
Last edited:
  • #7
... and don't forget to correct your inital conditions.
 
  • #8
Ow. So it should be

xl=xl
xu=xnew

Correct? Then? What should I do?
 
  • #9
jhosamelly said:
O
Correct? Then? What should I do?

Take all the advice and hints you've been given so far then sit down and write a new iteration of your program.
 
  • #10
xl = xl is redundant
 
  • #11
SteamKing said:
xl = xl is redundant

hmmm.. yes. But how can I put that xl=xl and use it on the equation again in program?

just give me one example and i'll do it for the rest of the statements. Thanks. I really don't have any idea.
 
  • #12
jhosamelly said:
hmmm.. yes. But how can I put that xl=xl and use it on the equation again in program?
It's silly and a waste of time to set a variable to its own value.

You're doing the same thing later in your program where you set xu = xu.

What is your purpose in doing this?
jhosamelly said:
just give me one example and i'll do it for the rest of the statements. Thanks. I really don't have any idea.
 
  • #13
I need to put in codes the conditions given.
 
  • #14
jhosamelly said:
Here are the Bisection Method formulas

xm = (xl+xu)/2

n2xrmu.jpg

I'm not convinced that you understand what the above means.

xL - Lower (left) endpoint of an interval
xM - Midpoint of an interval
xU - Upper (right) endpoint of an interval

a) If f(xL)*f(xM) < 0, the graph of the function crosses the x-axis somewhere between xL and xM, so the root you're looking for must be in the left half of the original interval. If so, USE THE SAME VALUE FOR xL (i.e., don't change xL) but reset xU to xM. Your code should NOT include xL = xL.

b) If f(xL)*f(xM) > 0, the graph of the function does not cross the x-axis between xL and xM, so we should look in the other half of the interval - in [xM, xU]. If so, USE THE SAME VALUE FOR xU (i.e., don't change xU), but reset xL to xM. Your code should NOT include xU = xU.

At each step for a) or b), we are shortening the interval by half its length, so that we eventually find the root.

c) If f(xL)*f(xM) = 0 then either f(xL) = 0 or f(xM). There's probably an assumption that f(xL) ≠ 0 and f(xU) ≠ 0, but you didn't show it in the attachment you posted.
 
  • #15
Yes, I understand that,, I just don't know how to put it in codes. i'll work on all your hints now. Thanks :)
 
  • #16
Perhaps you could work through several iterations by hand. Once you get an understanding of how the algorithm works you can then write the code.

Step 1. Is there a root on the interval?
Step 2. Find the midpoint.
step 3. Find the half that has a root.
Step 4. Repeat.
 
  • #17
I did that already. I am not getting the same answer with the the program so I know there's something wrong. Thanks for the help guys. Appreciate it.
 
  • #18
jhosamelly said:
I did that already. I am not getting the same answer with the the program so I know there's something wrong. Thanks for the help guys. Appreciate it.

What answer are you getting. Is you program even terminating? As it's written, I wouldn't have though it would do.
 

1. What is the Bisection method in Fortran 90?

The Bisection method is a numerical algorithm used to find the roots of a continuous function. It involves dividing the search interval in half and checking which half contains the root, repeating this process until the root is approximated to the desired accuracy.

2. How does the Bisection method work in Fortran 90?

The Bisection method works by first defining an initial search interval, which contains the root of the function. The algorithm then divides this interval in half and checks which half contains the root. This process is repeated until the root is approximated to the desired accuracy.

3. What are the advantages of using the Bisection method in Fortran 90?

The Bisection method is relatively simple to implement and does not require any derivative information of the function. It also guarantees convergence to the root as long as the function is continuous and the initial search interval contains the root.

4. What are the limitations of using the Bisection method in Fortran 90?

The Bisection method can be slow to converge compared to other root-finding methods. It also requires the function to be continuous and the initial search interval to contain the root, which may not always be known.

5. Can the Bisection method be used for finding multiple roots of a function?

No, the Bisection method can only find one root of a function within a given interval. To find multiple roots, the algorithm must be applied to different intervals that contain each root.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
2
Views
929
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
4
Views
587
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top