need someone to double check this for me (c)..

This is a discussion on need someone to double check this for me (c).. within the C Programming forums, part of the General Programming Boards category; so this is what i have to do: In order to find roots for f(x) = x*x*x - x*x- 9.0*x ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    8

    need someone to double check this for me (c)..

    so this is what i have to do:

    In order to find roots for f(x) = x*x*x - x*x- 9.0*x + 8.9 in the interval
    -1.0 <= x <= 5.0, create a table for x and f(x) from x=-1.0 to x=5.0 with
    an increment of del_x=0.25. Mark the intervals with 'Y' where f(x) changes
    sign. In the table, use %8.2f and %12.4f for printing values of x and
    f(x).
    In each interval where f(x) changes sign, use the mid-point value of x as
    an initial guess x0 for the Newton-Raphson method, obtain refined roots
    and print each root with %-12.6e. Use double Newton_Raphson(double x0)
    that gives a refined root for an initial guess of x0. (You are not allowed
    to modify double Newton_Raphson().)

    You need to write the following functions:
    double func1(double x) for f(x) = x*x*x - x*x- 9.0*x + 8.9;
    double func1_d(double x) for f'(x) = 3.0*x*x - 2.0*x - 9.0;
    .................................................. .....................
    Your output should look like this:

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

    and i did it (i think but when i a.out, i get something like half way right, half way wrong). can someone check me please? here's the code:

    Code:
    #include <stdio.h>
    #include <math.h>
    /* function prototypes */
    double func1(double x);
    double func1_d(double x);
    double Newton_Raphson(double x0);
    int main(void)
    {
            double x_begin=-1.0, del_x=0.25;
            double x, x_old, x0, root, f_x, f_x_old;
            int k;
            char sign_change;
    /* add necessary statements for the table and the Newton-Raphson refinements */
    /* You may compute f(-1.0) before starting the for-loop for incrementing x with k */
            printf("\nRoots of f(x) \n\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.4      %c\n", x, f_x, sign_change);
            }
    printf("\n");
            exit(0);/* normal termination */
    }
    double func1(double x)
    {
            double f_x;
            f_x = x*x*x - x*x- 9.0*x + 8.9;
            return f_x;
    }
    double func1_d(double x)
    {
            double fd_x;
            fd_x = 3.0*x*x - 2.0*x - 9.0;
            return fd_x;
    }
    double Newton_Raphson(double x0)
    {/* YOU ARE NOT ALLOWED TO MODIFY THIS FUNCTION */
    /* the Newton-Raphson method is used to find a root of f(x) for a given
    initial guess x0 */
    /* double func1(double x) computes f(x) while double func1_d(double x)
    computes f'(x) */
            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;
    }
    this is what i get when i compile and a.out:

    Roots of f(x)

    x f'(x) sign change
    -----------------------------------------
    -1.00 15.9000
    -0.75
    -0.50
    -0.25
    0.00
    0.25
    0.50
    0.75 3
    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.50
    1.75 f
    2.00 f
    2.25 f
    2.50 f
    2.75
    3.00
    3.25 3.4156 Y
    f(3.125) = 1.526953e+00
    f(3.0163) = 9.767939e-02
    f(3.00833) = 5.102961e-04
    f(3.00829) = 1.419436e-08
    A refined root is 3.008287e+00
    3.50
    3.75
    4.00 f
    4.25 f
    4.50 3
    4.75 3
    5.00 3

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
             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); //this should be the same as the other
            }

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    8
    Quote Originally Posted by tabstop View Post
    Code:
             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); //this should be the same as the other
            }
    lol... ty so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double check
    By calc in forum C Programming
    Replies: 2
    Last Post: 07-11-2009, 04:25 AM
  2. expected primary expression
    By mju4t in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 07:59 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 01:34 PM
  5. Double check on Java code.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 07-05-2002, 10:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21