2

I am making a program to apply Newton-Raphson method in Java with an equation:

f(x) = 3x - e^x + sin(x)

And

g(x) = f'(x) = 3- e^x + cos (x)

The problem is when I tried to solve the equation in a paper to reach an error less than (0.5%)
I got:

          Xn                             |           Error

        Xo = 2                        |    ------------------------

  X1 = 1.900158400           |            5.254%

  X2 = 1.89012709             |            0.5307%

But when I made the program in Java it does not reach the last line which is the required error
(Ex: X2 = 1.89012709)
It only displays the first line which is the first step which is
(X1 = 1.900158400)

My Java code is:

package newton.raphson.method;

public class NewtonRaphsonMethod {


          // let f be a function defined as f(x) = 3x - e^x + sin(x)

        public static double f (double x){

            return (3*x-(Math.pow(Math.E, x))+Math.sin(x));
        }

        // let g be a function defined as g(x) = f'(x) = 3- e^x + cos (x)


     public static double g (double x){

            return (3-(Math.pow(Math.E, x))+Math.cos(x));
        }


          public static double NewtonRaphson (){
              int iterations_number=0;
              boolean cont = true;
            double x0 , x1, Error=5000;
            x0 =2;
            x1=0;

            while (cont){
            x1 = x0 - (f(x0)/g(x0));
            Error = (Math.abs(x1-x0)/x1)*100;
            iterations_number++;
            if (f(x1)<=0.05){
            cont = false;
            System.out.println("The Program did it in "+iterations_number+" Step(s)");
            System.out.println("The root is: "+ x1);
             System.out.println("The Error is: "+(Math.abs(x1-x0)/x1)*100+"%");
            }
            }

            return x1;
        }

    public static void main(String[] args) {
         NewtonRaphson();

    }
}


And the output is:

The Program did it in 1 Step(s)
The root is: 1.9001584993293807
The Error is: 5.254377500921955%
  • I don't see any place in your code where you calculate, or display x2 – azurefrog Mar 06 '15 at 19:09
  • @azurefrog I already made a while loop, so it should calculate it automatically –  Mar 06 '15 at 19:11
  • You said in your question you are looking for "(Ex: X2 = 1.89012709)". There's nothing in your code called x2. You neither calculate nor attempt to display anything after "The Error is: ", which is the output you are getting. What is your *expected* output? – azurefrog Mar 06 '15 at 19:12
  • I want the while loop to end when the error reaches less than 0.5% –  Mar 06 '15 at 19:13
  • Then you should make your while-loop dependent on the error being less then 0.5%. Currently you terminate when `f(x1) <= 0.05`, not when your error is less than 0.05. – azurefrog Mar 06 '15 at 19:15
  • When i tried to make the if condition if (Error <= 0.5), I get nothing at all, because there is a problem in the while loop which I can't find. –  Mar 06 '15 at 19:18

3 Answers3

0

The reason that your code is never "hitting the last line", presumably you are referring to the return statement in your NewtonRhapson() method, is that it is in an infinite loop. Each iteration of the loop is identical to the last. You set x0 outside of the loop, then never set it again. Given that the rest of the values / calculations in your loop are derived from x0 you are getting the same results over and over again.

The Gilbert Arenas Dagger
  • 12,071
  • 13
  • 66
  • 80
0

It takes about 5 iterations to converge to a root with an error of 1e-12.

public class Newton {
public static double f(double x){
    return(3*x-Math.exp(x)+Math.sin(x)); /* function */
}
public static double fp(double x){
    return(3-Math.exp(x)+Math.cos(x)); /* derivative */
}

public static double NewtonMethod() {
    int it_count=0; /* iteration counter */
    int count_max=10; /* maximum allowed number of iterations*/
    double x0=2.0, x=1.0, error=1.0, tol=1E-12; /* initialize */

    while((it_count<=count_max) && (error>=tol))
    {
        /* loop while count<=10 and Error >= 1E-12 */
        x = x0 - f(x0)/fp(x0); /* Newton-Raphson method*/
        error = Math.abs(x-x0); /* error  */
        it_count++; /*update count*/
        x0=x; /* update root*/
    }
    System.out.println("The root is: "+ x); /* print root*/
    System.out.println("The number of iterations is: "+ it_count);/* print count*/
    return x;
}
public static void main(String[] args){
    NewtonMethod();
}

}

-1
public class NewtonRaphsonMethod {


          // let f be a function defined as f(x) = 3x - e^x + sin(x)

        public static double f (double x){

           // return (3*x-(Math.pow(Math.E, x))+Math.sin(x));
            return((Math.pow(x, 2))+5*x+6);
        }

        // let g be a function defined as g(x) = f'(x) = 3- e^x + cos (x)


     public static double g (double x){

           // return (3-(Math.pow(Math.E, x))+Math.cos(x));
         return(2*x+5);
        }


          public static double NewtonRaphson (){
              int iterations_number=0;
              boolean cont = true;
            double x0 , x1, Error=0;
            x0 =-1.8;
            x1=0;

            while (cont){

            x1 = x0 - (f(x0)/g(x0));
            Error = Math.abs(x1-x0);
            iterations_number++;
           // if (Error<=0.0000000005){
            if(iterations_number>100){
            cont = false;
            System.out.println("The Program did it in "+iterations_number+" Step(s)");
            System.out.println("The root is: "+ x1);
             System.out.println("The Error is: "+(Math.abs(x1-x0)/x1)*100+"%");
            }else{
                x0=x1;
            }
            }

            return x1;
        }

    public static void main(String[] args) {
         NewtonRaphson();

    }
}
Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61
X-Dark
  • 1