fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. double f(double x) {
  8. return 2*x*x*x + 3*x - 1;
  9. }
  10.  
  11. int main() {
  12. double x1, x2, x0, f1, f2, f0, E;
  13. int iteration = 0;
  14.  
  15. x1 = 0.0;
  16. x2 = 1.0;
  17. E = 1e-8;
  18.  
  19. f1 = f(x1);
  20. f2 = f(x2);
  21.  
  22. if (f1 * f2 > 0) {
  23. cout << "Initial values do not bracket a root!" << endl;
  24. return 1;
  25. }
  26.  
  27. cout << fixed << setprecision(6);
  28. cout << "Iteration\tx1\t\tx2\t\tx0\t\tf(x1)\t\tf(x2)\t\tf(x0)" << endl;
  29. cout << "----------------------------------------------------------------------------------------" << endl;
  30.  
  31. do {
  32. x0 = (x1 + x2) / 2.0;
  33. f0 = f(x0);
  34.  
  35. iteration++;
  36.  
  37. cout << iteration << "\t\t" << x1 << "\t" << x2 << "\t" << x0
  38. << "\t" << f1 << "\t" << f2 << "\t" << f0 << endl;
  39.  
  40. if (f0 == 0) {
  41. break;
  42. }
  43.  
  44. if (f1 * f0 < 0) {
  45. x2 = x0;
  46. f2 = f0;
  47. } else {
  48. x1 = x0;
  49. f1 = f0;
  50. }
  51.  
  52. } while (fabs((x2 - x1) / x2) >= E && iteration < 100);
  53.  
  54. double root = (x1 + x2) / 2.0;
  55. cout << "\nApproximate root = " << root << endl;
  56. cout << "Number of iterations = " << iteration << endl;
  57. cout << "f(root) = " << f(root) << endl;
  58.  
  59. return 0;
  60. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Iteration	x1		x2		x0		f(x1)		f(x2)		f(x0)
----------------------------------------------------------------------------------------
1		0.000000	1.000000	0.500000	-1.000000	4.000000	0.750000
2		0.000000	0.500000	0.250000	-1.000000	0.750000	-0.218750
3		0.250000	0.500000	0.375000	-0.218750	0.750000	0.230469
4		0.250000	0.375000	0.312500	-0.218750	0.230469	-0.001465
5		0.312500	0.375000	0.343750	-0.001465	0.230469	0.112488
6		0.312500	0.343750	0.328125	-0.001465	0.112488	0.055031
7		0.312500	0.328125	0.320312	-0.001465	0.055031	0.026666
8		0.312500	0.320312	0.316406	-0.001465	0.026666	0.012571
9		0.312500	0.316406	0.314453	-0.001465	0.012571	0.005546
10		0.312500	0.314453	0.313477	-0.001465	0.005546	0.002039
11		0.312500	0.313477	0.312988	-0.001465	0.002039	0.000287
12		0.312500	0.312988	0.312744	-0.001465	0.000287	-0.000589
13		0.312744	0.312988	0.312866	-0.000589	0.000287	-0.000151
14		0.312866	0.312988	0.312927	-0.000151	0.000287	0.000068
15		0.312866	0.312927	0.312897	-0.000151	0.000068	-0.000042
16		0.312897	0.312927	0.312912	-0.000042	0.000068	0.000013
17		0.312897	0.312912	0.312904	-0.000042	0.000013	-0.000015
18		0.312904	0.312912	0.312908	-0.000015	0.000013	-0.000001
19		0.312908	0.312912	0.312910	-0.000001	0.000013	0.000006
20		0.312908	0.312910	0.312909	-0.000001	0.000006	0.000003
21		0.312908	0.312909	0.312909	-0.000001	0.000003	0.000001
22		0.312908	0.312909	0.312908	-0.000001	0.000001	0.000000
23		0.312908	0.312908	0.312908	-0.000001	0.000000	-0.000000
24		0.312908	0.312908	0.312908	-0.000000	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
29		0.312908	0.312908	0.312908	-0.000000	0.000000	-0.000000

Approximate root = 0.312908
Number of iterations = 29
f(root) = 0.000000