fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. double f(double x)
  5. {
  6. return 2 * x * x * x + 3 * x - 1;
  7. }
  8.  
  9. int main()
  10. {
  11. double x1, x2, x0, f1, f2, f0;
  12. double E = 1e-8;
  13.  
  14. cout << "Enter the value of x0: ";
  15. cin >> x1;
  16. cout << "\nEnter the value of x1: ";
  17. cin >> x2;
  18.  
  19. f1 = f(x1);
  20. f2 = f(x2);
  21.  
  22. if (f1 * f2 > 0)
  23. {
  24. cout << "\nInvalid interval.\n";
  25. return 0;
  26. }
  27.  
  28. cout << "\nIteration\t x0\t\t x1\t\t x2\t\t f0\t\t f1\t\t f2\n";
  29.  
  30. for (int i = 1; ; i++)
  31. {
  32. x0 = (x1 + x2) / 2;
  33. f0 = f(x0);
  34.  
  35. cout << fixed << setprecision(6);
  36. cout << setw(3) << i << "\t" << x0 << "\t" << x1 << "\t" << x2
  37. << "\t" << f0 << "\t" << f1 << "\t" << f2 << endl;
  38.  
  39. if (fabs((x2 - x1) / x2) < E)
  40. {
  41. cout << "\nApproximate root = " << x0 << endl;
  42. break;
  43. }
  44.  
  45. if (f1 * f0 < 0)
  46. x2 = x0, f2 = f0;
  47. else
  48. x1 = x0, f1 = f0;
  49. }
  50.  
  51. return 0;
  52. }
  53.  
Success #stdin #stdout 0s 5308KB
stdin
4 
-2
stdout
Enter the value of x0: 
Enter the value of x1: 
Iteration	 x0		 x1		 x2		 f0		 f1		 f2
  1	1.000000	4.000000	-2.000000	4.000000	139.000000	-23.000000
  2	-0.500000	1.000000	-2.000000	-2.750000	4.000000	-23.000000
  3	0.250000	1.000000	-0.500000	-0.218750	4.000000	-2.750000
  4	0.625000	1.000000	0.250000	1.363281	4.000000	-0.218750
  5	0.437500	0.625000	0.250000	0.479980	1.363281	-0.218750
  6	0.343750	0.437500	0.250000	0.112488	0.479980	-0.218750
  7	0.296875	0.343750	0.250000	-0.057045	0.112488	-0.218750
  8	0.320312	0.343750	0.296875	0.026666	0.112488	-0.057045
  9	0.308594	0.320312	0.296875	-0.015444	0.026666	-0.057045
 10	0.314453	0.320312	0.308594	0.005546	0.026666	-0.015444
 11	0.311523	0.314453	0.308594	-0.004965	0.005546	-0.015444
 12	0.312988	0.314453	0.311523	0.000287	0.005546	-0.004965
 13	0.312256	0.312988	0.311523	-0.002340	0.000287	-0.004965
 14	0.312622	0.312988	0.312256	-0.001027	0.000287	-0.002340
 15	0.312805	0.312988	0.312622	-0.000370	0.000287	-0.001027
 16	0.312897	0.312988	0.312805	-0.000042	0.000287	-0.000370
 17	0.312943	0.312988	0.312897	0.000122	0.000287	-0.000042
 18	0.312920	0.312943	0.312897	0.000040	0.000122	-0.000042
 19	0.312908	0.312920	0.312897	-0.000001	0.000040	-0.000042
 20	0.312914	0.312920	0.312908	0.000020	0.000040	-0.000001
 21	0.312911	0.312914	0.312908	0.000009	0.000020	-0.000001
 22	0.312910	0.312911	0.312908	0.000004	0.000009	-0.000001
 23	0.312909	0.312910	0.312908	0.000002	0.000004	-0.000001
 24	0.312909	0.312909	0.312908	0.000000	0.000002	-0.000001
 25	0.312908	0.312909	0.312908	-0.000000	0.000000	-0.000001
 26	0.312908	0.312909	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
 29	0.312908	0.312908	0.312908	-0.000000	0.000000	-0.000000
 30	0.312908	0.312908	0.312908	0.000000	0.000000	-0.000000
 31	0.312908	0.312908	0.312908	0.000000	0.000000	-0.000000
 32	0.312908	0.312908	0.312908	-0.000000	0.000000	-0.000000

Approximate root = 0.312908