![]() |
|
#1
|
|||
|
|||
Root FindingHow could I find the root of the following equation using the Bisection rule?
F(x) = (2-x)(e^(-x/4))-1 I suppose that I could take an initial guess of 0 or 1 and a tolerance of 0.1% Sample Output Iter# Xi Xf Xr F(Xr) 1 0 1 0.5 0.323475 2 0.5 1 0.75 0.036286 For those who don't know what is the Bisection rule: You have to note intervals where you think roots are to be found Then, note that the function is positive on one side and negative on the other side Cut the interval in two and get a new, smaller interval Repeat until you are close enough Parameters needed: Function pointer to the function to search An interval with specific sign properties Tolerance for terminating the search Algorithm: bisection(f,a,b,tolerance) midpoint = (a+b)/2; if ((midpoint – a) < tolerance) return midpoint; if (f(a) * f(midpoint) < 0) return bisection(f,a,midpoint,tolerance); if (f(b) * f(midpoint) < 0) return bisection(f,midpoint,b,tolerance); I am stuck and I would appreciate any help! Thanks! |
|||
|
#2
|
|||
|
|||
|
Quote:
It seems that you are trying to solve the problem recursively. I will try to show how you might do it with a loop. If you really want to use recursion, or if you have an assignment that requires recursion, you may be able to convert. I find that most people (well, myself, at least) have a hard time getting into a recursive frame of mind. I also find that recursive programs are often difficult to debug, whereas with a loop, you can simply put a few printf()'s here and there and know exactly how you got there and where you are going. I look at recursion as a way that computer science elitists separate themselves from the great unwashed hordes of us common folk just trying to get along in the world. Maybe that's the whole point of the assignment, and my efforts here may not contribute directly to your success, but here goes. Here's my take on the bisection method: Bisection method for finding a real root of a continuous function: Given a real-valued function of a real variable, say f(x), which is continuous on an interval [a,b]. Now suppose we know that f(a) * f(b) < 0 (that is, the function is positive for one and negative for the other). Then there is a value of x on the interval (a, b) such that f(x) = 0. Here is a way to find an approximate value of a zero of f(x) Find numbers "left" and "right" such that f(left) * f(right) < 0. (In general, you might want to make a program to do some kind of search for such values, but for now, assume we are given these to start.) For the given function, you can try 0 and 1. So you know that there is a root between f(left) and f(right). You might want to try the value halfway between them: mid = (left + right) / 2 Try it, you might get lucky. That is, see if f(mid) is zero. It could happen; you might nail it first off. If not, then compare the sign of f(mid) with one of the previous points, say f(left). The programattically elegant way to see if two values have opposite signs is to see if their product is negative. So, see if f(left) * f(mid) is less than zero. If it is, then we know there is a zero between left and mid. Otherwise, we know there is a zero between mid and right. Pick the appropriate one to be a new end point and repeat the process. Oh, yes, one other thing: Given the always-present possibility of roundoff errors in representing function coefficients and in calculating function values, it is (usually) not realistic to expect to find a number, x, for which f(x) is exactly zero in our computer calculations. So, typically we define a "tolerance", and we accept an approximate value of the zero of f(x) if the absolute value of f(x) is less than the tolerance. The tolerance might be, 1.0e-4, for example, meaning that we will accept a value of x for which the absolute value of f(x) is less than .0001. Here's the plan for a program (we do it in a loop). Given initial values of "left" and "right" for which f(left)*f(right) < 0, we find successive values that are closer together as follows: Code:
Regards, Dave p.s. Someone, I forget who, once said: "To iterate is human, to recurse is divine." I kind of like this one better: To err is human, but it feels divine. ---Mae West |
Recent GIDBlog
Toyota - 2009 May Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| IPv6 added by ICANN to Root Servers | 000 | Web Hosting Forum | 3 | 23-Jul-2004 05:04 |
| finding a simple cms | dopee | MySQL / PHP Forum | 1 | 23-Mar-2004 10:30 |
| Not finding POST variables | shrdlu | Apache Web Server Forum | 2 | 22-Jan-2004 19:50 |
| finding time efficiency of STL | goathens | C++ Forum | 1 | 28-Sep-2003 18:41 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The