fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. double f(double x)
  5. {
  6. return x * x * x - 4;}
  7. int main()
  8. {
  9. double x1, x2, x0, f1, f2, f0;
  10. double E = 1e-8;
  11.  
  12. cout << "Enter the value of x0: ";
  13. cin >> x1;
  14. cout << "\nEnter the value of x1: ";
  15. cin >> x2;
  16.  
  17. f1 = f(x1);
  18. f2 = f(x2);
  19.  
  20. if (f1 * f2 > 0)
  21. {
  22. cout << "\nInvalid interval..\n";
  23. return 0;
  24. }
  25.  
  26. cout << "\n-------------------------------------------------------------";
  27. cout << "\nIteration\t x0\t\t x1\t\t x2\t\t f0\t\t f1\t\t f2";
  28. cout << "\n-------------------------------------------------------------\n";
  29.  
  30. for (int i = 1; ; i++)
  31. {
  32. x0 = x1 - (f1 * (x2 - x1)) / (f2 - f1);
  33. f0 = f(x0);
  34.  
  35. cout << fixed << setprecision(6);
  36. cout << setw(3) << i << "\t"
  37. << x1 << "\t" << x2 << "\t" << x0 << "\t"
  38. << f0 << "\t" << f1 << "\t" << f2 << endl;
  39.  
  40. if (fabs(f0) < 1e-6 || fabs((x2 - x1) / x2) < E)
  41. {
  42. cout << "\nApproximate root = " << x0 << endl;
  43. break;
  44. }
  45.  
  46. if (f1 * f0 < 0)
  47. {
  48. x2 = x0;
  49. f2 = f0;
  50. } else {
  51. x1 = x0;
  52. f1 = f0;
  53. }
  54. }
  55.  
  56. return 0;
  57. }
  58.  
Success #stdin #stdout 0s 5312KB
stdin
4
-2
stdout
Enter the value of x0: 
Enter the value of x1: 
-------------------------------------------------------------
Iteration	 x0		 x1		 x2		 f0		 f1		 f2
-------------------------------------------------------------
  1	4.000000	-2.000000	-1.000000	-5.000000	60.000000	-12.000000
  2	4.000000	-1.000000	-0.615385	-4.233045	60.000000	-5.000000
  3	4.000000	-0.615385	-0.311224	-4.030145	60.000000	-4.233045
  4	4.000000	-0.311224	-0.039870	-4.000063	60.000000	-4.030145
  5	4.000000	-0.039870	0.212626	-3.990387	60.000000	-4.000063
  6	4.000000	0.212626	0.448803	-3.909600	60.000000	-3.990387
  7	4.000000	0.448803	0.666044	-3.704534	60.000000	-3.909600
  8	4.000000	0.666044	0.859919	-3.364123	60.000000	-3.704534
  9	4.000000	0.859919	1.026632	-2.917957	60.000000	-3.364123
 10	4.000000	1.026632	1.164529	-2.420752	60.000000	-2.917957
 11	4.000000	1.164529	1.274492	-1.929807	60.000000	-2.420752
 12	4.000000	1.274492	1.359422	-1.487752	60.000000	-1.929807
 13	4.000000	1.359422	1.423313	-1.116625	60.000000	-1.487752
 14	4.000000	1.423313	1.470390	-0.820948	60.000000	-1.116625
 15	4.000000	1.470390	1.504534	-0.594302	60.000000	-0.820948
 16	4.000000	1.504534	1.529009	-0.425376	60.000000	-0.594302
 17	4.000000	1.529009	1.546404	-0.301981	60.000000	-0.425376
 18	4.000000	1.546404	1.558691	-0.213129	60.000000	-0.301981
 19	4.000000	1.558691	1.567333	-0.149798	60.000000	-0.213129
 20	4.000000	1.567333	1.573391	-0.104977	60.000000	-0.149798
 21	4.000000	1.573391	1.577629	-0.073416	60.000000	-0.104977
 22	4.000000	1.577629	1.580590	-0.051270	60.000000	-0.073416
 23	4.000000	1.580590	1.582655	-0.035769	60.000000	-0.051270
 24	4.000000	1.582655	1.584096	-0.024936	60.000000	-0.035769
 25	4.000000	1.584096	1.585099	-0.017376	60.000000	-0.024936
 26	4.000000	1.585099	1.585798	-0.012104	60.000000	-0.017376
 27	4.000000	1.585798	1.586285	-0.008429	60.000000	-0.012104
 28	4.000000	1.586285	1.586624	-0.005869	60.000000	-0.008429
 29	4.000000	1.586624	1.586860	-0.004086	60.000000	-0.005869
 30	4.000000	1.586860	1.587025	-0.002845	60.000000	-0.004086
 31	4.000000	1.587025	1.587139	-0.001980	60.000000	-0.002845
 32	4.000000	1.587139	1.587219	-0.001378	60.000000	-0.001980
 33	4.000000	1.587219	1.587274	-0.000959	60.000000	-0.001378
 34	4.000000	1.587274	1.587313	-0.000668	60.000000	-0.000959
 35	4.000000	1.587313	1.587340	-0.000465	60.000000	-0.000668
 36	4.000000	1.587340	1.587358	-0.000324	60.000000	-0.000465
 37	4.000000	1.587358	1.587371	-0.000225	60.000000	-0.000324
 38	4.000000	1.587371	1.587380	-0.000157	60.000000	-0.000225
 39	4.000000	1.587380	1.587387	-0.000109	60.000000	-0.000157
 40	4.000000	1.587387	1.587391	-0.000076	60.000000	-0.000109
 41	4.000000	1.587391	1.587394	-0.000053	60.000000	-0.000076
 42	4.000000	1.587394	1.587396	-0.000037	60.000000	-0.000053
 43	4.000000	1.587396	1.587398	-0.000026	60.000000	-0.000037
 44	4.000000	1.587398	1.587399	-0.000018	60.000000	-0.000026
 45	4.000000	1.587399	1.587399	-0.000012	60.000000	-0.000018
 46	4.000000	1.587399	1.587400	-0.000009	60.000000	-0.000012
 47	4.000000	1.587400	1.587400	-0.000006	60.000000	-0.000009
 48	4.000000	1.587400	1.587400	-0.000004	60.000000	-0.000006
 49	4.000000	1.587400	1.587401	-0.000003	60.000000	-0.000004
 50	4.000000	1.587401	1.587401	-0.000002	60.000000	-0.000003
 51	4.000000	1.587401	1.587401	-0.000001	60.000000	-0.000002
 52	4.000000	1.587401	1.587401	-0.000001	60.000000	-0.000001

Approximate root = 1.587401