【问题标题】:Trying to implement perameters (arguments) (args[]) into static double尝试将参数(参数)(args [])实现为静态双精度
【发布时间】:2016-06-01 19:18:52
【问题描述】:

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

它应该是1x^3 + 2x^2 + 0^2 + 5。一切似乎都有效,但我无法让 args 坚持到底,也不知道为什么。如果有人可以帮助我,我已经花了将近 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);
    }

}

【问题讨论】:

    标签: java parsing static command-line-arguments args


    【解决方案1】:

    假设多项式采用以下形式: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);
            }
    
        }
    

    【讨论】:

    • 不客气。如果您觉得有用,请您标记答案。干杯。
    猜你喜欢
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    相关资源
    最近更新 更多