473,385 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Infinite loop problem in Newton-Raphson program

I wrote this program to calculate f(x) and I'm running into a problem that is causing an infinite loop. The output is fine until the first time that the if-loop catches f_x <= 0.0. I've been looking endlessly for what might be causing it to no avail. Maybe fresher eyes can help. Anyone?

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. double func1(double x);
  4. double func1_d(double x);
  5. double Newton_Raphson(double x0);
  6. main(void)
  7. {
  8.         double x, x_begin=-1.0, del_x=0.25, x_old, x0, root, f_x, f_x_old;
  9.         int k;
  10.         char sign_change;
  11.         printf("----------------------------------\n");
  12.         printf("      x         f(x)   sign change\n");
  13.         printf("----------------------------------\n");
  14.         x = x_begin;
  15.         f_x = func1(x);
  16.                 printf("%8.2f %12.4f\n",x, f_x);
  17.         for (k=1; k<25; k++) {
  18.                 x_old = x;
  19.                 f_x_old = f_x;
  20.                 sign_change = ' ';
  21.                x = x_begin + (double)k * del_x;
  22.                 f_x = func1(x);
  23.                 if(f_x*f_x_old <= 0.0) {
  24.                         sign_change = 'Y';
  25.                         printf("%8.2f %12.4f    %c\n",x ,f_x ,sign_change);
  26.                         x0 = 0.5 * (x + x_old);
  27.                         root = Newton_Raphson(x0);
  28.                         printf("     A refined root is %-12.6e\n",root);
  29.                 }
  30.                 else {
  31.                         printf("%8.2f %12.4f    %c\n",x ,f_x ,sign_change);
  32.                 }
  33.         }
  34.         printf("\n");
  35.  
  36.         exit(0);/* normal termination */
  37. }
  38. double func1(double x)
  39. {
  40. /* f(x)= x*x*x - x*x -9.0*x + 8.9 */
  41.         double f_x;
  42.         f_x = x * x * x - x * x - 9.0 * x + 8.9;
  43.         return f_x;
  44. }
  45. }
  46. double func1_d(double x)
  47. {
  48. /* f'(x) = 3.0*x*x -2.0 * x - 9.0 */
  49.         double fd_x;
  50.         fd_x + 3.0 * x * x - 2.0 * x - 9.0;
  51.         return fd_x;
  52. }
  53. double Newton_Raphson(double x0)
  54. {
  55.         int debug = 1;
  56.         double tolx, tolf, x1, del_x;
  57.         double f0, f1, f_d0;
  58.         f0 = func1(x0);
  59.         if(debug != 0) printf("     f(%g) = %e \n", x0, f0);
  60. /* define tolerances */
  61.         tolx = 1.e-8 * fabs(x0); /* tolerance for |x1 - x0| */
  62.         tolf = 1.e-6 * fabs(f0); /* tolerance for |f(x1)| */
  63.         do{
  64.                 f_d0 = func1_d(x0);
  65.                 x1 = x0 - f0/f_d0;
  66.                 f1 = func1(x1);
  67.  
  68.                 if(debug != 0) printf("     f(%g) = %e\n", x1, f1);
  69.                 del_x = fabs(x1 - x0);
  70. /* update x0 and f0 for the next iteration */
  71.                 x0 = x1;
  72.                 f0 = f1;
  73.         } while(del_x > tolx && fabs(f1) > tolf);
  74.         return x1;
  75. }
Much, much thanks in advance.
Oct 30 '06 #1
3 3054
Some more information. Here is my output:


----------------------------------
x f(x) sign change
----------------------------------
-1.00 15.9000
-0.75 14.6656
-0.50 13.0250
-0.25 11.0719
0.00 8.9000
0.25 6.6031
0.50 4.2750
0.75 2.0094
1.00 -0.1000 Y
f(0.875) = 9.292969e-01
f(-0.125) = 1.000742e+01
f(-1.125) = 1.633555e+01
f(-2.125) = 1.391367e+01
f(-3.125) = -3.258203e+00
f(-4.125) = -4.118008e+01
f(-5.125) = -1.058520e+02
...

When it should be:

----------------------------------
x f(x) sign change
----------------------------------
-1.00 15.9000
-0.75 14.6656
-0.50 13.0250
-0.25 11.0719
0.00 8.9000
0.25 6.6031
0.50 4.2750
0.75 2.0094
1.00 -0.1000 Y
f(0.875) = 9.292969e-01
f(0.984935) = 2.096803e-02
f(0.987537) = 1.324866e-05
f(0.987539) = 5.316636e-12
A refined root is 9.875386e-01
1.25 -1.9594
1.50 -3.4750
1.75 -4.5531
.... .......

So it looks like the code is working as it should, but something is making x1 in Newton_Raphson calculate incorrectly. Still not sure what it is, though.
Oct 30 '06 #2
You are most probably runing into Oscilation problem. It happens when the solution fluctuate around a local maximum or minimum.
Try some other initial values for your independent variable. This might resolve the issue but there is no guarantee.
May 4 '07 #3
Ganon11
3,652 Expert 2GB
This code

Expand|Select|Wrap|Line Numbers
  1. double func1_d(double x)
  2. {
  3. /* f'(x) = 3.0*x*x -2.0 * x - 9.0 */
  4.         double fd_x;
  5.         fd_x + 3.0 * x * x - 2.0 * x - 9.0;
  6.         return fd_x;
  7. }
isn't doing anything. You aren't assigning any values to fd_x, just using it in an addition expression.
May 4 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: mailpitches | last post by:
Hello, Is there any way to kill a Javascript infinite loop in Safari without force-quitting the browser? MP
4
by: LOPEZ GARCIA DE LOMANA, ADRIAN | last post by:
Hi all, I have a question with some code I'm writting: def main(): if option == 1: function_a()
2
by: Newton | last post by:
I'm having some issues with a greasemonkey script i've made (nothing serious, and you'll notice that by the mess). I made it to learn javascript properly at first but it ended handy and now i'm...
1
by: Jim P. | last post by:
I'm having trouble returning an object from an AsyncCallback called inside a threaded infinite loop. I'm working on a Peer2Peer app that uses an AsyncCallback to rerieve the data from the remote...
1
by: SJ | last post by:
I'm developing a WAP client which presently works fine on most mobile phone browsers, but gives me an Infinite loop error (error 1025) when i try to access it from a couple of phones(motorola for...
5
by: Allerdyce.John | last post by:
Hi, I have this piece of code which loops thru a STL list, but that causs an infinite loop. bool Executer::group(MyList& bl, ResultList & grl) { for (ExecuterList::iterator i =...
11
by: jojobar | last post by:
I have a aspx file (snippet shown below): ======= <td class="light-m1" id="rwCompleteButton" runat="server"><br/> <asp:ImageButton CssClass="clear-m1" runat="server" CommandName="Complete"...
13
by: Vector | last post by:
Is any infinite loop better than other? Is there any difference between there efficiency?
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.