GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Nov-2004, 18:49
slim_pete slim_pete is offline
New Member
 
Join Date: Nov 2004
Posts: 1
slim_pete is on a distinguished road
Question

Root Finding


How 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  
Old 03-Nov-2004, 22:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by slim_pete
How 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!


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:
Heres the beginning of the loop: Let mid = (left + right) / 2 Calculate f(mid). If the absolute value is less than your tolerance, break out of the loop; you're done. Otherwise: Calculate f(left) * f(mid) If the result is less than 0, you know that there is a root between left and mid, so set right = mid for the next time through the loop If the result is greater than 0, you know that there is a root between mid and right, so set left = mid for the next time through the loop. End of the loop (go back and do it again)

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 GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 19:19.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.