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? -
#include <stdio.h>
-
#include <math.h>
-
double func1(double x);
-
double func1_d(double x);
-
double Newton_Raphson(double x0);
-
main(void)
-
{
-
double x, x_begin=-1.0, del_x=0.25, x_old, x0, root, f_x, f_x_old;
-
int k;
-
char sign_change;
-
printf("----------------------------------\n");
-
printf(" x f(x) sign change\n");
-
printf("----------------------------------\n");
-
x = x_begin;
-
f_x = func1(x);
-
printf("%8.2f %12.4f\n",x, f_x);
-
for (k=1; k<25; k++) {
-
x_old = x;
-
f_x_old = f_x;
-
sign_change = ' ';
-
x = x_begin + (double)k * del_x;
-
f_x = func1(x);
-
if(f_x*f_x_old <= 0.0) {
-
sign_change = 'Y';
-
printf("%8.2f %12.4f %c\n",x ,f_x ,sign_change);
-
x0 = 0.5 * (x + x_old);
-
root = Newton_Raphson(x0);
-
printf(" A refined root is %-12.6e\n",root);
-
}
-
else {
-
printf("%8.2f %12.4f %c\n",x ,f_x ,sign_change);
-
}
-
}
-
printf("\n");
-
-
exit(0);/* normal termination */
-
}
-
double func1(double x)
-
{
-
/* f(x)= x*x*x - x*x -9.0*x + 8.9 */
-
double f_x;
-
f_x = x * x * x - x * x - 9.0 * x + 8.9;
-
return f_x;
-
}
-
}
-
double func1_d(double x)
-
{
-
/* f'(x) = 3.0*x*x -2.0 * x - 9.0 */
-
double fd_x;
-
fd_x + 3.0 * x * x - 2.0 * x - 9.0;
-
return fd_x;
-
}
-
double Newton_Raphson(double x0)
-
{
-
int debug = 1;
-
double tolx, tolf, x1, del_x;
-
double f0, f1, f_d0;
-
f0 = func1(x0);
-
if(debug != 0) printf(" f(%g) = %e \n", x0, f0);
-
/* define tolerances */
-
tolx = 1.e-8 * fabs(x0); /* tolerance for |x1 - x0| */
-
tolf = 1.e-6 * fabs(f0); /* tolerance for |f(x1)| */
-
do{
-
f_d0 = func1_d(x0);
-
x1 = x0 - f0/f_d0;
-
f1 = func1(x1);
-
-
if(debug != 0) printf(" f(%g) = %e\n", x1, f1);
-
del_x = fabs(x1 - x0);
-
/* update x0 and f0 for the next iteration */
-
x0 = x1;
-
f0 = f1;
-
} while(del_x > tolx && fabs(f1) > tolf);
-
return x1;
-
}
Much, much thanks in advance.
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.
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.
This code - double func1_d(double x)
-
{
-
/* f'(x) = 3.0*x*x -2.0 * x - 9.0 */
-
double fd_x;
-
fd_x + 3.0 * x * x - 2.0 * x - 9.0;
-
return fd_x;
-
}
isn't doing anything. You aren't assigning any values to fd_x, just using it in an addition expression.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: mailpitches |
last post by:
Hello,
Is there any way to kill a Javascript infinite loop in Safari without
force-quitting the browser?
MP
|
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()
|
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...
|
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...
|
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...
|
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 =...
|
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"...
|
by: Vector |
last post by:
Is any infinite loop better than other? Is there any difference between
there efficiency?
|
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 .
|
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,...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |