fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. double function(double x) {
  8. return x * x * x - x - 3.0;
  9. }
  10.  
  11. double derivative(double x) {
  12. return 3.0 * x * x - 1.0;
  13. }
  14.  
  15. void newton_raphson_method(double x0, double tolerance) {
  16. double xi = x0;
  17. double xi_plus_1;
  18. double error;
  19. int iteration = 0;
  20.  
  21. cout << setprecision(6) << fixed;
  22.  
  23. cout << "--- Newton-Raphson Method for f(x) = x^3 - x - 3 ---" << endl;
  24. cout << "Initial Guess (x0): " << x0 << endl;
  25. cout << "Tolerance (E): " << scientific << tolerance << fixed << endl;
  26. cout << "\n";
  27.  
  28. cout << setw(10) << "Iter"
  29. << setw(15) << "xi"
  30. << setw(15) << "f(xi)"
  31. << setw(15) << "f'(xi)"
  32. << setw(15) << "xi+1"
  33. << setw(15) << "Rel. Error" << endl;
  34. cout << string(85, '-') << endl;
  35.  
  36. do {
  37. iteration++;
  38. double f_xi = function(xi);
  39. double f_prime_xi = derivative(xi);
  40.  
  41. if (abs(f_prime_xi) < 1e-8) {
  42. cout << "Error: Derivative is near zero at xi = " << xi << endl;
  43. break;
  44. }
  45.  
  46. xi_plus_1 = xi - (f_xi / f_prime_xi);
  47.  
  48. error = abs((xi_plus_1 - xi) / xi_plus_1);
  49.  
  50. cout << setw(10) << iteration
  51. << setw(15) << xi
  52. << setw(15) << f_xi
  53. << setw(15) << f_prime_xi
  54. << setw(15) << xi_plus_1
  55. << setw(15) << error << endl;
  56.  
  57. xi = xi_plus_1;
  58.  
  59. } while (error >= tolerance && iteration < 50);
  60.  
  61. cout << string(85, '-') << endl;
  62. cout << "The final root is approximately: " << xi << endl;
  63. cout << "Found after " << iteration << " iterations." << endl;
  64. }
  65.  
  66. int main() {
  67. double initial_guess = 3.0;
  68. double tolerance = 0.000001;
  69.  
  70. newton_raphson_method(initial_guess, tolerance);
  71.  
  72. return 0;
  73. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
--- Newton-Raphson Method for f(x) = x^3 - x - 3 ---
Initial Guess (x0): 3.000000
Tolerance (E): 1.000000e-06

      Iter             xi          f(xi)         f'(xi)           xi+1     Rel. Error
-------------------------------------------------------------------------------------
         1       3.000000      21.000000      26.000000       2.192308       0.368421
         2       2.192308       5.344390      13.418639       1.794027       0.222004
         3       1.794027       0.980105       8.655594       1.680793       0.067369
         4       1.680793       0.067556       7.475195       1.671756       0.005406
         5       1.671756       0.000411       7.384300       1.671700       0.000033
         6       1.671700       0.000000       7.383742       1.671700       0.000000
-------------------------------------------------------------------------------------
The final root is approximately: 1.671700
Found after 6 iterations.