I need to make use the Newton-Raphson function to solve for this.

Everything seems to be working perfectly, except I get "NaN" instead of numerical values after I compile. I figured out NaN stands for Not a Number, meaning my calculations had some weird divide-by-0-type stuff in it. I can't find a problem anywhere. The only place where I have a division sign is in my tolerances section, and I already tried erasing it from my code and I still got the same error.

My starting values:

f(x) = sin(x) +2x +1

d'f(x) = cos(x) +2

from x=-3 to x=3.

Any help? what's wrong with my code?

I followed an example problem almost to a tee to get this, and everything compiles fine. However, my output looks like this:Code:#include <stdio.h> #include <math.h> double Newton_Raphson(double x0); double func1(double x); double func1_d(double x); int main(void) { double x_begin=-3.0, del_x=0.25; double x, x_old, x0, root, f_x, f_x_old; int k; char sign_change; /* If there's a sign change, I put "Y" next to that value of x. If not, I leave it blank*/ printf(" x f(x) sign change\n"); printf("--------------------------------... x=x_begin; f_x=func1(x); printf("%8.2f %12.4f\n", x, f_x); { 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 %-13.5e\n", root); } else printf("%8.2f %12.4f %c\n", x, f_x, sign_change); } printf("\n"); exit(0); return; } double func1(double x) //my f(x) function { double f_x; f_x = sin(x)+2x+1; return f_x; } double func1_d(double x) //my d'f(x) function { double fd_x; fd_x = cos(x)+2; return fd_x; } double Newton_Raphson(double x0) { /* my tolerances I doubt this is where the problem lies as I erased this part from the coding and it came out with the same error */ 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); tolx = 1.e-8 * fabs(x0); tolf = 1.e-6 * fabs(f0); 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); x0 = x1; f0 = f1; } while(del_x > tolx && fabs(f1) > tolf); return x1; }

x f(x) sign change

--------------------------------------...

-3.00 0.0000

-3.00 0.0000 Y

f(-3) = 0.000000e+00

f(NaN) = 0.000000e+00

A refined root is NaN

-2.75 0.0000 Y

f(-2.875) = 0.000000e+00

f(NaN) = 0.000000e+00

A refined root is NaN

-2.50 0.0000 Y

f(-2.625) = 0.000000e+00

f(NaN) = 0.000000e+00

A refined root is NaN

-2.25 0.0000 Y

f(-2.375) = 0.000000e+00

f(NaN) = 0.000000e+00

A refined root is NaN

-2.00 0.0000 Y

f(-2.125) = 0.000000e+00

f(NaN) = 0.000000e+00

A refined root is NaN

-1.75 0.0000 Y

f(-1.875) = 0.000000e+00

f(NaN) = 0.000000e+00

A refined root is NaN

etc...etc...

Any help is greatly appreciated.

Thanks!

Add'l Info: This code piece is inside of a menu-style code, but it's meant to function as a standalone program. I doubt this is the problem, but it may very well be^^