2016-02-20 8 views
1

Я немного новичок в запуске параметров через командную строку. У меня возникли проблемы с внедрением последних 4 параметров в командную строку, поэтому пример ввода в командной строке будет java newton 2 4 .005 50 1 2 0 5 с 1 2 0 5, являющимися коэффициентами полинома в статическом двойном дне в обратном улове.Попытка реализовать параметры (аргументы) (args []) в статическом двойном

Следует читать 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); 
    } 

} 

ответ

0

Предположим, полином имеет вид: а х^3 + б х^2 + C * х + д Список

сделанных изменений:

  • Измененный имя класса Ньютон (от ньютона)
  • Сохраненных полиномиального со-efficients в, б, в, г
  • модифицированных функции F() и fprime(), чтобы использовать а, б, в, г

Пожалуйста, попробуйте и это поможет.

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

    } 
+0

Wow genuis thank you !!!!!! – gil

+0

Добро пожаловать. Не могли бы вы отметить ответ, если найдете его полезным. Приветствия. –