c++methodsiterationbisection

C++ Newton Raphson method is slower than bisection?



My task is to find the root of a function with both the Newton Raphson and the bisection method within an error margin of 10E-7. The point of all that is, that we learn that the Newton Raphson method is faster and more effective. Now for some reason I come to the opposite result. Although I am aware that the initial guess of the root in both methods strongly affects the number of necessary iterations. But I entered a similar guess in both algorithms and my fellow students dont get the result I do.

Bisection method:

#include <iostream>
#include <iomanip>

using namespace std;


//Declaring the given function
double func1(double x) {

    return 0.00000000027 * (x - 10000000) - 0.16460351745 * (-1 + ((1000000000) / (x))) * 1 / (sqrt(x));

}

int main() {
    std::fixed;


    //Initial guess: root ist at 10 to the 7.
    double x1 = 10000000;
    double x2 = 1000000000;
    double eps = 0.0000001;
    int i = 0;
    double x0[100000];
    x0[0] =0;


    //Exception handler
    if (func1(x1) * func1(x2) > 0) {
        cout << "Root is not inside the bracket.";
        goto end;
    }
    goto start;


    //Bisection Algorithm 
    while (abs(x0[i] - x0[i-1]) >= eps) {

    start:

        i = i + 1;
        x0[i] = 0.5 * (x1 + x2);


        if (func1(x1) * func1(x0[i]) < 0) {

            x2 = x0[i];
        }
        else {
            x1 = x0[i];
        }





    }

    cout << endl << "Bisection Method: " << fixed << setprecision(10) << x0[i] << endl << "Iterations: " << i << endl << endl << endl << endl << endl;

    end:
    return 0;
}
}

Newton Raphson:

#include <iostream>
#include <iomanip>

using namespace std;

// Declaring the function and its derivative
 double func1(double x) {

  return 0.00000000027 * (x - 10000000) - 0.16460351745 * (-1 + ((1000000000) / (x))) * 1 / (sqrt(x));

}

double funcderiv1(double x) {

     return 0.00000000027+((0.1646035174)/(2*x*x*sqrt(x)))*(30000000-x);

}


int main()
{
    std::fixed;
    double eps = 1;
    double x_start = 10000000;
    double c;

    int i = 0;

    while (eps >= 0.0000001) {


        c = x_start - ((func1(x_start)) / (funcderiv1(x_start)));
        eps = abs(func1(x_start) / funcderiv1(x_start));
        x_start = c;

        i = i + 1;



    }

    cout << fixed << setprecision(5) << "RESULT " << c << endl << " Iterations: " << i << endl;

}


The root is at 17903534.23630

Does anyone know why my bisection method needs 55 iterations while Newton Raphson takes like 82?


Solution

  • For the function

    f(x) = A * (x - B) - C * (D / x - 1) / sqrt(x)
    
    A = 0.00000000027
    B = 10000000
    C = 0.16460351745
    D = 1000000000
    

    the correct derivative is:

    f'(x) = A - C (x - 3D) / (2 * x * x * sqrt(x))
    

    Compare this with your expression:

    g(x) = A - C (x - 3B) / (2 * x * x * sqrt(x))
    

    After fixing the formula (by adding two zeros), your code makes 6 iterations:

    RESULT 17903534.23630
    
     Iterations: 6