fork download
  1. class BisectionMethod {
  2. static double f(double x) {
  3. return 2*x*x*x + 3*x - 1;
  4. }
  5.  
  6. public static void main(String[] args) {
  7. double x1 = 0, x2 = 1, x0, E = 1e-8;
  8. int i = 0;
  9.  
  10. if (f(x1) * f(x2) > 0) {
  11. System.out.println("Invalid interval!");
  12. return;
  13. }
  14.  
  15. System.out.printf("%-5s %-12s %-12s %-12s %-12s %-12s\n",
  16. "Step", "x1", "x2", "x0", "f(x0)", "Error");
  17.  
  18. double error = Math.abs((x2 - x1) / x2);
  19. while (error >= E) {
  20. x0 = (x1 + x2) / 2;
  21. i++;
  22. double f0 = f(x0);
  23. System.out.printf("%-5d %-12.8f %-12.8f %-12.8f %-12.8f %-12.8f\n",
  24. i, x1, x2, x0, f0, error);
  25.  
  26. if (f(x1) * f0 < 0)
  27. x2 = x0;
  28. else
  29. x1 = x0;
  30.  
  31. error = Math.abs((x2 - x1) / x2);
  32. }
  33.  
  34. System.out.println("\nApproximate root = " + (x1 + x2) / 2);
  35. System.out.println("Total steps = " + i);
  36. }
  37. }
  38.  
Success #stdin #stdout 0.2s 58712KB
stdin
Standard input is empty
stdout
Step  x1           x2           x0           f(x0)        Error       
1     0.00000000   1.00000000   0.50000000   0.75000000   1.00000000  
2     0.00000000   0.50000000   0.25000000   -0.21875000  1.00000000  
3     0.25000000   0.50000000   0.37500000   0.23046875   0.50000000  
4     0.25000000   0.37500000   0.31250000   -0.00146484  0.33333333  
5     0.31250000   0.37500000   0.34375000   0.11248779   0.16666667  
6     0.31250000   0.34375000   0.32812500   0.05503082   0.09090909  
7     0.31250000   0.32812500   0.32031250   0.02666569   0.04761905  
8     0.31250000   0.32031250   0.31640625   0.01257145   0.02439024  
9     0.31250000   0.31640625   0.31445313   0.00554611   0.01234568  
10    0.31250000   0.31445313   0.31347656   0.00203884   0.00621118  
11    0.31250000   0.31347656   0.31298828   0.00028655   0.00311526  
12    0.31250000   0.31298828   0.31274414   -0.00058926  0.00156006  
13    0.31274414   0.31298828   0.31286621   -0.00015138  0.00078003  
14    0.31286621   0.31298828   0.31292725   0.00006758   0.00039002  
15    0.31286621   0.31292725   0.31289673   -0.00004190  0.00019505  
16    0.31289673   0.31292725   0.31291199   0.00001284   0.00009752  
17    0.31289673   0.31291199   0.31290436   -0.00001453  0.00004876  
18    0.31290436   0.31291199   0.31290817   -0.00000085  0.00002438  
19    0.31290817   0.31291199   0.31291008   0.00000599   0.00001219  
20    0.31290817   0.31291008   0.31290913   0.00000257   0.00000610  
21    0.31290817   0.31290913   0.31290865   0.00000086   0.00000305  
22    0.31290817   0.31290865   0.31290841   0.00000001   0.00000152  
23    0.31290817   0.31290841   0.31290829   -0.00000042  0.00000076  
24    0.31290829   0.31290841   0.31290835   -0.00000021  0.00000038  
25    0.31290835   0.31290841   0.31290838   -0.00000010  0.00000019  
26    0.31290838   0.31290841   0.31290840   -0.00000005  0.00000010  
27    0.31290840   0.31290841   0.31290840   -0.00000002  0.00000005  
28    0.31290840   0.31290841   0.31290841   -0.00000001  0.00000002  
29    0.31290841   0.31290841   0.31290841   -0.00000000  0.00000001  

Approximate root = 0.3129084100946784
Total steps = 29