java - 尝试将参数(参数)(args[])实现为静态 double

标签 java parsing static command-line-arguments args

我对通过命令行运行参数有点陌生。我在命令行中实现最后 4 个参数时遇到问题,因此命令行上的示例输入将是 java newton 2 4 .005 50 1 2 0 5 ,其中 1 2 0 5 是 return catch 底部的 static double 中的多项式系数。

它应该是1x^3 + 2x^2 + 0^2 + 5。一切似乎都有效,但我无法让参数粘在底部,也不知道为什么。如果有人可以帮助我,我已经花了近 10 个小时尝试研究,但似乎无法在任何地方找到任何有关此问题的帮助。

import java.util.Scanner;

import java.text.DecimalFormat;

public class newton {
    public static void main(String[] args) {
        double x0, xnew, xxnew;// Initiating double
        double x1, p1;
        double fx0, fx1;
        double delta, delta1; // amount added to get next iterate
        double error; // error estimate
        double tol = Double.parseDouble(args[2]);// tolerance (max error)
        int i, maxIts, j; // iteration count and maximum number of
                            // iteraterations made
        x0 = Integer.parseInt(args[0]);
        x1 = Integer.parseInt(args[1]);
        p1 = Integer.parseInt(args[4]);
        maxIts = Integer.parseInt(args[3]);

        DecimalFormat fmt = new DecimalFormat("0.############");

        System.out.println("\n");
        System.out.println("Polynomail Root Finder By [Gilbert Jimenez]" + "\n");
        System.out.println("Initial Perameters :" + "\n");
        System.out.println("P0 : = " + args[0]);
        System.out.println("p1 : = " + args[1]);
        System.out.println("Tol = " + tol);
        System.out.println("Maximum = " + maxIts + "\n");
        System.out.println("Polynomial is of order:  4 ");
        System.out.println("Terms of polynomial: " + args[4] + "x^3" + "+" + args[5] + "x^2" + "+" + args[6] + "x" + "+"
                + args[7]);

        {
            // Performing Newton's method
            i = 1;
            error = 100;
            System.out.println("Newtons Method:\t     " + "\n");

            while (i <= maxIts && error > tol) {
                delta = -(f(x0) / fprime(x0));
                error = Math.abs(delta);
                xnew = x0 + delta;

                System.out.println("p" + i + "\t" + fmt.format(xnew));
                i++;
                x0 = xnew;
            }

            System.out.println("\n");
            System.out.println("Solution found after " + i + " " + "itterations :" + fmt.format(x0) + "\n");
        }

        {
            // Performing
            j = 1;
            error = 100;

            System.out.println("Secant Method:\t   " + "\n");
            fx0 = f(x0);
            while (j <= maxIts && error > tol) {
                fx1 = f(x1);
                delta1 = (-fx1 * (x1 - x0) / (fx1 - fx0));
                error = Math.abs(delta1);
                xxnew = x1 + delta1;

                System.out.println("p" + j + "\t" + fmt.format(xxnew));
                j++;
                x0 = x1;
                fx0 = fx1;
                x1 = xxnew;
            }

            System.out.println("\n");
            System.out.println("Solution found after " + j + " " + "itterations :" + fmt.format(x1) + "\n");
        }
    }

    // function of f
    public static double f(double x) {
        return (x * x * x - 2.0 * x * x + 0 * x - 5);
    }

    // derivative of f
    public static double fprime(double x) {
        return (3.0 * x * x - 4.0 * x);
    }

}

最佳答案

假设多项式采用以下形式:ax^3 + bx^2 + c*x + d

所做的更改列表:

  • 将类名称更改为 Newton(从 newton)
  • 将多项式系数保存到 a、b、c、d 中
  • 修改函数 f() 和 fprime() 以使用 a、b、c、d

请尝试看看这是否有帮助。

   public class Newton {

        static int a = 0;
        static int b = 0;
        static int c = 0;
        static int d = 0;

        public static void main(String[] args)  {

            double x0, xnew, xxnew;// Initiating double
            double x1, p1;
            double fx0, fx1;
            double delta, delta1; // amount added to get next iterate
            double error; // error estimate
            double tol = Double.parseDouble(args[2]);// tolerance (max error)

            int i, maxIts, j; // iteration count and maximum number of
                                // iteraterations made

            x0 = Integer.parseInt(args[0]);
            x1 = Integer.parseInt(args[1]);
            p1 = Integer.parseInt(args[4]);
            maxIts = Integer.parseInt(args[3]);

            DecimalFormat fmt = new DecimalFormat("0.############");

            System.out.println("\n");
            System.out.println("Polynomail Root Finder By [Gilbert Jimenez]" + "\n");
            System.out.println("Initial Perameters :" + "\n");
            System.out.println("P0 : = " + args[0]);
            System.out.println("p1 : = " + args[1]);
            System.out.println("Tol = " + tol);
            System.out.println("Maximum = " + maxIts + "\n");
            System.out.println("Polynomial is of order:  4 ");

            a = Integer.valueOf(args[4]);
            b = Integer.valueOf(args[5]);
            c = Integer.valueOf(args[6]);
            d = Integer.valueOf(args[7]);

            System.out.println("Terms of polynomial: " + a + "x^3" + "+" + b + "x^2" + "+" + c + "x" + "+" + d);

            {

                // Performing Newton's method

                i = 1;
                error = 100;
                System.out.println("Newtons Method:\t     " + "\n");

                while (i <= maxIts && error > tol)

                {

                    delta = -(f(x0) / fprime(x0));
                    error = Math.abs(delta);
                    xnew = x0 + delta;
                    System.out.println("p" + i + "\t" + fmt.format(xnew));
                    i++;
                    x0 = xnew;

                }

                System.out.println("\n");
                System.out.println("Solution found after " + i + " " + "itterations :" + fmt.format(x0) + "\n");

            }

            {

                // Performing

                j = 1;
                error = 100;
                System.out.println("Secant Method:\t   " + "\n");

                fx0 = f(x0);

                while (j <= maxIts && error > tol)

                {

                    fx1 = f(x1);
                    delta1 = (-fx1 * (x1 - x0) / (fx1 - fx0));
                    error = Math.abs(delta1);
                    xxnew = x1 + delta1;
                    System.out.println("p" + j + "\t" + fmt.format(xxnew));

                    j++;
                    x0 = x1;
                    fx0 = fx1;
                    x1 = xxnew;

                }

                System.out.println("\n");
                System.out.println("Solution found after " + j + " " + "itterations :" + fmt.format(x1) + "\n");

            }

        }

        // function of f

        public static double f(double x)

        {
            return (a * x * x * x + b * x * x + c * x + d);
        }

        // derivative of f

        public static double fprime(double x)

        {
            return (3 * a * x * x + 2 * b * x + c);
        }

    }

关于java - 尝试将参数(参数)(args[])实现为静态 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35518495/

相关文章:

java - 日期格式异常

regex - 解析 CSV 字符串,同时忽略各个列内的逗号

Java - 使用 fastXML 和 Jackson 进行反序列化注释

c++ - 将静态 libcurl 添加到 Code::Blocks IDE

java - 为什么我不能在主类中创建除 main 方法之外的另一个方法?

java - 什么时候写入访问日志?

java - 我如何通过我在 GWT 中的历史记录来确定我是进入 "forward"还是 "backward"?

java - 具有纬度/经度 OpenGL 的线框球体

regex - 从两个特定字符串之间的任何位置删除特定字符?

Java 继承或静态方法