Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的Newton-Raphson方法_Java_Netbeans_Newtons Method - Fatal编程技术网

Java中的Newton-Raphson方法

Java中的Newton-Raphson方法,java,netbeans,newtons-method,Java,Netbeans,Newtons Method,我正在制作一个程序,在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编写程序时,它没有到达最后一行,这是必

我正在制作一个程序,在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编写程序时,它没有到达最后一行,这是必需的错误
(Ex: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();

    }
}

您的代码永远不会“到达最后一行”的原因,可能是您在引用NewtonRhapson()方法中的return语句,是因为它位于无限循环中。循环的每个迭代都与上一个相同。将x0设置在循环之外,然后再也不设置它。假设循环中的其余值/计算都是从x0派生的,那么您会一次又一次地得到相同的结果。

代码从未“到达最后一行”的原因,可能是指NewtonRhapson()方法中的return语句,因为它位于无限循环中。循环的每个迭代都与上一个相同。将x0设置在循环之外,然后再也不设置它。假设循环中的其余值/计算是从x0导出的,那么您会一次又一次地得到相同的结果。

公共类NewtonRaphsonMethod{
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();

    }
}
//设f是定义为f(x)=3x-e^x+sin(x)的函数 公共静态双f(双x){ //return(3*x-(Math.pow(Math.E,x))+Math.sin(x)); 返回((数学功率(x,2))+5*x+6); } //设g是定义为g(x)=f'(x)=3-e^x+cos(x)的函数 公共静态双g(双x){ //return(3-(Math.pow(Math.E,x))+Math.cos(x)); 返回(2*x+5); } 公共静态双牛顿函数(){ 整数迭代次数=0; 布尔控制=真; 双x0,x1,误差=0; x0=-1.8; x1=0; while(续){ x1=x0-(f(x0)/g(x0)); 误差=数学绝对值(x1-x0); 迭代次数_number++; //如果(错误100){ cont=假; System.out.println(“程序在“+迭代次数+步骤”中完成); System.out.println(“根为:“+x1”); System.out.println(“错误为:”+(Math.abs(x1-x0)/x1)*100+“%”; }否则{ x0=x1; } } 返回x1; } 公共静态void main(字符串[]args){ 牛顿拉斐逊(); } }
公共类NewtonRaphsonMethod{
//设f是定义为f(x)=3x-e^x+sin(x)的函数
公共静态双f(双x){
//return(3*x-(Math.pow(Math.E,x))+Math.sin(x));
返回((数学功率(x,2))+5*x+6);
}
//设g是定义为g(x)=f'(x)=3-e^x+cos(x)的函数
公共静态双g(双x){
//return(3-(Math.pow(Math.E,x))+Math.cos(x));
返回(2*x+5);
}
公共静态双牛顿函数(){
整数迭代次数=0;
布尔控制=真;
双x0,x1,误差=0;
x0=-1.8;
x1=0;
while(续){
x1=x0-(f(x0)/g(x0));
误差=数学绝对值(x1-x0);
迭代次数_number++;
//如果(错误100){
cont=假;
System.out.println(“程序在“+迭代次数+步骤”中完成);
System.out.println(“根为:“+x1”);
System.out.println(“错误为:”+(Math.abs(x1-x0)/x1)*100+“%”;
}否则{
x0=x1;
}
}
返回x1;
}
公共静态void main(字符串[]args){
牛顿拉斐逊();
}
}

我看不到您的代码中有任何计算或显示的地方x2@azurefrog我已经做了一个while循环,所以它应该自动计算你在问题中所说的你要寻找的“(Ex:X2=1.89012709)”。你的代码中没有x2。您既不计算也不尝试在“错误为:”之后显示任何内容,这是您得到的输出。您的预期输出是什么?我希望当错误达到小于0.5%时,while循环结束,然后您应该使while循环依赖于小于0.5%的错误。当前,当
f(x1)时终止,我在代码中没有看到计算或显示的任何位置x2@azurefrog我已经做了一个while循环,所以它应该自动计算你在问题中所说的你要寻找的“(Ex:X2=1.89012709)”。你的代码中没有x2。您既不计算也不尝试在“错误为:”之后显示任何内容,这是您得到的输出。您的预期输出是什么?我希望当错误达到小于0.5%时,while循环结束,然后您应该使while循环依赖于小于0.5%的错误。当前,当
f(x1)在您的答案中添加一些注释时,您将终止。它将对其他人有帮助。该程序在101个步骤中完成了此操作。根为:1.89002972992519856错误为:f(x)=3x-e^x+sin(x)的0.0%。请在您的答案中添加一些注释。它将对其他人有帮助。程序在101个步骤中完成了它。根是:1.89002972992519856错误是:f(x)=3x-e^x+sin(x)的0.0%
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();

    }
}