fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. double function(double x) {
  9. return x * x - 34.0;
  10. }
  11.  
  12. void secant_method(double x1, double x2, double tolerance) {
  13. double f1 = function(x1);
  14. double f2 = function(x2);
  15. double x3;
  16. int iteration = 0;
  17.  
  18. cout << setprecision(6) << fixed;
  19.  
  20. cout << "--- Secant Method for f(x) = x^2 - 34 ---" << endl;
  21. cout << "Initial Guesses (x1, x2): (" << x1 << ", " << x2 << ")" << endl;
  22. cout << "Tolerance (E): " << scientific << tolerance << fixed << endl;
  23. cout << "\n";
  24.  
  25. cout << setw(10) << "Iter"
  26. << setw(15) << "x1"
  27. << setw(15) << "x2"
  28. << setw(15) << "x3"
  29. << setw(15) << "f(x1)"
  30. << setw(15) << "f(x2)"
  31. << setw(15) << "f(x3)" << endl;
  32. cout << string(95, '-') << endl;
  33.  
  34. while (abs(function(x2)) >= tolerance && iteration < 50) {
  35. iteration++;
  36.  
  37. if (abs(f2 - f1) < 1e-8) {
  38. cout << "Error: Denominator near zero. Secant line is almost horizontal." << endl;
  39. break;
  40. }
  41.  
  42. x3 = x2 - (f2 * (x2 - x1)) / (f2 - f1);
  43. double f3 = function(x3);
  44.  
  45. cout << setw(10) << iteration
  46. << setw(15) << x1
  47. << setw(15) << x2
  48. << setw(15) << x3
  49. << setw(15) << f1
  50. << setw(15) << f2
  51. << setw(15) << f3 << endl;
  52.  
  53. x1 = x2;
  54. f1 = f2;
  55. x2 = x3;
  56. f2 = f3;
  57.  
  58. if (abs(f3) < tolerance) {
  59. break;
  60. }
  61. }
  62.  
  63. cout << string(95, '-') << endl;
  64. cout << "The final root is approximately: " << x2 << endl;
  65. cout << "Found after " << iteration << " iterations." << endl;
  66. }
  67.  
  68. int main() {
  69. double x1_initial = 4.0;
  70. double x2_initial = 2.0;
  71. double tolerance = 0.000001;
  72.  
  73. secant_method(x1_initial, x2_initial, tolerance);
  74.  
  75. return 0;
  76. }
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
--- Secant Method for f(x) = x^2 - 34 ---
Initial Guesses (x1, x2): (4.000000, 2.000000)
Tolerance (E): 1.000000e-06

      Iter             x1             x2             x3          f(x1)          f(x2)          f(x3)
-----------------------------------------------------------------------------------------------
         1       4.000000       2.000000       7.000000     -18.000000     -30.000000      15.000000
         2       2.000000       7.000000       5.333333     -30.000000      15.000000      -5.555556
         3       7.000000       5.333333       5.783784      15.000000      -5.555556      -0.547845
         4       5.333333       5.783784       5.833063      -5.555556      -0.547845       0.024626
         5       5.783784       5.833063       5.830943      -0.547845       0.024626      -0.000100
         6       5.833063       5.830943       5.830952       0.024626      -0.000100      -0.000000
-----------------------------------------------------------------------------------------------
The final root is approximately: 5.830952
Found after 6 iterations.