fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. double f(double x) {
  5. return 2 * x * x * x + 3 * x - 1;
  6. }
  7.  
  8. int main()
  9. {
  10. double x1, x2, x0, f1, f2, f0;
  11. double E = 1e-8;
  12. int i = 0;
  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 << "\n-------------------------------------------------------------";
  29. cout << "\nIteration\t x0\t\t x1\t\t x2\t\t f0\t\t f1\t\t f2";
  30. cout << "\n-------------------------------------------------------------\n";
  31.  
  32. do
  33. {
  34. i++;
  35. x0 = (x1 + x2) / 2;
  36. f0 = f(x0);
  37.  
  38. cout << fixed << setprecision(6);
  39. cout << setw(3) << i << "\t\t"
  40. << x0 << "\t" << x1 << "\t" << x2 << "\t"
  41. << f0 << "\t" << f1 << "\t" << f2 << endl;
  42.  
  43. if (f1 * f0 < 0)
  44. {
  45. x2 = x0;
  46. f2 = f0;
  47. } else {
  48. x1 = x0;
  49. f1 = f0;
  50. }
  51.  
  52. }
  53. while (fabs((x2 - x1) / x2) > E);
  54.  
  55. cout << "\nApproximate root = " << x0 << endl;
  56.  
  57. return 0;
  58. }
  59.  
Success #stdin #stdout 0.01s 5280KB
stdin
3
-1
stdout
Enter the value of x0: 
Enter the value of x1: 
-------------------------------------------------------------
Iteration	 x0		 x1		 x2		 f0		 f1		 f2
-------------------------------------------------------------
  1		1.000000	3.000000	-1.000000	4.000000	62.000000	-6.000000
  2		0.000000	1.000000	-1.000000	-1.000000	4.000000	-6.000000
  3		0.500000	1.000000	0.000000	0.750000	4.000000	-1.000000
  4		0.250000	0.500000	0.000000	-0.218750	0.750000	-1.000000
  5		0.375000	0.500000	0.250000	0.230469	0.750000	-0.218750
  6		0.312500	0.375000	0.250000	-0.001465	0.230469	-0.218750
  7		0.343750	0.375000	0.312500	0.112488	0.230469	-0.001465
  8		0.328125	0.343750	0.312500	0.055031	0.112488	-0.001465
  9		0.320312	0.328125	0.312500	0.026666	0.055031	-0.001465
 10		0.316406	0.320312	0.312500	0.012571	0.026666	-0.001465
 11		0.314453	0.316406	0.312500	0.005546	0.012571	-0.001465
 12		0.313477	0.314453	0.312500	0.002039	0.005546	-0.001465
 13		0.312988	0.313477	0.312500	0.000287	0.002039	-0.001465
 14		0.312744	0.312988	0.312500	-0.000589	0.000287	-0.001465
 15		0.312866	0.312988	0.312744	-0.000151	0.000287	-0.000589
 16		0.312927	0.312988	0.312866	0.000068	0.000287	-0.000151
 17		0.312897	0.312927	0.312866	-0.000042	0.000068	-0.000151
 18		0.312912	0.312927	0.312897	0.000013	0.000068	-0.000042
 19		0.312904	0.312912	0.312897	-0.000015	0.000013	-0.000042
 20		0.312908	0.312912	0.312904	-0.000001	0.000013	-0.000015
 21		0.312910	0.312912	0.312908	0.000006	0.000013	-0.000001
 22		0.312909	0.312910	0.312908	0.000003	0.000006	-0.000001
 23		0.312909	0.312909	0.312908	0.000001	0.000003	-0.000001
 24		0.312908	0.312909	0.312908	0.000000	0.000001	-0.000001
 25		0.312908	0.312908	0.312908	-0.000000	0.000000	-0.000001
 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
 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

Approximate root = 0.312908