Thread: <( ' '<) Simple Programming Question?

  1. #1
    Registered User
    Join Date
    Mar 2009

    <( ' '<) Simple Programming Question?

    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?

    #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("%8.2f %12.4f\n", x, f_x);
    sign_change=' ';
    x=x_begin + (double)k*del_x;
    if(f_x*f_x_old <= 0.0){
    sign_change = 'Y';
    printf("%8.2f %12.4f %c\n", x, f_x,
    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,
    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);
    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;
    I followed an example problem almost to a tee to get this, and everything compiles fine. However, my output looks like this:

    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

    Any help is greatly appreciated.


    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^^

  2. #2
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    1. indent the code
    2. where is k initialized?
    To be or not to be == true

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM