fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. public class Main {
  8.  
  9. static double f(double x) {
  10. return 2*x*x*x + 3*x - 1;
  11. }
  12.  
  13. public static void main(String[] args) {
  14.  
  15. double x0 = -1; // starting value
  16. double x1 = 2; // starting value
  17. double eps = 1e-6;
  18. double x2, f0, f1, f2;
  19. int iter = 1;
  20.  
  21. System.out.println("Enter the value of x0: -1");
  22. System.out.println("Enter the value of x1: 2\n");
  23.  
  24. System.out.println("Iteration x0\t\t x1\t\t x2\t\t f0\t\t f1\t\t f2");
  25. System.out.println("--------------------------------------------------------------------------------");
  26.  
  27. while (Math.abs(x1 - x0) > eps) {
  28.  
  29. x2 = (x0 + x1) / 2;
  30. f0 = f(x0);
  31. f1 = f(x1);
  32. f2 = f(x2);
  33.  
  34. System.out.printf("%-10d %.6f %.6f %.6f %.6f %.6f %.6f\n",
  35. iter, x0, x1, x2, f0, f1, f2);
  36.  
  37. if (f0 * f2 < 0)
  38. x1 = x2;
  39. else
  40. x0 = x2;
  41.  
  42. iter++;
  43. }
  44.  
  45. System.out.println("\nApproximate root = " + (x0 + x1) / 2);
  46. }
  47. }
  48.  
Success #stdin #stdout 0.25s 58336KB
stdin
Standard input is empty
stdout
Enter the value of x0: -1
Enter the value of x1:  2

Iteration   x0		  x1		  x2		   f0		   f1		   f2
--------------------------------------------------------------------------------
1          -1.000000  2.000000  0.500000  -6.000000  21.000000  0.750000
2          -1.000000  0.500000  -0.250000  -6.000000  0.750000  -1.781250
3          -0.250000  0.500000  0.125000  -1.781250  0.750000  -0.621094
4          0.125000  0.500000  0.312500  -0.621094  0.750000  -0.001465
5          0.312500  0.500000  0.406250  -0.001465  0.750000  0.352844
6          0.312500  0.406250  0.359375  -0.001465  0.352844  0.170952
7          0.312500  0.359375  0.335938  -0.001465  0.170952  0.083636
8          0.312500  0.335938  0.324219  -0.001465  0.083636  0.040819
9          0.312500  0.324219  0.318359  -0.001465  0.040819  0.019611
10         0.312500  0.318359  0.315430  -0.001465  0.019611  0.009057
11         0.312500  0.315430  0.313965  -0.001465  0.009057  0.003792
12         0.312500  0.313965  0.313232  -0.001465  0.003792  0.001163
13         0.312500  0.313232  0.312866  -0.001465  0.001163  -0.000151
14         0.312866  0.313232  0.313049  -0.000151  0.001163  0.000506
15         0.312866  0.313049  0.312958  -0.000151  0.000506  0.000177
16         0.312866  0.312958  0.312912  -0.000151  0.000177  0.000013
17         0.312866  0.312912  0.312889  -0.000151  0.000013  -0.000069
18         0.312889  0.312912  0.312901  -0.000069  0.000013  -0.000028
19         0.312901  0.312912  0.312906  -0.000028  0.000013  -0.000008
20         0.312906  0.312912  0.312909  -0.000008  0.000013  0.000003
21         0.312906  0.312909  0.312908  -0.000008  0.000003  -0.000003
22         0.312908  0.312909  0.312908  -0.000003  0.000003  0.000000

Approximate root = 0.3129080533981323