Java中的Newton-Raphson方法

问题描述 投票:2回答:2

我正在制作一个程序,用Java中的Newton-Raphson方法应用等式: f(x)= 3x - e ^ x + sin(x) 和 g(x)= f'(x)= 3- e ^ x + cos(x)

问题是当我试图在纸上解决方程式以达到小于(0.5%)的误差时 我有:

Xn |错误 Xo = 2 | ------------------------ X1 = 1.900158400 | 5.254% X2 = 1.89012709 | 0.5307%

但是当我用Java编写程序时,它没有到达最后一行,这是所需的错误 (例如:X2 = 1.89012709) 它只显示第一行,即第一步 (X1 = 1.900158400)

我的Java代码是:

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();

    }
}

输出是:

The Program did it in 1 Step(s)
The root is: 1.9001584993293807
The Error is: 5.254377500921955%
java netbeans newtons-method
2个回答
0
投票

您的代码永远不会“命中最后一行”的原因,可能是您在NewtonRhapson()方法中引用的return语句,它是在无限循环中。循环的每次迭代都与最后一次相同。您将x0设置在循环外部,然后再从不设置它。鉴于循环中的其余值/计算是从x0派生的,您反复得到相同的结果。


-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();

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.