# Help!

This is a discussion on Help! within the C Programming forums, part of the General Programming Boards category; Can someone please tell me why the following program results in an infinite loop? I have to use an numerical ...

1. ## Help!

Can someone please tell me why the following program results in an infinite loop? I have to use an numerical analysis method called Newton's method to determine the root of the function, and stop when the error estimated is < 10^-6.

I'd appreciate any input anyone has.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

void main()
{
double x1; /* variable for xn+1 */
double x0=2; /* initialize x0 to 2*/
double f;
double fprime;
double error;
double condition;

condition=pow(10,-6);/* difference between x0 and x1 should be 10^-6*/

f=(double)(x0*x0*x0)-(double)(2*x0)-(double)5; /* function f(x0)*/
fprime=x0*x0-2; /* derivative f'(x0) */

x1=x0-(f/fprime); /* calculation of x1 using Newton's method */

error=x1-x0; /* error between x0 and x1 */

if(error<0)
error=error*-1; /* if error is negative, change to positive */

while(error>condition)
{
x0=x1;

f=(double)(x0*x0*x0)-(double)(2*x0)-(double)5; /* calculates f(x0) */
fprime=x0*x0-2;

x1=x0-(f/fprime); /* calculate new x1 */

printf("x=%lf\n",x1); /* print value of x1 */
error=x1-x0; /* recalculate error */

if(error<0)
error=error*-1;
}
}

2. Nevermind. I had the derivative entered wrong.

Thanks anyway!