fork download
  1. #include <iostream> //id=240242103
  2. #include <cmath>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int main() {
  7. double x0, x1, x2, f0, f1, f2;
  8. double e = 1e-8;
  9. int step = 1;
  10.  
  11. cout << "Enter the value of x0: ";
  12. cin >> x0;
  13. cout << "Enter the value of x1: ";
  14. cin >> x1;
  15.  
  16. f0 = 2*x0*x0*x0 + 3*x0 - 1;
  17. f1 = 2*x1*x1*x1 + 3*x1 - 1;
  18.  
  19. if (f0 * f1 > 0) {
  20. cout << "Invalid interval! f(x0) and f(x1) must have opposite signs." << endl;
  21. return 0;
  22. }
  23.  
  24. cout << fixed << setprecision(6);
  25. cout << "\nIteration x0 x1 x2 f0 f1 f2\n";
  26. cout << "-------------------------------------------------------------------------\n";
  27.  
  28. do {
  29. x2 = (x0 + x1) / 2;
  30. f2 = 2*x2*x2*x2 + 3*x2 - 1;
  31.  
  32. cout << step << "\t" << x0 << "\t" << x1 << "\t" << x2
  33. << "\t" << f0 << "\t" << f1 << "\t" << f2 << endl;
  34.  
  35. if (f0 * f2 < 0)
  36. x1 = x2;
  37. else
  38. x0 = x2;
  39.  
  40. f0 = 2*x0*x0*x0 + 3*x0 - 1;
  41. f1 = 2*x1*x1*x1 + 3*x1 - 1;
  42. step++;
  43. } while (fabs(x1 - x0) >= e);
  44.  
  45. cout << "\nApproximate Root = " << x2 << endl;
  46. return 0;
  47. }
  48.  
  49.  
Success #stdin #stdout 0s 5320KB
stdin
-1
1
stdout
Enter the value of x0: Enter the value of x1: 
Iteration  x0        x1        x2        f0         f1         f2
-------------------------------------------------------------------------
1	-1.000000	1.000000	0.000000	-6.000000	4.000000	-1.000000
2	0.000000	1.000000	0.500000	-1.000000	4.000000	0.750000
3	0.000000	0.500000	0.250000	-1.000000	0.750000	-0.218750
4	0.250000	0.500000	0.375000	-0.218750	0.750000	0.230469
5	0.250000	0.375000	0.312500	-0.218750	0.230469	-0.001465
6	0.312500	0.375000	0.343750	-0.001465	0.230469	0.112488
7	0.312500	0.343750	0.328125	-0.001465	0.112488	0.055031
8	0.312500	0.328125	0.320312	-0.001465	0.055031	0.026666
9	0.312500	0.320312	0.316406	-0.001465	0.026666	0.012571
10	0.312500	0.316406	0.314453	-0.001465	0.012571	0.005546
11	0.312500	0.314453	0.313477	-0.001465	0.005546	0.002039
12	0.312500	0.313477	0.312988	-0.001465	0.002039	0.000287
13	0.312500	0.312988	0.312744	-0.001465	0.000287	-0.000589
14	0.312744	0.312988	0.312866	-0.000589	0.000287	-0.000151
15	0.312866	0.312988	0.312927	-0.000151	0.000287	0.000068
16	0.312866	0.312927	0.312897	-0.000151	0.000068	-0.000042
17	0.312897	0.312927	0.312912	-0.000042	0.000068	0.000013
18	0.312897	0.312912	0.312904	-0.000042	0.000013	-0.000015
19	0.312904	0.312912	0.312908	-0.000015	0.000013	-0.000001
20	0.312908	0.312912	0.312910	-0.000001	0.000013	0.000006
21	0.312908	0.312910	0.312909	-0.000001	0.000006	0.000003
22	0.312908	0.312909	0.312909	-0.000001	0.000003	0.000001
23	0.312908	0.312909	0.312908	-0.000001	0.000001	0.000000
24	0.312908	0.312908	0.312908	-0.000001	0.000000	-0.000000
25	0.312908	0.312908	0.312908	-0.000000	0.000000	-0.000000
26	0.312908	0.312908	0.312908	-0.000000	0.000000	-0.000000
27	0.312908	0.312908	0.312908	-0.000000	0.000000	-0.000000
28	0.312908	0.312908	0.312908	-0.000000	0.000000	-0.000000

Approximate Root = 0.312908