2014-09-21 3 views
3

자바에서 방정식을 풀기위한 이분법을 구현 중입니다. 나는 처음에 미리 정의 된 다항식 x^3 + 4x^2 - 10에 대한 해를 코딩했습니다. 이제 사용자가 입력하는 다항식에 대한 해를 일반화했습니다.자바에서의 2 분법 구현

나는 해당 학위의 계수를 읽었습니다. 이제 f(), f (b) 및 f (c)를 평가할 수 있도록 f() 메서드를 조정해야합니다.

// BISECTION METHOD IMPLEMENTATION IN JAVA 
// This program uses bisection method to solve for x^3 + 4x^2 -10 = 0 

package nisarg; 
import java.util.Scanner; 

public class BetterBisection { 

    public static void main(String[] args) { 
     double a, b, c; // a, b and c have the usual meaning 
     double f_of_a, f_of_b; // f_of_a, f_of_b store values of f(a) and f(b) 
          // respectively 
     int highest_degree; 
     System.out.println("What is the highest degree of your polynomial? "); 
     Scanner input = new Scanner(System.in); 
     highest_degree = input.nextInt(); 
     for (int i = highest_degree; i >= 0; i--) { 
     int coeff_deg_i; 
     coeff_deg_i = poly_input(i); 
     // System.out.println(coeff_deg_i); 
     } 
     // The following do-while loop keeps asking the user for a and b until 
     // f(a)f(b) does not become negative 
     do { 
     a = input(); 
     b = input(); 
     if (f(a) * f(b) >= 0) { 
      System.out 
        .println("Sorry the two numbers are not bracketing the root. Please try again "); 
     } 
     } while (f(a) * f(b) >= 0); 
     f_of_a = f(a); 
     f_of_b = f(b); 
     double root = bisectionMethod(f_of_a, f_of_b, a, b); 
     System.out.println("Root is : " + root); 
    } 

    public static double input() { // Reads in the bracketing number i.e a and b 
     Scanner input = new Scanner(System.in); 
     System.out.println("Enter a bracketing number"); 
     return (input.nextDouble()); 
    } 

    public static double f(double num) { // Calculates f(x) given x and returns 
             // f(x) 
     final int COEFF_DEG_3 = 1; // Coefficient of x^3 
     final int COEFF_DEG_2 = 4; // Coefficient of x^2 
     final int COEFF_DEG_0 = -10; // Coefficient of x^0 
     return (COEFF_DEG_3 * Math.pow(num, 3) + COEFF_DEG_2 * Math.pow(num, 2) + COEFF_DEG_0 
      * Math.pow(num, 0)); 
    } 

    public static double bisectionMethod(double f_of_a, double f_of_b, double a, 
     double b) { // Does the actual work of evaluating 
     double c; // the root using the method of bisection. 
     double f_of_c; 
     final double TOLERANCE = 0.0001; 
     while (Math.abs(a - b) > TOLERANCE) { 
     c = (a + b)/2; 
     f_of_c = f(c); 
     if (f_of_c * f(a) == 0 || f_of_c * f(b) == 0) { 
      return c; 
     } else if (f_of_c * f(a) > 0) { 
      a = c; 
     } else { 
      b = c; 
     } 
     } 
     return (a + b)/2; 
    } 

    public static int poly_input(int degree) { 
     System.out.println("Please enter coefficient for degree " + degree); 
     Scanner input = new Scanner(System.in); 
     int coefficient; 
     coefficient = input.nextInt(); 
     return coefficient; 
    } 
} 
+0

귀하의 질문은 거의 의미가 있습니다 :

public class global { public static int coeff_deg_1; public static int coeff_deg_2; public static int coeff_deg_3; // and so on... } 

또는 12 개 요소를 하나의 배열을 정의 : : 여기

public class global { public static final int coeff_degs = new int[12]; } 
Jared

+0

1) 게시 된 코드가 컴파일되지 않습니다. 2) Java에는 전역 변수가 없습니다. –

+0

12 변수를 전역 적으로 선언해야합니다. 나는 배열을 원하지 않는다. for 루프를 사용할 수 없다면 어떻게해야합니까? – tofu

답변

1

변수를 정의하는 데 루프를 사용할 수 없습니다.

public static double f(double x) { 
      return (x*x*x)-4*x-10; 
} 
public static double RecursiveBisection(Function fct, final double left, final double right, final double tolerance) { 
    double x = 0; 
    double dx = 0; 
    if (Math.abs(right - left) < tolerance) // base case 
     return (left + right)/2; 
    else { // recursive case 
     x = (left + right)/2; 
     System.out.println("Root obtained: " + x); 
     dx = right - left; 
     System.out.println("Estimated error: " + dx); 
     if (fct.f(left) * fct.f(x) > 0) // on same side 
      return RecursiveBisection (fct, x, right, tolerance); 
     else // opposite side 
      return RecursiveBisection(fct, left, x, tolerance); 

    } 
} 
+1

-1 글로벌 변경 가능 상태로해야한다. 여전히 모든 비용으로 피할 수 있습니다. – Basilevs

0

재귀 하나 하나 (12 개) 명시 적 변수가 있습니다. 12 개의 변수를 선언하거나 변수 배열을 원한다고 생각합니다. for 루프를 사용하여 여러 변수를 선언 할 수는 없습니다. `public static final int [] COEFF_DEG = new int [12];`