请大家帮忙弄个用牛顿法解方程的程序,不要说废话,只求代码

解决方案 »

  1.   

    private final double E = (double) 2.71828;private double f(double x)
        {
            return(x-Math.pow(E,-x));
        }    private double df(double x)
        {
            return(1+Math.pow(E,-x));
        }    private double iterate(double x)
        {
            double x1;
            x1=x-f(x)/df(x);
            return(x1);
        }
        private void newtonsMethod() {
            // TODO add your handling code here:
            final int MAXREPT = 1000;
            double x0,x1,eps;int k=0;
            double d;
            System.out.println("\n please input x0,eps:");
            Scanner s = new Scanner(System.in);
            x0 = s.nextDouble();
            eps = s.nextDouble();
            System.out.println("\n k xk\n");
            System.out.printf(" %d %f\n",k,x0);
            do
            {
                k++;
                x1=iterate(x0);
                System.out.printf(" %d %f\n",k,x1);
                d=Double.valueOf(x1-x0);
                x0=x1;
            } while((d>=eps)&(k<MAXREPT));
            if(k<MAXREPT) {
                System.out.printf("the root is x=%f, k=%d\n",x1,k);
            }else{
                System.out.printf("\n The iteration is failed!\n");
            }
        } 
      

  2.   

    把上面给的代码放入你自己的class,然后调用其函数就行了。