| New Reply |
Bisection method in fortran 90 |
Share Thread |
| Dec14-12, 03:47 AM | #1 |
|
Blog Entries: 1
|
Bisection method in fortran 90
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 Here are the Bisection Method formulas xm = (xl+xu)/2
|
| Dec14-12, 04:13 AM | #2 |
|
Recognitions:
|
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. |
| Dec14-12, 04:15 AM | #3 |
|
Blog Entries: 1
|
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 |
| Dec14-12, 04:26 AM | #4 |
|
Recognitions:
|
Bisection method in fortran 90I'm surprised that the program even compiled without the "end if" statements. (BTW. does it actually compile?) |
| Dec14-12, 04:28 AM | #5 |
|
Blog Entries: 1
|
Can you please please help me? How can I put the conditions above and do the iteration? Please? thanks.
Yes it did compile. |
| Dec14-12, 06:51 AM | #6 |
|
Recognitions:
|
I'll look at one section: Code:
if fxnew<0 then xl=xl xnew=xu |
| Dec14-12, 08:52 AM | #7 |
|
|
... and don't forget to correct your inital conditions.
|
| Dec14-12, 08:55 AM | #8 |
|
Blog Entries: 1
|
Ow. So it should be
xl=xl xu=xnew Correct? Then? What should I do? |
| Dec14-12, 09:13 AM | #9 |
|
Recognitions:
|
|
| Dec14-12, 10:16 AM | #10 |
|
Recognitions:
|
xl = xl is redundant
|
| Dec14-12, 07:53 PM | #11 |
|
Blog Entries: 1
|
just give me one example and i'll do it for the rest of the statements. Thanks. I really don't have any idea. |
| Dec15-12, 12:25 PM | #12 |
|
Mentor
|
You're doing the same thing later in your program where you set xu = xu. What is your purpose in doing this? |
| Dec15-12, 05:30 PM | #14 |
|
Mentor
|
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. |
| Dec15-12, 05:45 PM | #15 |
|
Blog Entries: 1
|
Yes, I understand that,, I just dont know how to put it in codes. i'll work on all your hints now. Thanks :)
|
| Dec15-12, 08:01 PM | #16 |
|
Mentor
Blog Entries: 9
|
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. |
| New Reply |
Similar discussions for: Bisection method in fortran 90
|
||||
| Thread | Forum | Replies | ||
| Bisection method in c++ | Engineering, Comp Sci, & Technology Homework | 2 | ||
| FORTRAN Help: Bisection Method & Roots of Functions | Engineering, Comp Sci, & Technology Homework | 3 | ||
| Differential equations, euler's method and bisection method | Calculus & Beyond Homework | 3 | ||
| bisection method by c | Programming & Comp Sci | 10 | ||
| Bisection Method | Programming & Comp Sci | 2 | ||