can someone double check this for me please? (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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#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
Why use this long method? There is a formula for this.
http://en.wikipedia.org/wiki/Cubic_polynomial

There is a mathematical solution. There is probably a formula somewhere in the internet...
Last edited on
Topic archived. No new replies allowed.